If I write
import json
class Color():
FUSCHIA = 0x00
TURQUOISE = 0x01
EMERALD = 0x02
def my_default_serializer(o):
return o.__dict__
def get_colors():
return json.dumps(
{
'dark_bg': True,
'colors_batch': [Color.TURQUOISE,
Color.EMERALD]
},
default=my_default_serializer
)
print(get_colors())
then I get, as expected:
{"dark_bg": true, "colors_batch": [1, 2]}
Now suppose I modernize this code by introducing enum.Enum. Among other things, this is handy because Enum will parse a string (read from a file, for example), and hence I replace the code above with:
import json
from enum import Enum
class Color(Enum):
FUSCHIA = 0x00
TURQUOISE = 0x01
EMERALD = 0x02
def my_default_serializer(o):
return o.__dict__
def get_colors():
return json.dumps(
{
'dark_bg': True,
'colors_batch': [Color['TURQUOISE'],
Color['EMERALD']]
},
default=my_default_serializer
)
print(get_colors())
This introduces the error:
AttributeError: 'mappingproxy' object has no attribute '__dict__'
(and if I don't specify the default serializer, I get TypeError: Object of type 'Color' is not JSON serializable ).
How should I JSON-serialize Enum-children?