我有一个字典,其中一些键是枚举实例(enum.Enum的子类)。我试图按照documentation使用自定义JSON编码器类将字典编码为JSON字符串。我想要的只是将输出的JSON中的键作为Enum名称的字符串。例如,{ TestEnum.one : somevalue }
将被编码为{ "one" : somevalue }
。将Python枚举编码为JSON
我已经写一个简单的测试的情况下,如下所示,这是我在一个干净的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!"}
# Test dumps with Encoder method
#print("Test with encoder method:")
#result = json.dumps(test, default=encode_enum)
#print(result)
# Test dumps with Encoder Class
print("Test with encoder class:")
result = json.dumps(test, cls=TestEncoder)
print(result)
我不能成功地进行编码可以在字典(使用Python 3.6.1)。我不断得到TypeError: keys must be a string
错误,并且我的自定义编码器实例(通过json.dumps
方法的cls
参数提供)的默认方法似乎从未被调用?我也尝试通过json.dumps
方法的default
参数提供自定义编码方法,但这种方法从未被触发。
我见过涉及IntEnum类的解决方案,但我需要Enum的值为字符串。我也看到了this answer,它讨论了与从另一个类继承的Enum相关的问题。但是,我的枚举仅继承自enum.Enum类,并正确地响应isinstance
调用?
提供给json.dumps
方法时,自定义类和方法都生成TypeError
。典型的输出如下所示:
$ 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
我相信这个问题是,JSONEncoder类的encode
方法假定它知道如何连载枚举类(因为iterencode
方法的if语句中的一个被触发)所以永远不要调用自定义的默认方法,并且无法序列化Enum?
任何帮助将不胜感激!
相关:http://stackoverflow.com/a/43730306/674039 – wim
您不能使用字符串以外的任何其他键。编码器无法在此处进行调解,这是一条硬性规则。 –