Search Unity

  1. Unity Asset Manager is now available in public beta. Try it out now and join the conversation here in the forums.
    Dismiss Notice
  2. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  3. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Feedback Please add System.Text.Json support

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

  1. Kamyker

    Kamyker

    Joined:
    May 14, 2013
    Posts:
    1,085
    No, if performance is required I wouldn't use json at all.
     
    Lurking-Ninja and Deleted User like this.
  2. Cameo221

    Cameo221

    Joined:
    Aug 2, 2018
    Posts:
    37
    On the topic of using various Json libraries in Unity:

    I develop a ScriptedImporter that has to deserialize a json file, so performance is absolutely critical for me to reimport quickly.
    After using Newtonsoft for a long time, I've been trying out a couple of other libraries to provide my importer with something faster.

    After some trials and tribulations to get the libraries working successfully for me, I've tried out SystemTextJson and Utf8Json and benchmarked them via using the profiler. (in unity 2022.1)
    • File with 3010 lines
    • SystemTextJson: 31.07ms
    • Newtonsoft: 21.07ms
    • Utf8Json: 5.41ms
    • File with 32983 lines
    • SystemTextJson: 856.25ms
    • Newtonsoft: 603.51ms
    • Utf8Json: 125.40ms
    I'm quite surprised that SystemTextJson is slower than Newtonsoft, and I'm very convinced that I'm doing something wrong there, but unsure what.
    Another caveat I've also had with SystenTextJson is that I have errors trying to make any IL2CPP builds from some of the older unity versions I'm trying to support. (from 2019.3 and up)

    Overall, Utf8Json is looking really attractive, despite it not having active support for a few years on its GitHub.
    The only caveat I've noticed is that it uses System.Reflection.Emit in its codebase, meaning that any unity version at 2021.2 or older would need to change the API compatibility level from NetStandard to NetFramework in the project settings.
    But overall, not the worst thing in the world. Everything appears to build fine including IL2CPP in this way between all the unity versions I test, not to mention the incredible performance gains (~4-5x faster).

    Hopefully this was interesting for anyone who's looking around at Json libraries for Unity :)
     
    Last edited: Dec 4, 2022
  3. Laicasaane

    Laicasaane

    Joined:
    Apr 15, 2015
    Posts:
    358
    I think you should run this benchmarking in a normal C# project targeting .NET 6 or 7 to have a better reference.
     
    Huszky likes this.
  4. YeCanming

    YeCanming

    Joined:
    Dec 9, 2022
    Posts:
    1
    Can anyone help me to use it?
    Unity gives me the error after I import System.Text.Json from UnityNuget.

    Code (CSharp):
    1. Assembly 'Assets/Packages/System.Text.Encodings.Web.7.0.0/lib/net462/System.Text.Encodings.Web.dll' will not be loaded due to errors:
    2. Unable to resolve reference 'System.Runtime.CompilerServices.Unsafe'. Is the assembly missing or incompatible with the current platform?
    3. Reference validation can be disabled in the Plugin Inspector.
    My Unity version is 2021.3.14f1c1, I tried all combination of {IL2CPP, Mono}*{.NET 4, .NET2.1}
    The version of UnityNuget is 3.0.5,
     
  5. villeHelin

    villeHelin

    Joined:
    Mar 27, 2013
    Posts:
    40
    I manually downloaded the System.Runtime.CompilerServices.Unsafe dll from Nuget website and got rid of that error. I have manually downloaded all these

    upload_2022-12-16_16-51-12.png
     
    HoshimiyaSio likes this.
  6. TCROC

    TCROC

    Joined:
    Aug 15, 2015
    Posts:
    230
    A burst compatible json encoder would be valuable for server side. We run as much as our servers in possible in burst as it allows us to server tremendously more clients.
     
  7. Ted-Brown

    Ted-Brown

    Joined:
    Oct 16, 2012
    Posts:
    33
    Just wanted to airdrop into this repeatedly necro'd thread to explain why some people want to use System.Text.Json. There seems to be a perspective that simply switching to a different library is a valid solution. This is true, if your needs are simple. But that's not always the case.

    I am building a .NET Standard 2.1 library that needs to work on more engines than just Unity (e.g. Blazor). I also need strong support of polymorphic serialization using custom converters. I spent a long time building prototypes and experiments to figure out the best solution for the broad matrix of platforms I'm targeting, and System.Text.Json is the best solution for my needs. Which, to be clear, aren't shared by everyone! But that's the use case I'm looking at.

    Hope that helps.
     
    CodeRonnie, Trigve and JoshPeterson like this.