Simplejson's Simple Gotcha
I like simple things. Anything more complex more than ‘simple’ is tough to deal with. Therefore it comes as no surprise that I love JSON. Heck, the entire grammar fits in a business card.
If you take a closer look at the spec above, you will notice that the keys for a JSON ‘dictionary’ should be strings. I have a feeling that programmers like me whose language-of-mass-destruction is python are likely to overlook this minor gotcha.
This is where it gets interesting. Python’s json module tries to do something smart when you try to encode a python dictionary into a JSON string. In this case, all the keys are python ints.
When I decode it back to a python object…
BAM! The json module silently converts all my integer keys to strings. I, for one, would have preferred an Exception to be raised instead.
Something like this, for example.
1 2 3 4 5 6
In this case, I get the desired behaviour where a ‘TypeError’ is raised. Stupid example, but you get the idea.
If I am missing something obvious here please do comment. This particular cheekiness of the json module caught me unawares recently. Or maybe I was wrong in expecting the decode operation to return something that is identical to the source.
Interestingly, the json support modules that ship with TurboGears error out if you try to return a dictionary having non-string keys from your controller methods.