开发者

Google App Engine Python simplejson escaping?

开发者 https://www.devze.com 2023-03-14 22:30 出处:网络
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

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号