Google App Engine Python simplejson escaping?
The version of django.utils.simplejson on GAE is for example escaping "/" characters, but not "\n" when doing js = json.dumps(my_dict_w_strings_w_newline_and_slash)
which is causing problems when I try to json.loads(js)
in my client someplace else.
Any suggestions on how to sort out a solution? The strings a开发者_开发百科re base64 encoded data which get ruined by this.
I tried the simplejson version shipped with the SDK (Django 0.96 and 1.2) and both escape '\n':
>>> simplejson.dumps({'foo': '\n'})
'{"foo": "\\n"}'
And on http://shell.appspot.com/:
Google App Engine/1.5.1
Python 2.5.2 (r252:60911, Mar 17 2011, 15:16:30)
[GCC 4.3.1]
>>> from django.utils import simplejson
>>> simplejson.dumps({'foo': '\n'})
'{"foo": "\\n"}'
>>> simplejson.dumps('foo/bar')
'"foo\\/bar"'
My colleague has suggested:
if json.encoder.ESCAPE_DCT.get('/') != '/':
json.encoder.ESCAPE_DCT['/'] = '/'
which is working nicely.
精彩评论