Search Unity

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

Newtonsoft Json package and ILStripping

Discussion in 'Scripting' started by Joen-EyeJustRead, Mar 30, 2022.

  1. Joen-EyeJustRead

    Joen-EyeJustRead

    Joined:
    Dec 16, 2020
    Posts:
    2
    When porting our project from standalone to webgl we've hit a wall with the Newtonsoft Json package for our TransferModel communication with our rest api servers. We've tried the 3.0.1 and 2.0.2 versions.

    ILStripping causes Json deserialization to fail on certain constructs. First off we get no hints to what part of the json fails to serialize. After we've isolated the problem we've found ourselves unable to work around it.

    A simple HashSet properties like this fails at runtime with absolutely no helpful debug information.

    The runtime exception is straight up garbage.
    System.ArgumentNullException: Value cannot be null.
    Parameter name: method
    at Newtonsoft.Json.Utilities.ValidationUtils.ArgumentNotNull (System.Object value, System.String parameterName)
    at Newtonsoft.Json.Utilities.LateBoundReflectionDelegateFactory.CreateParameterizedConstructor (System.Reflection.MethodBase method)
    at Newtonsoft.Json.Serialization.JsonArrayContract.CreateWrapper (System.Object list)
    at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateNewList (Newtonsoft.Json.JsonReader reader, Newtonsoft.Json.Serialization.JsonArrayContract contract, System.Boolean& createdFromNonDefaultCreator)

    The actual source code of the package is nowhere to be found, the link points to the non AOT public NewtonSoft.Json repo. But clearly LateBoundReflectionDelegateFactory.CreateParameterizedConstructor has been modified by Unity because the official version just tries to call the HashSet<int>(ICollection<int> data) constructor but keeping that in there didn't work. Taking at look at the actual dll reveals that the culprit is
    the invention of something called internal class CollectionWrapper<T>. It turns out that the constructor CollectionWrapper<HashSet<int>>.CollectionWrapper(ICollection<int> list) is not available. Since the class is internal I can't even manually reference it in my code and as such I see no way to work around this.

    So please:
    * Add a simple try catch with some relevant debug information could tell us which part of the json parsing fails!
    * Stop hiding the source code for Open Source Projects! If I had code and could compile my own version of an open source project I could have fixed this in 30 minutes tops.
    * Stop making classes internal so we can't reference them in code and prevent them from being stripped.

    Needless to say making webgl builds is SLOW, so I've wasted a full day on this crap.
     
  2. chanon81

    chanon81

    Joined:
    Oct 6, 2015
    Posts:
    168
    I'm running into this same problem. Can't deserialize HashSets anymore.

    It's super frustrating because Unity 2021.3 is supposed to be LTS but between 2021.3.1f1 and 2021.3.2f1 a breaking change was made where looking in the package dependencies, the package "com.unity.services.core" now directly depends on "com.unity.nuget.newtonsoft-json".

    I was using my own copy of newtonsoft json in my Assets folder, but because of Unity now requiring use of the nuget package, I had to delete my own copy in my Assets folder to fix the duplicate symbol errors.

    Then my game crashes for some reason and I've tracked it down to not being able to deserialize HashSets from the game saves.

    I've wasted a full day trying to fix this issue ... just from upgrading a point version from 2021.3.1f1 to 2021.3.2f1 .... dealing with stuff like this makes me want to pull my hair out! Why does Unity keep making breaking changes between point versions wasting our time so much?

    Tried doing this, but still got errors at runtime:
    https://forum.unity.com/threads/unable-to-deserialize-hashsets.962805/#post-7046434

    Gonna have to call it a day.
     
  3. chanon81

    chanon81

    Joined:
    Oct 6, 2015
    Posts:
    168
    norcrel and makomarkus like this.