- Home /
[JsonUtility] cannot deserialize json to new instances of type X
Hi, so i'm trying to create a "PlayerStats" object using the JsonUtility class. There are no errors in the Editor but whenever i run the code i get the Exception : System.ArgumentException: Cannot deserialize JSON to new instances of type 'PlayerStats.' and i don't know why. I use the method just like they do in the documentation but i keep getting that exception. The json file was created using JU.ToJson and should work without problem. What am i missing ?
if (File.Exists(savePath + "\\" + name + ".json")) {
string saveData = File.ReadAllText(savePath + "\\" + name + ".json");
GameObject localPlayer = (GameObject)Instantiate(Resources.Load("Player/" + JsonUtility.FromJson<PlayerStats>(saveData).race.ToString()));
}
the json file :
{
"race": 0,
"playerName": "xxx",
"initative": 10.0
}
PlayerStats.cs :
public class PlayerStats : MonoBehaviour {
public Race race;
public string playerName;
public float initative = 10;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
}
Update: Looks like i can create PlayerStats object from within the PlayerStats script with JsonUtility.FromJson but not within any other script.
Post the definition of the PlayerStats class as well, please.
That. I suppose PlayerStats has a different definition from what the Json describes.
Answer by Bunny83 · Feb 04, 2017 at 03:56 PM
"PlayerStats" should not be a MonoBehaviour. The JsonUtility can only be used for data classes. MonoBehaviours are components which need to be attached to gameobjects. You can't simply "create" an instance, that's why it failed.
Try:
[System.Serializable]
public class PlayerStats
{
public Race race;
public string playerName;
public float initative = 10;
}
Of course this class can not be attached to a gameobject. It's only ment as a data class that represents your save data. Also note that the "Race" class also can't be a MonoBehaviour. All classes you want to save / restore have to be normal serializable classes (or maybe derived from ScriptableObject, though i never used that JsonUtility myself).
ps: by looking at your json data it looks like your "race" is actually a number. I doubt that it can create a "Race" instance from a number. Have you created that Json data yourself?
"Race" is just an enum and the json was created using JasonUtility.ToJson(). I see, eventhough it's strange i can create multiple PlayerStats object from within the PlayerStats script. I have created my own Jsonparser for my use-case but i will keep it in $$anonymous$$d incase i use JsonUtility in the future. Thanks.
Answer by gz7190 · Aug 28, 2019 at 05:52 PM
In case anyone else comes across this and still wants to serialize MonoBehaviour or ScriptableObject, the fine print in the docs state the following:
However, when deserializing JSON into subclasses of MonoBehaviour or ScriptableObject, you must use FromJsonOverwrite; FromJson is not supported and will throw an exception.
source: https://docs.unity3d.com/Manual/JSONSerialization.html
Thanks for pointing this out, this finally solved my problem deserializing a ScriptableObject from JSON!
Your answer
Follow this Question
Related Questions
Deserialize Json into list.,how to deserialize a list via .FromJson 1 Answer
JSON invalid value 1 Answer
Paring json Array object 3 Answers
Deserializing dictionary with 'Json.Net' for Unity returns a null object. 0 Answers
JSON deserialization 0 Answers