(Correction for the uneditable subject: Broken in 4.1 but not 3.2.1!)
I’m developing a Unity3D app on the iPhone, that reads and writes a lot of JSON.
So I tried using the LitJson library, and it worked fine in the iPhone emulator, but not on the iPad or iPhone 3GS.
I wasn’t able to figure out what was wrong. Essentially it was parsing in JSON dictionaries without syntax errors, but no keys were showing up in them, or some keys were missing sometimes.
So then I switched to the JsonFx.Json library, hoping I’d have better luck.
That kinda solved the problem, but only on the iPad (3.2.1)!
Still has the same incorrect behavior on the iPhone 3GS (4.1), of parsing in empty dictionaries, or sometimes simply missing some but not all of the dictionary keys, never issuing a syntax error though.
So I puts lots and lots of debug statements in, worked all night, and finally isolated the problem.
Sure enough when it was setting keys in the dictionary (when the json parser parsed out a key and a value, it would set the resulting dict key), some times the key did not get set!
I even wrote debug log loops to dump out the size of the dict and all the keys and values before and after the set, to see what was going on.
First my “handle” key gets set, so I go from a 0 length Dictionary<String, object> to a 1 length one with the handle key, correctly.
Then subsequent sets of different keys have no effect on the dictionary at all!
It just stays with one key “handle” and never adds any more keys.
Depending on the JSON dict it’s parsing, sometimes no keys get set at all!
So I’m sure there’s something going wrong under certain conditions with setting keys in Dictionary<String, object> on my 4.1 iPhone, but it works on my 3.2.1 iPad.
I tried reproducing the same problem with just setting the same keys and values in a Dictionary<String, object> I created myself, but it worked ok in that simple test.
So I decided not to trust generics, and to get rid of the generic dictionary and use an old fashioned Hashtable instead:
I replaced the Json parser’s “new Dictionary<String, object>()” with a less generic but lower tech “new Hashtable()”, and now the code works correctly, parsing my json dictionaries without missing any keys!!!
Does anyone know if the iOS platform’s handing of generics and/or reflection (which the JSON library also uses) is not fully implemented, or has some rough edges?
What should I look out for?
Is there a good C# Json library that is known to work well on the iOS Unity platform, including both 3.2.1 and 4.1? (And likely to continue working across future releases! What could Apple have done that broken C#'s Dictionary<String, object> from release 3.2.1 to 4.1??!)
I have patched JsonFx to make it work for my test cases, but it is still doing a bunch of other stuff with generic dictionaries that I don’t trust and don’t completely understand yet, so I’d like to know if is going to just start flaking out on future iOS releases and platforms I haven’t tested it on, even if it seems to work for me now (with this hacky work-around) on my devices.
It is really really weird that the same code runs on the iPad but does not work on the iPhone.
The iPhone is running 4.1 (8B117), and the iPad is running 3.2.1 (7B405).
Perhaps the iOS 4.x upgrade broke something about generics in the iPhone Mono runtime?
I would like to get to the bottom of this peculiar behavior before upgrading the iPad to 4.1, since this may cause problems with other users’ iPads.
-Don