Trying to serialize a dict object into a json string using Python 2.7’s json (ie: import json).
Example:
json.dumps({
'property1': 'A normal string',
'pickled_property': \u0002]qu0000U\u0012
})
The object has some byte strings in it that are “pickled” data using cPickle, so for json’s purposes, they are basically random byte strings. I was using django.utils’s simplejson and this worked fine. But I recently switched to Python 2.7 on google app engine and they don’t seem to have simplejson available anymore.
Now that I am using json, it throws an exception when it encounters bytes that aren’t part of UTF-8. The error that I’m getting is:
UnicodeDecodeError: 'utf8' codec can't decode byte 0x80 in position 0: invalid start byte
It would be nice if it printed out a string of the character codes like the debugging might do, ie: \u0002]q\u0000U\u001201. But I really don’t much care how it handles this data just as long as it doesn’t throw an exception and continues serializing the information that it does recognize.
How can I make this happen?
Thanks!
The JSON spec defines strings in terms of unicode characters. For this reason, the
jsonmodule assumes that anystrinstance it receives holds encoded unicode text. It will try UTF-8 as its default encoding, which causes trouble when you have a string like the output ofpickle.dumpswhich may not be a valid UTF-8 sequence.Fortunately, fixing the issue is easy. You simply need to tell the
json.dumpsfunction what encoding to use instead of UTF-8. The following will work, even thoughmy_bytestringis not valid UTF-8 text:I believe that any 8-bit encoding will work in place of the
latin-1used here (just be sure to use the same one for decoding later).When you want to unpickle the JSON encoded data, you’ll need to make a call to
unicode.decode, sincejson.loadsalways returns encoded strings asunicodeinstances. So, to get themy_datalist back out ofjson_dataabove, you’d need this code: