Is it possible to dump an enum in json without passing an encoder to json.dumps()? Is it possible to dump an enum in json without passing an encoder to json.dumps()? json json

Is it possible to dump an enum in json without passing an encoder to json.dumps()?


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 jsonprint(json.dumps(data))

Result:

[    {        "name": "car",        "color": "red"    },    {        "name": "dog",        "color": "blue"    }]


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 enums as ints; 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 imported 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 soJSONEncoder.default() automatically checks to see if the object being encodedis an instance of an Enum type and, if so, returns its name."""from enum import Enumfrom 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_defaultJSONEncoder.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 Enumimport jsonimport make_enum_json_serializable  # ADDEDclass FooBarType(Enum):    standard = 0    foo = 1    bar = 2a_dict = {'name': 'spam', 'value': 42, 'type': FooBarType.foo}print(json.dumps(a_dict))

Output:

{"type": "foo", "name": "spam", "value": 42}