[issue19837] Wire protocol encoding for the JSON module

Clay Gerrard report at bugs.python.org
Wed Mar 8 00:59:28 EST 2017


Clay Gerrard added the comment:

and for *encoding* case?  Can you just add the encoding argument back to json.dumps?  Have it default to None because of backwards compatibility in python3 and continue to return strings by default... 

... and then *everyone* that ever wants to *serialize* an object to json because they want to put it on a wire or w/e will hopefully someday learn when you call json.dumps you *always* set encoding='utf-8' and it will always return utf-8 encoded bytes (which is the same thing it would have done py2 regardless)?

Is it confusing for the py3 encoding argument to mean something different than py2?  Probably?  The encoding argument in py2 was there to tell the Encoder how to decode keys and values who's strings were acctually utf-8 encoded bytes.  But w/e py3 doesn't have that problem - so py3 can unambiguously hijack dumps' encoding param to mean bytes!  Then, sure, maybe the fact I can write:

    sock.send(json.dumps(obj, encoding='utf-8'))

... in either language is just a happy coincidence - but it'd be useful nevertheless.

Or I could be wrong.  I've not been thinking about this for 3 years.  But I have bumped into this a couple of times in the years since starting to dream of python 3.2^H4^H5^H6^H7 support - but until then I do seem to frequently forget json.dumps(obj).decode('utf-8') so maybe my suggestion isn't really any better!?

----------
nosy: +Clay Gerrard

_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue19837>
_______________________________________


More information about the Python-bugs-list mailing list