On Jun 11, 10:51 pm, Luke Plant <l.plant...@cantab.net> wrote:
> We've switched internally from json to simplejson. Our 1.5 release notes > say: Do you mean the other way around? > You can safely change any use of django.utils.simplejson to json > > I just found a very big difference between json and simplejson > > >>> simplejson.loads('{"x":"y"}') > > {'x': 'y'} > > >>> json.loads('{"x":"y"}') > > {u'x': u'y'} > > i.e. simplejson returns bytestrings if the string is ASCII (it returns > unicode objects otherwise), while json returns unicode objects always. > > This was, unfortunately, a very unfortunate design decision on the part > of simplejson - json is definitely correct here - and a very big change > in semantics. It led to one very difficult to debug error for me already. Right. And on Python 3, the json module (correctly) doesn't accept byte-strings at all. > So, this is a shout out to other people to watch out for this, and a > call for ideas on what we can do to mitigate the impact of this. It's > likely to crop up in all kinds of horrible places, deep in libraries > that you can't do much about. In my case I was loading config, including > passwords, from a config file in JSON, and the password was now > exploding inside smtplib because it was a unicode object. This is one place where there are limitations in the 2.x stdlib - other places include cStringIO and cookies. For example, if you pass a Unicode object to a cStringIO.StringIO, it doesn't complain, but does the wrong thing: >>> from cStringIO import StringIO; StringIO(u'abc').getvalue() 'a\x00b\x00c\x00' >>> Fun and games ... I'm not sure there's any easy way out, other than comprehensive testing. Regards, Vinay Sajip -- You received this message because you are subscribed to the Google Groups "Django developers" group. To post to this group, send email to django-developers@googlegroups.com. To unsubscribe from this group, send email to django-developers+unsubscr...@googlegroups.com. For more options, visit this group at http://groups.google.com/group/django-developers?hl=en.