[Python-Dev] Enum is really serializable using json?

Nick Coghlan ncoghlan at gmail.com
Sun Feb 22 03:56:29 CET 2015


On 22 February 2015 at 05:10, Facundo Batista <facundobatista at gmail.com> wrote:
> On Sat, Feb 21, 2015 at 3:50 PM, Benjamin Peterson <benjamin at 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 at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list