1

I've been looking for a better json library for my Unity project. I need to be serialize using a different name other than the property name, deserialize dictionaries containing arbitrary user defined content and ignore null fields in serialization. I've added the .dll (net20) for Json.NET v12.0.1 to my Unity project and it seems to work fine in my (limited) testing.

public class Foo
{
    [JsonProperty("baz", NullValueHandling = NullValueHandling.Ignore)]
    public Bar Baz{ get; set; }

    [JsonProperty("quux", NullValueHandling = NullValueHandling.Ignore)]
    public Qux Quux{ get; set; }
}

public class Corge: Dictionary<string, JObject>
{
    //whatever the user wants to send will come back from the REST call
}

public void OnResponse(string responseJson)
{
    var corge = JsonConvert.DeserializeObject<Corge>(responseJson);
    var name = corge["name"].ToString();
}

Before I completely refactor my project will there be any limitations using Json.NET in Unity and publishing to different platforms? Would using dynamic work better than extending Dictionary?

3 Answers 3

1
  1. No, Json.NET currently work on all Unity platforms.
  2. I'm against using dynamic for your own data structure. Only use it for deserializing data from external source (API, other languages etc)
Sign up to request clarification or add additional context in comments.

5 Comments

The dynamic model would be both for sending and receiving arbitrary data to/from an API. I guess having an object extend Dictionary would be preferable though.
For clarity I just wanted to confirm that Json.NET will work on any platform Unity can publish to, ie Windows, OSX, Hololens, iOS, Android, Playstation, XBOX, etc. Do you know if the net20 is the appropriate dll to use?
Eh there's a JSON.Net for Unit asset that you should use instead of the default one: assetstore.unity.com/packages/tools/input-management/… And yes it work on all Unity platforms
I’ve seen the one in the Unity Asset Store. I need an open source library to include in my SDK. I don’t see the license for the json.net for Unity. I’m guessing there would be issues using the original json.net since there is one specific for Unity.
Oh, right, there should be no problem using the original json.net now. “JSON.NET for unity” was created because unity used to support .net 2.0 only, but it was 2 years ago. It has been catching up to the latest .net and c# version now
1

As a follow up to this question, after implementing the Json.NET library there were issues making REST requests on the iOS platform.

Exception: System.NotSupportedException: System.Reflection.Emit.DynamicMethod::.ctor
at System.Reflection.Emit.DynamicMethod..ctor (System.String name, System.Type returnType, System.Type[] parameterTypes, System.Type owner, System.Boolean skipVisibility)

We ended up switching to Json.NET for Unity which seems to be working fine.

Comments

0

Upgraded IBM's Watson for Unity, got the same error. Found out IBM is using Newtonsoft JSON.Net. Switched to Json.NET for Unity and things at least sort of working.

1 Comment

Latest update for the Unity SDK Core should fix this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.