Search code examples
unity-game-enginejson-deserializationasp.net-core-3.1net-standard-2.1

How exchange json between Unity and .Net Core 3.1?


Hello i am developing a Unity application that will use a .NET Core 3.1 API .I have my models in a separate .net standard 2.0 dll .I would have chosen 2.1 to use the JsonSerializer class but it is not supported by Unity yet.

My question is: How does one serialize and deserializes data between the two applications. If i Serialize using the Newtonsoft.Json how would that go on the other end while deserializing with the JsonUtility.FromJson<T> ?

Is there a conversion available between the two ? Is there a conversion between Unity and JsonSerializer?

Is there any way i could share a .Net Standard 2.1 dll between the two ?


Solution

  • First, System.Text.Json isn't dependent on .NET Standard 2.1, only 2.0. As such, you can simply add the NuGet to your .NET Standard 2.0 class library. Additionally, while it's preferred to use System.Text.Json, you can still opt to continue to use Newtonsoft.Json if you prefer in ASP.NET Core 3.1+.

    Second, the end result of either library is just JSON, so either library can read what the other library created. The only potential difference would be in how they might serialize, i.e. camel-case vs. pascal-case, custom value converters, etc. However, all of that is configurable in both libraries, so you just need to ensure that the way one is serializing is also the way the other is deserializing.