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

Daniel Stutzbach daniel at stutzbachenterprises.com
Mon Apr 13 23:25:28 CEST 2009

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

> > True, I can always convert from bytes to str or vise versa.
> I think you are missing the point. It will not be necessary to convert.

Sometimes I want bytes and sometimes I want str.  I am going to be
converting some of the time. ;-)

Below is a basic CGI application that assumes that json module works with
str, not bytes.  How would you write it if the json module does not support
returning a str?

print("Content-Type: application/json; charset=utf-8")
input_object = json.loads(sys.stdin.read())
output_object = do_some_work(input_object)

The questions is: which of them is more appropriate, if, what you want,
> is bytes. I argue that the second form is better, since it saves you
> an encode invocation.

If what you want is bytes, encoding has to happen somewhere.  If the json
module has some optimizations to do the encoding at the same time as the
serialization, great.  However, based on the original post of this thread,
it sounds like that code doesn't exist or doesn't work correctly.

What's the benefit of preventing users from getting a str out if that's what
they want?

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/7b9ab738/attachment.htm>

More information about the Python-Dev mailing list