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

Feedback Please add System.Text.Json support

Discussion in 'Experimental Scripting Previews' started by stopiccot_tds, Nov 5, 2020.

  1. stopiccot_tds

    stopiccot_tds

    Joined:
    Oct 1, 2016
    Posts:
    111
    Pretty self-describing
     
    CyberAngel and Pepe20129 like this.
  2. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,252
  3. stopiccot_tds

    stopiccot_tds

    Joined:
    Oct 1, 2016
    Posts:
    111
    It lacks a lot of features of proper json serializer/deserializer.
     
    stonstad and phobos2077 like this.
  4. print_helloworld

    print_helloworld

    Joined:
    Nov 14, 2016
    Posts:
    226
    You can also point out why exactly you need that namespace, for example, by listing out the features.
     
    Last edited: Nov 9, 2020
  5. stopiccot_tds

    stopiccot_tds

    Joined:
    Oct 1, 2016
    Posts:
    111
  6. runner78

    runner78

    Joined:
    Mar 14, 2015
    Posts:
    580
    phobos2077 likes this.
  7. TheZombieKiller

    TheZombieKiller

    Joined:
    Feb 8, 2013
    Posts:
    199
    Note that you can still use System.Text.Json (alongside Span<T> and others) via the NuGet package. You just won't be able to take advantage of specific runtime optimizations associated with Span<T> and such. https://www.nuget.org/packages/System.Text.Json/
     
    phobos2077 likes this.
  8. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    804
    phobos2077 likes this.
  9. print_helloworld

    print_helloworld

    Joined:
    Nov 14, 2016
    Posts:
    226
    Given that they want "features for proper json" then the newtonsoft variant is what they're looking for.
     
  10. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    804
    *May work in Net 6 when reflection will be replaced with source generators :)
     
    stonstad likes this.
  11. punppiskamk

    punppiskamk

    Joined:
    Jan 11, 2021
    Posts:
    1
    They would also like to have lighweight solution. Serializing/Deserializing a very simple object could allocate memory of 10s to 100s of MBs. That's not really viable for mobile game development, or any development to be honest. You should not run out of memory because few kB JSON docs.
     
  12. poprev-3d

    poprev-3d

    Joined:
    Apr 12, 2019
    Posts:
    39
    System.Text.Json support would be greatly appreciated. The Unity serialization system (even the new one) lacks features. Newtonsoft.Json is too random and horrible performance-wise.

    System.Text.Json is a good alternative between maintainability, performance, and features. Right now it works on Mono builds but not on IL2CPP/WebGL builds, support for IL2CPP would be greatly appreciated.
     
    thexdd and phobos2077 like this.
  13. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,014
    You may want to try the new features in this build: https://forum.unity.com/threads/il2cpp-build-time-improvements-seeking-feedback.1064135/

    This should avoid the AOT compilation errors that can come with System.Text.Json.
     
    phobos2077 likes this.
  14. bdovaz

    bdovaz

    Joined:
    Dec 10, 2011
    Posts:
    922
  15. Digika

    Digika

    Joined:
    Jan 7, 2018
    Posts:
    205
    That's kinda ironic, considering rewrite of .NET built-in JSON parses inspired by Utf2json and the creator was one of the top contributors to new netcode json library
     
  16. markinovus

    markinovus

    Joined:
    Apr 27, 2021
    Posts:
    5
    Sorry for dragging up an old thread but has anyone managed to get System.Text.Json working at all in Unity?
     
    stonstad likes this.
  17. poprev-3d

    poprev-3d

    Joined:
    Apr 12, 2019
    Posts:
    39
    @markinovus yes !

    You basically have to follow one to three steps, depending on your situation:
    • Gather the required DLL of System.Text.Json with the right version (using NuGet online website, then decompress the nuget package with 7Zip for example). Use the 5.0.0 version of System.Text.Json. You'll need to have all these Dlls:
      • Microsoft.Bcl.AsyncInterfaces.dll
      • System.Buffers.dll
      • System.Memory.dll
      • System.Runtime.CompilerServices.Unsafe.dll
      • System.Text.Encodings.Web.dll
      • System.Text.Json.dll
      • System.Threading.Tasks.Extensions.dll
    • For Il2CPP and WebGL: Add the following link.xml in the Asset folder
      • Code (CSharp):
        1. <linker>
        2.     <assembly fullname="System.Text.Json">
        3.         <namespace fullname="System.Text.Json" preserve="all" />
        4.         <namespace fullname="System.Text.Json.Serialization" preserve="all" />
        5.         <namespace fullname="System.Text.Json.Serialization.Converters" preserve="all" />
        6.     </assembly>
        7. </linker>
    • For Il2CPP and WebGL: You might have to write custom converters for very specific cases such as serializing generic function instances for value types. See the github thread : https://github.com/dotnet/runtime/issues/49772

     
    Last edited: Jan 11, 2022
  18. stonstad

    stonstad

    Joined:
    Jan 19, 2018
    Posts:
    368
    Unity's serializer doesn't run server-side. An important use case is having the same tooling on the client and game server. Newtonsoft.Json works (and it is what I use) but it lacks the minimal allocation and high performance design of System.Text.Json.
     
    Incole, VOTRUBEC and Peter77 like this.
  19. Laicasaane

    Laicasaane

    Joined:
    Apr 15, 2015
    Posts:
    108
    I've never used Unity's JSON serializer. It just lacks all the features I need. And now we just need all that features of Newtonsoft.JSON plus more performance.
     
  20. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    804
  21. Peter77

    Peter77

    QA Jesus

    Joined:
    Jun 12, 2013
    Posts:
    6,252
    Quoting my own post :)

    Unity Technologies provide Newtonsoft.JSON through the Package Manager by now. Just add the following line to
    Packages/manifest.json
    :
    "com.unity.nuget.newtonsoft-json": "2.0.0",
     
  22. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    804
    It's outdated version of https://github.com/jilleJr/Newtonsoft.Json-for-Unity
     
    VOTRUBEC likes this.
  23. Laicasaane

    Laicasaane

    Joined:
    Apr 15, 2015
    Posts:
    108
    I suppose most of the people here asking for System.Text.Json are well aware of this already. However we are now in need of a better solution: higher perf, less allocation. You seem don't understand this point since the beginning?
     
  24. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    804
    You too:
     
  25. Laicasaane

    Laicasaane

    Joined:
    Apr 15, 2015
    Posts:
    108
    I know about neuecc/Utf8Json actually, but didn't successfully use it 3 years ago. 2 years later my main concern changed, I worked mainly on offline games without json data. However, it's now changed again, I'm now working in an online game project. I see neuecc/Utf8Json has been inactive for a long time thus I just want to use something that is more actively maintained.
     
  26. paul-masri-stone

    paul-masri-stone

    Joined:
    Mar 23, 2020
    Posts:
    49
    I know from previous experience (non games applications) the value of including version numbering into JSON and version migration within the code, so that if the code is upgraded and the JSON structure changes then the app still works and can auto-migrate data saved from an older version to the current codebase.

    Unfortunately a limitation of JsonUtility is that it serializes/deserializes the current structure only. If you were to try deserializing data saved from an older version of code, it just wouldn't work.

    I want to have full JsonDocument control (as provided by System.Text.Json), the ability to include a version attribute that I can peek at before deserializing and the ability to manipulate the JSON as a structured object.

    As a temporary workaround, I am considering this approach:

    1. Include the JSON version as a hidden, serializable, private field in the object I want to serialize:
    Code (CSharp):
    1. [Serializable]
    2. public MyClass
    3. {
    4.     [SerializeField, HideInInspector]
    5.     private int _jsonVersion = 1;
    6.     ...

    2. Before deserializing, peek at the version number:
    Code (CSharp):
    1.     public void DeserializeJson(string json)
    2.     {
    3.         private struct JsonVersionStruct
    4.         {
    5.             public int _jsonVersion;
    6.         }
    7.  
    8.         var jsonVersionStruct = JsonUtility.FromJson<JsonVersionStruct>(json);
    9.         if (jsonVersionStruct._jsonVersion == _jsonVersion)
    10.         {
    11.             ...
    But this is a gnarly workaround when there's a perfectly good class in System.Text.Json.

    I like JsonUtility, but it's just too basic to trust with a production system.

    [EDIT: I originally had _jsonVersion as static in step 1 but this won't serialize.]
     
    Last edited: Oct 26, 2021
  27. stonstad

    stonstad

    Joined:
    Jan 19, 2018
    Posts:
    368
    Has anyone succeeded in getting System.Text.Json to run properly in Unity 2021.2?
     
  28. print_helloworld

    print_helloworld

    Joined:
    Nov 14, 2016
    Posts:
    226
    This exact use case has made me manually inject a $jsonVersion header into my json for my levels, and then an upgrading system where its able to take a file from version n and upgrade it match version n + 1 perfectly. If you chain all of the upgraders then you are able to take it from version 1 to version 24 assuming you implement all of the conversions. This does require you to handle the json structure correctly tho, so using something that is able to unflatten a json string can give you really good control, this is something that unity's jsonutility cant do. If it could unflatten json strings that would be amazing.
     
    paul-masri-stone likes this.
  29. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    804
    Seems like System.Text.Json works in 2021.2.7, at least on windows (also il2cpp!) for this class (I've tried int as T):

    Code (CSharp):
    1. public class Obj<T>
    2. {
    3.     public string[] TestString {get; set;}
    4.     public InnerObj TestObj {get; set;}
    5.     public T TestT {get; set;}
    6.  
    7.     public class InnerObj
    8.     {
    9.         public string TestString {get; set;}
    10.     }
    11. }
     
    stonstad likes this.
  30. VolodymyrBS

    VolodymyrBS

    Joined:
    May 15, 2019
    Posts:
    142
    Which IL2CPP Code Generation option did you use?
     
    stonstad likes this.
  31. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    804
    Both work.
     
  32. andyz

    andyz

    Joined:
    Jan 5, 2010
    Posts:
    1,879
  33. JoshPeterson

    JoshPeterson

    Unity Technologies

    Joined:
    Jul 21, 2014
    Posts:
    6,014
    No, there is nothing official at the moment. FWIW we will be happy to look at bugs around System.Text.Json and try to fix them. It should "just work" like any other managed code, but clearly there are some difficulties.

    There are ongoing discussions within Unity about how to support and communicate JSON libraries from managed code. I expect that we will be able to get to a good place though.
     
    AliAlbarrak and andyz like this.
  34. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    804
    Last edited: Apr 25, 2022
    PrimePeter, cyriaca and Peter77 like this.
unityunity