3

Does anyone know why this code below will return the error

ArgumentException: Cannot deserialize JSON to new instances of type 'CatalogueList.'
UnityEngine.JsonUtility.FromJson[CatalogueList]

My Catalogue list lives in my assets and i can serialize and upload to my server perfectly fine, im trying to download my file and fill in all fields with the json.

Here is the code.

void AddDownloadedItems(string text, CatalogueList list)
{
    CatalogueList inventoryItemList = UnityEditor.AssetDatabase.LoadAssetAtPath("Assets/CatalogueItem.asset", typeof(CatalogueList)) as CatalogueList;
    list = JsonUtility.FromJson<CatalogueList>(text);
}

But as i say it will just return the error?

CatalogueItemList Code

public class CatalogueList : ScriptableObject { 
    public List<CatalogueItem> itemList;
}

Catalogue Item Code

[System.Serializable]                                                           //  Our Representation of an InventoryItem
public class CatalogueItem
{
    public string databaseID;
    public string itemName = "New Item";
    public enum PrizeMachine { Bronze, Silver, Gold, Platinum };
    public PrizeMachine myMachine;                     
    public Texture2D itemThumb = null;
    public Texture2D itemIcon = null;
    public string itemThumbName;
    public string itemIconName;
    public string shortDescripion;
    public string fullDescription;                                 
    public int priceInBronze;
    public int priceInSilver;
    public int priceInGold;
    public int priceInPlatinum;
}
John Esslemont
  • 87
  • 1
  • 3
  • 10
  • 1
    Likely a discrepancy between the saved json and the CatalogueList class. Perhaps you saved a json then edited the class and now you're trying to load & deserialize the old data? Debug.Log it before deserializing so you can verify that the json is correctly mapped to the CatalogueList class – Fredrik Schön Mar 20 '17 at 13:10
  • 1
    Is CatalogueList static, Interface or abstract or similar? Please add the CatalogueList code to the question – Fredrik Schön Mar 20 '17 at 13:11
  • Added code above, Also catalogue list is a scriptable object. Debugging my downloaded json returns what it should be. – John Esslemont Mar 20 '17 at 13:22
  • I have created an editor window that allows our designers to create new prizes as they wish, when they are done they will press upload which converts to json and uploads to our server, i have checked the uploaded json vs the downloaded json and they are exactly the same. I just for some reason cannot deserialize it back into the scriptable object our editor uses which lives in the assets folder. Just thought i would explain what i am busy trying to do. – John Esslemont Mar 20 '17 at 13:50
  • 1
    Yeah, that's always a good idea to do; avoids the [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). Anyhow, I've never done anything with ScriptableObjects so I can't be sure but it sounds like the FromJson tries to `new` the CatalogueList which doesn't seem to work. The little I've read about it doesn't seem like this is how you should work with them, but, again, I don't know much about how they work. – Fredrik Schön Mar 20 '17 at 13:55
  • I re-created your issue locally, and indeed. This is because it inherits of ScriptableObject. I did find the solution, however. – Fredrik Schön Mar 20 '17 at 14:06

1 Answers1

13

Use FromJsonOverwrite instead!

Note that the JSON Serializer API supports MonoBehaviour and ScriptableObject subclasses as well as plain structs/classes. However, when deserializing JSON into subclasses of MonoBehaviour or ScriptableObject, you must use FromJsonOverwrite; FromJson is not supported and will throw an exception.

https://docs.unity3d.com/2022.3/Documentation/ScriptReference/JsonUtility.FromJsonOverwrite.html

So for you that'd be

JsonUtility.FromJsonOverwrite(text, list);

Note that this is not an immutable method and will overwrite your object, but it seems like that was your intention anyway.

PinguinoSod
  • 85
  • 1
  • 9
Fredrik Schön
  • 4,888
  • 1
  • 21
  • 32
  • Hey @Fredrik I also tried that as that is 100% my intentions, but i keep getting the error in VS with cannot convert void to CatalogueItem which in my mind right now makes no sense at all haha. Any ideas? – John Esslemont Mar 20 '17 at 14:19
  • 1
    But not in the Unity Editor? From that same line of code? Have you built the code in VS? Try restarting VS? Works fine here! Does the feature work when you changed from FromJsonOverwrite? Doubleclick on the error to see where in your code it is - maybe you used the old method on other places. – Fredrik Schön Mar 20 '17 at 14:21
  • 1
    OMG I was trying to assign my list to it! :( Ok dumb moment. Thank you fredrik for helping me man! – John Esslemont Mar 20 '17 at 14:29
  • Hey, still have the error: `ArgumentException: JSON must represent an object type.` – Ben Jun 15 '20 at 23:13
  • Hi Ben! I'm guessing you're doing something like ToJson() with a string? It expects an object, which it translates to a string formatted as json, so that it can later be deserialized to the object again. – Fredrik Schön Jun 16 '20 at 19:18