I have a dictionary where some of the keys are instances of Enum (subclasses of enum.Enum). I am trying to encode a dictionary into a JSON string using the custom JSON Encoder class in accordance with the documentation . All I want is to make the keys in the outputted JSON be Enum name strings. For example, { TestEnum.one : somevalue }will be encoded to { "one" : somevalue }.
I wrote a simple test case, shown below, which I tested in pure virtualenv:
import json
from enum import Enum
class TestEnum(Enum):
one = "first"
two = "second"
three = "third"
class TestEncoder(json.JSONEncoder):
""" Custom encoder class """
def default(self, obj):
print("Default method called!")
if isinstance(obj, TestEnum):
print("Seen TestEnum!")
return obj.name
return json.JSONEncoder.default(self, obj)
def encode_enum(obj):
""" Custom encoder method """
if isinstance(obj, TestEnum):
return obj.name
else:
raise TypeError("Don't know how to decode this")
if __name__ == "__main__":
test = {TestEnum.one : "This",
TestEnum.two : "should",
TestEnum.three : "work!"}
print("Test with encoder class:")
result = json.dumps(test, cls=TestEncoder)
print(result)
( Python 3.6.1). TypeError: keys must be a string, ( cls json.dumps), , ? default json.dumps, .
, IntEnum, , Enum . , , Enum, . enum.Enum isinstance?
, TypeError, json.dumps. :
$ python3 enum_test.py
Test with encoder class
Traceback (most recent call last):
File "enum_test.py", line 59, in <module>
result = json.dumps(test, cls=TestEncoder)
File "/usr/lib64/python3.6/json/__init__.py", line 238, in dumps
**kw).encode(obj)
File "/usr/lib64/python3.6/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/usr/lib64/python3.6/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
TypeError: keys must be a string
, , encode JSONEncoder , , Enum ( if iterencode), Enum?
!