[issue18264] enum.IntEnum is not compatible with JSON serialisation
Eli Bendersky
report at bugs.python.org
Sat Aug 3 01:38:45 CEST 2013
Eli Bendersky added the comment:
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(...)).
----------
keywords: +patch
Added file: http://bugs.python.org/file31134/issue18264.1-prelim.patch
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue18264>
_______________________________________
More information about the Python-bugs-list
mailing list