[Python-porting] json encoding

Clay Gerrard clay.gerrard at gmail.com
Fri Jun 19 23:41:59 CEST 2015
Has anyone ported any projects that use json as a data interchange format -
or can think of any that probably had to port some json handling code?

I'm perplexed that python3's json.dumps returns a str.  I mean I know it's
dump*s* - but that was from back when we only had binary strings ;)

My reading leads me to believe that the json format is not a "string" - its
a binary format - encoded in utf-8 [1]

Why would want to first create a unicode string - then encode it to utf-8?
What else are you doing with this this data exchange format if not using it
to exchange the representation of the data encoded as bytes to somewhere
else?

    json_data = json.dumps(my_object)
    if not isinstance(json_data, six.bytes_type):
        json_data = json_data.encode('utf-8')
    my_buffer = BytesIO(json_data)

^ Is that really the best I can do?

Why did json.dumps in python3 [3] loose the encoding kwarg from python2 [2].

Thanks for any guidance or suggestion - maybe I'm just thinking about it
wrong.

-Clay

1. https://tools.ietf.org/html/rfc7159#section-8.1
3. https://docs.python.org/3.2/library/json.html#json.dumps
2. https://docs.python.org/2/library/json.html#json.dumps
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-porting/attachments/20150619/96b55332/attachment.html>


More information about the Python-porting mailing list