- Home /
What is the easiest json library to integrate with Unity?
I'm looking to integrate JSON into my game engine (rather than serializing xml). Has anybody else had any experience and can recommend a good json library? Does Unity ship with a mono-based json implementation?
Thanks!
JSON can be pain in the ass if you want it to happen more than 20 times a second. All those libraries are not native and can be slow.
If you want something fast use String.split() and you can create any structure you want.
This isn't very good advice.
First of all, most decent JSON parsers can do thousands or tens of thousands per second, so 20 per second is not a very high bar.
Secondly, String.split()
is going to produce structures which are very difficult to navigate especially when you get into nested JSON objects.
Finally, the memory used by successive String.split()
(to produce arrays of strings) will slow your execution down considerably more than a proper lexer/parser.
Agree with @mckamey String.Split() is terrible advice for the reasons he says.
@Fattie I had some problems with that library:
cannot decode an exponential float when it has encoded it itself
does not auto escape strings
dos not support dictionary types
does not tolerate missing variables
I tried fastJson but it was too clever and was injecting IL so blew up immediately and probably wouldn't work on iOS and LitJson may be no better - does not have a method to escape a string??? Does not auto escape on its own.
I may need to either work around the libraries short-cum$$anonymous$$gs or write my own (ugh).
Answer by Ashkan_gc · Nov 20, 2009 at 07:40 PM
you can use this JSON library. JSONsharp
also you can go to JSON's official web site
to get more info
Unfortunately 'jsonsharp' pack seems largely useless. I use and have previously strongly recommended:
"X$$anonymous$$L-JSON Serialization" from the Asset store ..
http://www.aworldforus.com/xml-json-serialization/
It is extremely easy to use,documented, continuously developed and they answer email instantly. Eg:
var stream:JSONOutStream = new JSONOutStream( );
stream
.Content("name", "$$anonymous$$")
.Content("height", 1.12)
.End();
Debug.Log("JSON done... " + stream.Serialize() );
HOWEVER, PLEASE NOTE BOVINE'S CO$$anonymous$$$$anonymous$$ENTS ON THIS PAC$$anonymous$$AGE, BELOW.
As stated above I had the following issues with this X$$anonymous$$L-JSON lib on the asset store:
cannot decode an exponential float when it has encoded it itself
does not auto escape strings
does not support dictionary types
does not tolerate missing variables - i.e. if I have extended a class previously serialized
Using it as @Fattie is might be better that automatic serialization... Also, some of the above may have been fixed now.
Looking at the code I also felt it looked a tad hacky. It's cheapish and functional however, but for me the above points killed it, though I could handle point 3 missing had 1 and 2 been present and point 4 might have been something I could stomach, but if you use it to save game data for instance and then add to that structure it just won't deserialize that structure anymore and your save is toast.
You could also use my SimpleJSON parser ;) It's actually the most advanced one around, but still doesn't have an own type for the different "JSON value types". However there are easy to use casting properties like ToInt, ToFloat, ... which can be used to read to write a value.
There's an implicit string to JSONData and JSONData to string operator, so it's very easy to use. I use it myself all across my project to save the levels the user can build, compress them, load from or send to a server, ... The targetplatform of my game is Android and iOS but it works on most others as well except Webplayer and flash for the time being due to file access routines.
I will update it when i have some time, but at the moment there isn't any ;)
ps: It does read it's own format and does escape critical stuff (namely '\\', '\"', '\n', '\r', '\t', '\b', '\f')
@bov for future readers, per your question title I believe the EASIEST one to use is the "aworldifus" one mentioned. I am a leading expert on finding the "easiest" thing to use, believe me :-) (ie, my general stupidity and incompetence is proof os ease of use .. "if Fattie can install and use it ANYONE can use it" :) )
Answer by PaulT · Nov 20, 2009 at 09:19 PM
I use LitJSON in my projects. This is also soon to be implemented officially in u3dobject, for embedding unity and communicating between Unity and Flash on a website.
Personally I find this the easiest library to use.
Hi Paul. I tried to use LitJSON, but run into a "max allowed depth" exception for pretty much anything I try to serialize - even if I'm simply trying to serialize a float. Did you encounter this?
I've found that whenever I want to serialize a float I usually need to truncate it to a few decimal places to avoid getting the "max allowed depth" errors. A single float is a lot of numbers, though, so to conserve bandwidth you'd probably better be doing that anyway.
I have the same problem with LitJSON. It fails in Ints as well :( JsonException: $$anonymous$$ax allowed object depth reached while trying to export from type System.Int32 LitJson.Json$$anonymous$$apper.WriteValue (System.Object obj, LitJson.JsonWriter writer, Boolean writer_is_private, Int32 depth) (at Assets/Plugins/LitJson/Json$$anonymous$$apper.cs:761) LitJson.Json$$anonymous$$apper.WriteValue (System.Object obj, LitJson.JsonWriter writer, Boolean writer_is_private, Int32 depth) (at Assets/Plugins/LitJson/Json$$anonymous$$apper.cs:753)
LitJSON doesn't support type-hinting, so you need to know at compile time which type you are deserializing. If you need to be able to deter$$anonymous$$e the type dynamically (for example to deserialize derived classes) then you'll need to add support for type hinting, or use another library, such as Json.NET.
From my experience LitJSON really does not work well on AOT compiled platforms, such as iOS and the Xbox360.
Answer by Tetrad · Feb 18, 2011 at 05:35 PM
I'm a fan of $$anonymous$$iniJSON. It's extremely simple to use, and is the most portable, reliable, Unity3D compatible JSON parsing library that I've used. I've used it on when targetting Web, iOS, Android, and desktop PC standalone, without any problems, which is more than I can say about other JSON libraries (ie: LitJson).
Answer by Brian-Kehrer · Nov 21, 2009 at 03:27 AM
We have a customized JSON library jormungandr rebuilt based on Jayrock. It is far smaller than the default Jayrock library (which uses XML for some reason - we removed that), and we have recompiled it for .net 1.1 so it can be used on the iPhone as well. The final size is very very small, not even close to System.XML
$$anonymous$$, excellent I sent you a P$$anonymous$$. thx
Any possibility to post a link? I'd be interested to take a look. I'm looking for the lightest /app/ possible :)
Your answer
