Json.NET under Unity3d for iOS
Asked Answered
E

7

13

has anyone succeeded to adapt/port Json.NET to a version able to run under Unity3d deployed to iOS (and the webplayer, and android, too, but these seem less problematic), somehow overcoming the AOT issues there?

Or are there plans to release a compatible version of Json.NET?

Many thanks,

Max

Elaineelam answered 3/5, 2013 at 13:6 Comment(0)
T
12

You can use netstandard version of Newtonsoft.Json in Unity and it works fine. It seams that this library has changed some inner implementations to not use System.Reflection.Emit for deserialization anymore. Just be sure that after downloading nuget package you get Newtonsoft.Json.dll from netstandard2.0 folder, not from net45. And also don't forget to specify in link.xml that System.Linq.Expressions.Interpreter.LightLambda class shouldn't be stripped (it's used for deserialization) along with libraries/classes that you use for deserialization (to preserve constructors). Your link.xml should look smth like this:

<linker>
    <assembly fullname="Your.Dto.Package.Name" preserve="all" />
    <assembly fullname="System.Core">
        <type fullname="System.Linq.Expressions.Interpreter.LightLambda" preserve="all" />
    </assembly>
</linker>

We use this library in all our projects for Android and iOS with IL2CPP and Mono runtimes. Moreover, unlike Unity's JsonUtility, Newtonsoft.Json fully supports any type of objects, including Dictionary.

Microsoft Example

Trygve answered 26/12, 2019 at 10:20 Comment(1)
In order to get this working in 2021 I had to set the lL2CPP Code generation: Faster (smaller) build; true, in the iOS build settings. But it works.Cooperative
B
6

Newtonsoft Json fails AOT in iOS and also fails at deserializing anything inheriting from KeyedCollection in the webplayer. Here's a version that has been fixed:

JSON .NET For Unity (Supports iOS)

Betthezel answered 14/9, 2013 at 0:12 Comment(0)
U
2

We use MiniJson and so far it serves our json needs well :)

Unfasten answered 4/5, 2013 at 11:53 Comment(2)
That looks interesting. I need to share code between the (Unity) client and the server portion of my game, this one looks feasible.Elaineelam
However, bit of a drag it's so low-level, as compared to Json.NETElaineelam
I
2

I have been facing the same issue for a while and searched on every nook and cranny of the internet to look for a solution. Here's what worked for me.

What's the difference?

  • NewtonSoft.Json for Unity is different from standard NewtonSoft.Json
  • NewtonSoft.Json does not support AOT targets for IL2CPP builds in Unity
  • NewtonSoft.Json for Unity supports AOT targets such as all IL2CPP builds (WebGL, iOS, Android, Windows, Mac OS X) and portable .NET (UWP, WP8).

Hope this helps. Cheers!

Ian answered 12/11, 2020 at 22:34 Comment(0)
C
1

Nope AFAIK.

LitJson and JsonFX would be alternate choice for Unity3D.

http://wiki.unity3d.com/index.php?title=UnityLitJSON

Cindiecindra answered 3/5, 2013 at 13:49 Comment(2)
does LitJSON work under iOS/AOT? That page doesn't say anything. Any experiences?Elaineelam
@AModernRonin:Yes.It works on android and iOS.But to AOT approach,I'm not sure if LitJson would be suitable for your code.It would be better that you provide your sample code to describe your requirements.Cindiecindra
H
1

Have a look at the modified version of LitJson: UnityLitJson


Actually LitJson has some issues and is not always working as expected, so I created a simple JSON-Lib that can handle all Unity3D Types, Enums and arbitrary classes. Find it at GitHub: Tiny-JSON

You can use it very simple:

// encode
Animal a = new Animal(4);
string json = Json.Encode(a);

// decode
IList<Animal> a = Json.Decode<IList<Animal>>("[{\"legs\":4}, {\"legs\":2}]");
Huonghupeh answered 23/3, 2015 at 11:26 Comment(0)
B
-1

Yes, we share code on client and server and use an older version when it was called NewtonSoft.Json. Works in IOS, Android, everything.

Search for NewtonSoft.Json, and you could probably find an archive.

Bugle answered 8/5, 2013 at 0:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.