Enum is really serializable using json?
We have this issue closed as resolved: http://bugs.python.org/issue18264 It's called "enum.IntEnum is not compatible with JSON serialisation", and it looks that after a long conversation they added proper support for it in Py3.4. However, this is still failing: Python 3.4.2 (default, Oct 8 2014, 13:18:07) [GCC 4.9.1] on linux
import enum, json Foo = enum.Enum('Foo', 'bar baz') json.dumps(Foo.bar) Traceback (most recent call last): ... TypeError: <Foo.bar: 1> is not JSON serializable
Am I failing to understand the bug tracker saying the patch was applied for Py3.4? (thought it was more polite to ask here than to reopen the whole thread in the issue tracker) Thanks! -- . Facundo Blog: http://www.taniquetil.com.ar/plog/ PyAr: http://www.python.org/ar/ Twitter: @facundobatista
On Sat, Feb 21, 2015, at 13:48, Facundo Batista wrote:
We have this issue closed as resolved:
http://bugs.python.org/issue18264
It's called "enum.IntEnum is not compatible with JSON serialisation", and it looks that after a long conversation they added proper support for it in Py3.4.
However, this is still failing:
Python 3.4.2 (default, Oct 8 2014, 13:18:07) [GCC 4.9.1] on linux
import enum, json Foo = enum.Enum('Foo', 'bar baz') json.dumps(Foo.bar) Traceback (most recent call last): ... TypeError: <Foo.bar: 1> is not JSON serializable
Am I failing to understand the bug tracker saying the patch was applied for Py3.4?
As the issue title suggests, IntEnum but not Enum supports JSON serialization.
On Sat, Feb 21, 2015 at 3:50 PM, Benjamin Peterson <benjamin@python.org> wrote:
Am I failing to understand the bug tracker saying the patch was applied for Py3.4?
As the issue title suggests, IntEnum but not Enum supports JSON serialization.
Arghhh.. stupid of me. Sorry for the noise, and thanks! -- . Facundo Blog: http://www.taniquetil.com.ar/plog/ PyAr: http://www.python.org/ar/ Twitter: @facundobatista
On 22 February 2015 at 05:10, Facundo Batista <facundobatista@gmail.com> wrote:
On Sat, Feb 21, 2015 at 3:50 PM, Benjamin Peterson <benjamin@python.org> wrote:
Am I failing to understand the bug tracker saying the patch was applied for Py3.4?
As the issue title suggests, IntEnum but not Enum supports JSON serialization.
Arghhh.. stupid of me. Sorry for the noise, and thanks!
I don't know if we actually clearly explain anywhere why that's the case, though. As I see it, the core problem is that when mapping an Enum to JSON, you may either do it by name (so it becomes a string on the JSON side) or by value (so you lose the fact that it's an enum, but can work with protocols that use integers rather than strings). There's no "right" answer in general as to which of those is correct - the one you want will depend on exactly what protocols you're working with. By contrast, for IntEnum, we want that to be an as-close-to-drop-in-as-is-feasible replacement for existing ints, so there's a clear correct answer: we need to serialise it to JSON as an integer, and lose the additional enum details. Regards, Nick. -- Nick Coghlan | ncoghlan@gmail.com | Brisbane, Australia
participants (3)
-
Benjamin Peterson
-
Facundo Batista
-
Nick Coghlan