4

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;
}
6
  • 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 Commented Mar 20, 2017 at 13:10
  • 1
    Is CatalogueList static, Interface or abstract or similar? Please add the CatalogueList code to the question Commented Mar 20, 2017 at 13:11
  • Added code above, Also catalogue list is a scriptable object. Debugging my downloaded json returns what it should be. Commented Mar 20, 2017 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. Commented Mar 20, 2017 at 13:50
  • 1
    Yeah, that's always a good idea to do; avoids 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. Commented Mar 20, 2017 at 13:55

1 Answer 1

14

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.

Sign up to request clarification or add additional context in comments.

5 Comments

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?
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.
OMG I was trying to assign my list to it! :( Ok dumb moment. Thank you fredrik for helping me man!
Hey, still have the error: ArgumentException: JSON must represent an object type.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.