Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct here to familiarize yourself with the rules and how to post constructively.

  2. Unity 2022.1 is now available as the latest Tech release.
    Dismiss Notice
  3. Improve your project's performance with our new guide on profiling in Unity.
    Dismiss Notice

Resolved What are the recommended ways of serializing and deserializing level data (perhaps in JSON)?

Discussion in 'Scripting' started by seloran, Feb 22, 2021.

  1. seloran

    seloran

    Joined:
    Mar 8, 2020
    Posts:
    38
    I made a level editor with the help of the rather popular "JSON .NET For Unity" free asset from the asset store because Unity's JsonUtility does not support the objects and collections (object, dictionary, enum, list, queue, tuple) I would like to serialize and deserialize.

    Everything was going well in the Unity Editor, but as soon as I tried testing on Android, I got an error presumably due to the deserialization method call as my debug messages before that went through but not the ones after:
    Code (CSharp):
    1. Error Unity ArgumentNullException: Value cannot be null.
    2. Error Unity Parameter name: method
    3. Error Unity   at Newtonsoft.Json.Utilities.ValidationUtils.ArgumentNotNull (System.Object value, System.String parameterName) [0x00000] in <00000000000000000000000000000000>:0
    4. Error Unity   at Newtonsoft.Json.Utilities.LateBoundReflectionDelegateFactory.CreateParameterizedConstructor (System.Reflection.MethodBase method) [0x00000] in <00000000000000000000000000000000>:0
    5. Error Unity   at Newtonsoft.Json.Serialization.JsonArrayContract.CreateWrapper (System.Object list) [0x00000] in <00000000000000000000000000000000>:0
    6. Error Unity   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList (Newtonsoft.Json.JsonReader reader, System.Type objectType, Newtonsoft.Json.Serialization.JsonContract contract, Newtonsoft.Json.Serialization.JsonProperty member, System.Object existingValue, System.String id) [0x00000] in <00000000000000000000000000000000>:0
    7. Error Unity   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue (Newtonsoft.Json.Serialization.JsonProperty pro
    8.  
    I have checked that I'm saving the JSON files into a folder under Resources and that I'm using Resources.Load<TextAsset>("relative/path/under/Resources/to/json/file/without/extension").text to successfully get the file content.

    Is the error likely related to AOT or otherwise related to the lack of updates for "JSON .NET For Unity"? Also, does anyone know if the jilleJr/Newtonsoft.Json-for-Unity repo would be a good alternative to try, or if there are other serialization formats and tips that may help? Any help would be much appreciated.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    24,921
    That won't work at runtime. Things only make it into the Resources.Load() "directory of files" at the moment of build or play.

    You need to write your files to a path from here:

    https://docs.unity3d.com/ScriptReference/Application-persistentDataPath.html

    I imagine once you fix that, your null-ref above will be gone too. But if it's still there, the answer is always the same... ALWAYS. It is the single most common error ever.

    Don't waste your life spinning around and round on this error. Instead, learn how to fix it fast... it's EASY!!

    Some notes on how to fix a NullReferenceException error in Unity3D
    - also known as: Unassigned Reference Exception
    - also known as: Missing Reference Exception
    - also known as: Object reference not set to an instance of an object

    http://plbm.com/?p=221

    The basic steps outlined above are:
    - Identify what is null
    - Identify why it is null
    - Fix that.

    Expect to see this error a LOT. It's easily the most common thing to do when working. Learn how to fix it rapidly. It's easy. See the above link for more tips.

    This is the kind of mindset and thinking process you need to bring to this problem:

    https://forum.unity.com/threads/why-do-my-music-ignore-the-sliders.993849/#post-6453695

    Step by step, break it down, find the problem.
     
  3. seloran

    seloran

    Joined:
    Mar 8, 2020
    Posts:
    38
    I made sure to have saved those serialized level data files to the path under Resources before building. I am only having trouble with the deserialize method call.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    24,921
    I'm sorry, I misunderstood this to think you were doing this at runtime.

    Could be... one theory to test would be to find the JSON classname that is choking this up, then see if it goes away if you put a constructor call in somewhere so it doesn't get stripped.
     
  5. seloran

    seloran

    Joined:
    Mar 8, 2020
    Posts:
    38
    Just to clarify a bit more, the files are saved to the Resources before building, but I am trying to fetch them and have them deserialized at run-time on device.

    Thanks for the suggestion.

    Hmm, would you mind elaborating on “putting a constructor call for it to not get stripped”? I keep hearing about it, but can’t seems to find details on it.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    24,921
    JSON needs a constructor to make classes.

    If you don't have code somewhere that actually news up stuff with that constructor anywhere else in code, that method could be stripped in an AOT environment and then you wouldn't be able to hydrate one of those classes.

    Using the constructor would prohibit it from being stripped.

    "Stripped" here means "code stripping" if you wanna google some more.
     
    seloran likes this.
  7. seloran

    seloran

    Joined:
    Mar 8, 2020
    Posts:
    38
    Oh! So you mean the constructor for the class whose instances I am trying to serialize and deserialize? I’ll try to just new an instance of it then. Thanks!

    Edit: Unfortunately, the issue still persists after I made new instances of the classes I try to serialize and deserialize and rebuild, so maybe it's not a code stripping issue. Maybe it's time for me to try the jilleJr/Newtonsoft.Json-for-Unity repo.

    Edit2: the jilleJr/Newtonsoft.Json-for-Unity repo fixed the issue whatever it was. Now, the JSON level data files seem to be deserializing successfully.
     
    Last edited: Feb 22, 2021
  8. jeyalakshmi_chandrasekaran

    jeyalakshmi_chandrasekaran

    Joined:
    Mar 8, 2021
    Posts:
    7
    The jilleJr/Newtonsoft.Json-for-Unity repo fixed the issue. Both serialization/deserialization of JSON works well.
     
unityunity