Is it possible to dump an enum in json without passing an encoder to json.dumps()?
Sadly, there is no direct support for Enum
in JSON.
The closest automatic support is to use IntEnum
(which enum34
also supports), and then json
will treat your enum
s as int
s; of course, decoding them will give you an int
back, but that is as good it gets without specifying your encoder/decoder.
Just adding method(s) to the FooBarType
enum won't do what you want.
As I mentioned in my comment, you can however use part of my answer to the question Making object JSON serializable with regular encoder to monkey-patch the json
module so it will return the name (or value) of Enum
members. I'm assuming you're using the enums34
module by Ethan Furman et al, which was backported to Python 2.7 since that version doesn't come with it built-in — it became part of the standard library in Python 3.4.
Note this will work even though you can't change the line where the json.dumps()
call occurs as long as that happens after the patch is applied. This is because Python normally caches import
ed modules in sys.modules
, i.e. they aren't reloaded everytime they are used in separate scripts — so any changes made this to them are "sticky" and remain in effect.
So for what you want to do, first create your own module to make the patch. For example: make_enum_json_serializable.py
.
""" Module that monkey-patches the json module when it's imported so
JSONEncoder.default() automatically checks to see if the object being encoded
is an instance of an Enum type and, if so, returns its name.
"""
from enum import Enum
from json import JSONEncoder
_saved_default = JSONEncoder().default # Save default method.
def _new_default(self, obj):
if isinstance(obj, Enum):
return obj.name # Could also be obj.value
else:
return _saved_default
JSONEncoder.default = _new_default # Set new default method.
Then, in your own script, all you need to do is essentially add one line:
from enum import Enum
import json
import make_enum_json_serializable # ADDED
class FooBarType(Enum):
standard = 0
foo = 1
bar = 2
a_dict = {'name': 'spam', 'value': 42, 'type': FooBarType.foo}
print(json.dumps(a_dict))
Output:
{"type": "foo", "name": "spam", "value": 42}
Try:
from enum import Enum
# class StrEnum(str, Enum):
# """Enum where members are also (and must be) strs"""
class Color(str, Enum):
RED = 'red'
GREEN = 'green'
BLUE = 'blue'
data = [
{
'name': 'car',
'color': Color.RED,
},
{
'name': 'dog',
'color': Color.BLUE,
},
]
import json
print(json.dumps(data))
Result:
[
{
"name": "car",
"color": "red"
},
{
"name": "dog",
"color": "blue"
}
]