ScriptableObject List to Json

I was wondering what is the best way to save a list of ScriptableObjects to Json. I'm currently using Newtonsoft.Json way of doing it and is giving me a warning since the way of loading data from the Json is creating instances of the scriptableObjects with "new ScriptObject" instead of "ScriptableObject.CreateInstance" method. It's just a warning and I don't see any major errors, still, it's picking my interest.

Thanks.

728x90

2 Answers ScriptableObject List to Json

It is better to use JsonUtility. JsonUtility is a unity class with utility functions for working with JSON data.

You can use FromJsonOverwrite(string json, object objectToOverwrite) for ScriptableObjects and MonoBehaviours.

You should not use new keyword to create or instantiate scriptableObjects and monoBehaviours. So JsonUtility is more suitable than other standard .net json library like Newtonsoft for monoBehaviours and scriptableObjects.

JsonUtility Unity3D

4 months ago

I definitely suggest using the Newtonsoft version (JsonDotNet)
Had so much problem with the Unity version. Things got so much easier when i switched to Newtonsoft version.

You can simply say to serialize

string jsonString = JsonConvert.SerializeObject(obj);

or you can simply deserialize by doing this

MyClass obj = JsonConvert.DeserializeObject<MyClass>(jsonString);

4 months ago