Author eli.bendersky
Recipients amaury.forgeotdarc, barry, cvrebert, eli.bendersky, eric.snow, ethan.furman, ezio.melotti, giampaolo.rodola, gvanrossum, ncoghlan, pitrou, rhettinger
Date 2013-08-02.23:38:44
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1375486725.21.0.5394765724.issue18264@psf.upfronthosting.co.za>
In-reply-to
Content
I've been reading the discussion again to figure out what we need to move forward; it appears that a simple approach of using str(int(obj)) in json encoding when isinstance(obj, int) was acceptable for many of the participants, and it helps solve the most common problem - the one with IntEnum. We can solve it now, move on to converting socket.* and other constants to IntEnum and perhaps change the solution to something more grandiose later.

I'm attaching a proof-of-concept (probably incomplete) patch that implements this for the Python and C implementations of json.

With it applied, Nick's example goes like this:

>>> from enum import IntEnum
>>> import json
>>> class Example(IntEnum): x = 1
... 
>>> json.dumps(Example.x)
'1'
>>> json.loads(json.dumps(Example.x))
1
>>> 

When thinking about the more generic approaches, we must consider what was raised here already - JSON interoperates with other languages, for which Python's enum is meaningless (think of JavaScript, for instance). So the only logical way to JSON-encode a IntEnum is str(int(...)).
History
Date User Action Args
2013-08-02 23:38:45eli.benderskysetrecipients: + eli.bendersky, gvanrossum, barry, rhettinger, amaury.forgeotdarc, ncoghlan, pitrou, giampaolo.rodola, ezio.melotti, cvrebert, ethan.furman, eric.snow
2013-08-02 23:38:45eli.benderskysetmessageid: <1375486725.21.0.5394765724.issue18264@psf.upfronthosting.co.za>
2013-08-02 23:38:45eli.benderskylinkissue18264 messages
2013-08-02 23:38:44eli.benderskycreate