New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
json.dumps has different behaviour if encoding='utf-8' or encoding='utf8' #77436
Comments
Hey I'm new here, so please let me know what incorrect things I am doing! I think
The first https://github.com/python/cpython/blob/2.7/Lib/json/encoder.py#L198 This will decode any str if the encoding is not 'utf-8'. In the mixed case (unicode and str) this will blow. I workaround is to use any of the aliases for 'utf-8' like 'utf8' or 'u8'. I would be crazy happy to provide a PR if this is really an issue. |
Treating 'utf-8' and its aliases differently (when they specifically mean the Python's, rather than something else's, encoding) is definitely as issue. You shouldn't hardcode a list of aliases though; rather use existing facilities to resolve them. From quick googling, e.g. Make sure to follow https://devguide.python.org/pullrequest when doing the PR; a test case will likely be needed, too. |
In simplejson: >>> simplejson.dumps({u"greeting": "hi", "currency": "€"}, ensure_ascii=False, encoding="utf8")
u'{"currency": "\u20ac", "greeting": "hi"}'
>>> simplejson.dumps({u"greeting": "hi", "currency": "€"}, ensure_ascii=False)
u'{"currency": "\u20ac", "greeting": "hi"}' I think it makes sense to fix the case for "utf-8". |
Hi Sehriy, I am ok with that change. I think it makes much more sense, but I also think it will break people's codes. At least with the simplest fix in which: >>> json.dumps({"g"}, ensure_ascii=False)
u'"g"' Which is again compatible with simplejson. |
You could decode only non-ascii strings. But I'm not sure that it is worth to change something in 2.7. This could be treated aa a new feature. Left this on to Benjamin, the release manager of 2.7. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: