[Python-Dev] Dropping bytes "support" in json

Daniel Stutzbach daniel at stutzbachenterprises.com
Mon Apr 13 20:42:41 CEST 2009

On Mon, Apr 13, 2009 at 12:19 PM, "Martin v. Löwis" <martin at v.loewis.de>wrote:

> > I use the json module in 2.6 to communicate with a C# JSON library and a
> > JavaScript JSON library.  The C# and JavaScript libraries produce and
> > consume the equivalent of str, not the equivalent of bytes.
> I assume there is a TCP connection between the json module and the
> C#/JavaScript libraries?

Yes, there's a TCP connection.  Sorry for not making that clear to begin

I also sometimes store JSON objects in a database.  In that case, I pass
strings to the database API which stores them in a TEXT field.  Obviously
somewhere they get encoding to bytes, but that's handled by the database.

> If so, it doesn't matter what representation these implementations chose
> to use.

True, I can always convert from bytes to str or vise versa.  Sometimes it is
illustrative to see how others have chosen to solve the same problem.  The
JSON specification and other implementations serializes an object to a
string.  Python's json.dumps() needs to either return a str or let the user
specify an encoding.

At least one of these two needs to work:

json.dumps({}).encode('utf-16le')  # dumps() returns str

json.dumps({}, encoding='utf-16le')  # dumps() returns bytes

In 2.6, the first one works.  The second incorrectly returns '{}'.

Daniel Stutzbach, Ph.D.
President, Stutzbach Enterprises, LLC <http://stutzbachenterprises.com>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-dev/attachments/20090413/1eac0792/attachment.htm>

More information about the Python-Dev mailing list