JSON .NET Port

This has now been released. For those following this thread:
Announcement Thread: http://forum.unity3d.com/threads/200336-RELEASED-JSON-NET-For-Unity-3-5-with-AOT-Support
Asset Store Link: https://www.assetstore.unity3d.com/#/content/11347

Since no one has done it, I’m in the process of porting Json.Net to work with Unity. It’s a pretty extensive task as it makes heavy use of conditional compilation and shares a code base across all versions but I should have it finished and initial testing complete today.

The only question mark is how it will work with RT Apps (Windows 8) so I’m looking for a volunteer or two that would like to help me test it since I don’t have access to a Surface tablet (though I can do the testing on the Windows 8 side).

I’ve used the LitJson port which works with Unity but it’s a little bit lacking. Json.Net has excellent proven performance. It also has more features and is just as easy to use as LitJson. One thing ListJson lacks for instance is Guid support. Json.Net also handles GMT DateTime formats and offsets and JavaScript formatted dates particularly well. These are many of the reasons that Microsoft opted to use it instead of their own JavascriptSerializer in MVC4.

I’m porting it currently to work with Unity 4. Once I’ve tested heavily I will need to make a few more small tweaks and should be able to ensure compatibility with Unity 3 as well (the versions utilizing .NET 2.0).

If anyone is interested in helping me test this out, let me know. I plan to then release it as an Asset on the Asset Store.

Thanks!

Dustin

If you can get it to work on IOS and webplayer, consider me sold. I’m facing this issue when trying JSON 5.0f6

http://forum.unity3d.com/threads/133505-webplayer-and-System-Collections-ObjectModel-KeyedCollection

Well that will be the real test. Could you test it for me on iOS? I have it ported. I’m just cleaning up warnings now. There are a couple of uncertainties around some of the security attributes that were used as to whether they will play nice with Mobile so I’ll need someone to test it. I also found that even the .NET 3.5 version of Mono is missing some features form System.ComponentModel relating to observable properties and collections. I could have stripped the LINQ2Json support out but I wanted to keep as much in tact as I could so I re-implemented the missing Observables:

AddingNewEventHandler
AddingNewEventArgs
PropertyChangingEventHandler
PropertyChangingEventArgs
INotifyPropertyChanging

I also threw in a loose impelentation for observable collections through INotifyCollectionChanged but the weren’t being used for .NET 2.0 or 3.5 anyway so they’re not in use.

I should also note that this is the latest trunk for Json .NET so it’s technically the 6.0 version :wink: No need to rely on an older version especially since there were bug fixes.

So far so good. It’s serializing and deserializing mostly as expected. :wink: I did have a bit of an issue with the Vector3 type I need to look into. It’s an exception around a self-referencing loop with Vector3.normalize so I need to look into that. I may have to code in explicit vector3 support.

Alright it’s fixed. There are some serialization settings you can change globally, but in this case we need to ignore circular serialization for Vector2, Vector3 and Vector4’s so I added special handling to make sure we don’t run into the issue and it’s working great. :wink: Do you have Skype or Google Talk? I was reading your issue with KeyedCollections and I’d like to get some more info to test. The KeyedCollection class is an abstract class so you can’t deserialize to it directly… but you can use a Dictionary<TKey, TValue> which inherits KeyedCollection<TKey, TValue>. For instance I did the following and it serialized and deserialized fine (going to check it in the web player next):

var d = new Dictionary<string, int>
{
	{"A", 1},
	{"B", 2},
	{"C", 3},
	{"D", 4}
};

var json = JsonConvert.SerializeObject(d);
Debug.Log(json);

var d2 = JsonConvert.DeserializeObject<Dictionary<string, int>>(json);
Debug.Log(d2.Count);

Ok so I did some testing with the WebPlayer. It appears that the signature for KeyedCollection is different in the WebPlayer version. Not sure why, but it shouldn’t be difficult to fix, just going to take a little time.

There were lots of differences, but I now have it working properly with KeyedCollections in WebPlayer. :wink:

I have also confirmed that it is working with Unity 3.5 and higher. I’ll be putting together a package hopefully tonight and pushing it to the Asset Store. My tests have all been successful thus far, with the exception of not being able to test on an actual iOS device yet. I want to build out one more suite of tests to make sure it’s working properly with common built-in Unity types with the least amount of effort.

One more quick note and I’ll stop flooding my own post. :confused: If anyone is following this and interested, one of the other big advantages of this port is that it has BSON support (Binary JSON). Processing for BSON is faster, more compact, and of course it’s binary. :slight_smile: I’ve been testing BSON as well and it’s working great.

My skype is my nick. IOS is tricky because it works in the editor but on device, it produces AOT error.

Guess we’ll just have to try it. :wink: Sending you a download link via Private Message.

Big big thanks to imtrobin for helping with the testing and AOT (iOS) compatibility. This is now working on iOS and in Unity 3.5+. We had to revert to a slightly older version of JSON .NET (R4 instead of R6) to get everything rolling but the features are the same. I’m working on documentation and a sample project and then will be pushing to the Asset Store as well as some other locations.

Yay! This works great on webplayer and IOS, supporting even custom null types. I have tried LitJSON, fastJSON, JSONFx, all are lacking in features or not working on IOS compared to JSONNet. This is the only JSON serialization lib you need for unity now!

Will this be a full-source code option or a DLL?

BTW, @imtrobin, I’m glad I’m not the only one having AOT errors with iOS. You tell some vendors this issue, they pretend to be deaf and don’t bother to update their libs, then shoo you away.

This will be full source. Also, bing thanks to eskimojoe for doing additional iOS testing and verifying that Bson is working with iOS as well. I’m also talking with the developer of Recast https://www.assetstore.unity3d.com/#/content/7041 and working out a deal so he can get it included in his asset. I haven’t personally used Recast but it looks like a pretty good Asset and this should clear up his AOT errors since they are due to his Json .NET dependency.

I’ve published to the Asset store and I’m just waiting for approval. Not sure how long it generally takes.

I am quite keen on this, I’ve used most of the JSON solutions - but they all seem to have their quirks and issues.

I’ll certainly be following this thread, look forward to trying it out.

Good good. Now we’re just at the mercy of the reviewers to get it approved for the Asset Store. I’ve tested it pretty heavily. I don’t have access to an iOS device myself so I’ve relied on others to help me test but it’s been solid and no AOT errors. Both Json and Bson are working properly for serialization and deserialization. Testing for Blackberry and Android have been a bit more limited but are still underway. As a professional developer I’m also of the strong belief that you don’t roll bugs across major versions (unless it’s a limitation of the platform), so if I do get any reported bugs I’ll tackle them and roll them into a minor version update. Right now my contact is through my blog but I’m in the processing of building a website dedicated to my Assets (there will only be the one for now).

I focused on JSON .NET because it’s been a big request from the community that I’ve seen over and over and, surprisingly, no one has done an actual conversion. People have managed to get older versions working to varying degrees but still suffer from iOS and Webplayer compatibility issues so I focused on taking care of the AOT issues and getting it working properly for the Webplayer. The conversion wasn’t really that difficult for the most part, but it was time consuming and very tedious to track down every issue and get it cleaned up and I couldn’t have done it without help testing from the community.

I’ll definitely update here once it’s released (approved) on the asset store. The asset I pushed has full source code. This is necessary because there are some precompiler conditionals that modify compilation behavior based on the environment to handle support differences between platforms. I’ve also bundled a sample scene and monobehavior that demonstrates different serialization and deserialization scenarios (both JSON and BSON). It’s a pretty ugly scene that just has three GUIText labels but the important part is on the example code.

For the most part, the documentation provided for the standard JSON .NET version will hold true for this one. I’ve maintained all of the same namespacing and types that are supported by Unity (I removed some, such as the System.Data dependent functionality), so if there are other third-party assets with a JSON .NET dependency that fail with AOT errors, you can safely delete the Newtonsoft.Json.Net.dll and drop in this asset and it will work so long as they didn’t compile their assemblies with specific version dependencies, though there’s no reason they should have. Once I’ve got the Asset site finalized, I’ll add my own documentation and provide code samples that are specifically geared toward Unity users.

I also added an additional configuration setting. People will have different preferences on how they want their Enums to be serialized, so I added a global setting to the serializer settings to allow the behavior to be modified. By default, the serializer will serialize Enum properties as their Name so if you have the following:

    myObject.Property = MyEnum.ValueA;

It will get serialized as “ValueA”. You can change the behavior by setting:

   JsonSerializerSettings.DefaultEnumSerializationHandling = EnumSerializationHandling.Value;

The valid values for EnumSerializationHandling is “Value” and “Name”. Setting it to Value will use the integer value, so it would be serialized as 3 rather than “ValueA” (if ValueA were the fourth item in your Enum or was set to a value of 3).

Right now this is a global setting, so you can set it once and it will be maintained. No configuration change is required to deserialize as it will handle either the name or value for an Enum. There are readability reasons for using the Name rather than the Value, but sometimes you want more compact data representation so the Value might be right for you. You can set it to Value, serialize an object and set it right back to Name if you want. There are other default properties in the serializer which can be overridden so eventually I plan to make Enum handling a property that can be overridden per serialization operation or with an attribute setting but it’s minor so for now it’s just a global setting. Most likely if you want to use Value instead of Name you’re going to want to do it consistently for every serialization so the global setting is most appropriate.

If you have any questions while we wait for the Asset to be approved, feel free to post here or reach out to me and I’d be happy to answer.

This has now been released. For those following this thread:
Announcement Thread: http://forum.unity3d.com/threads/200336-RELEASED-JSON-NET-For-Unity-3-5-with-AOT-Support
Asset Store Link: https://www.assetstore.unity3d.com/#/content/11347

Edit: Forum gave me an error and double posted for some reason.