Serialising an Enum member to JSON

后端 未结 7 1866
借酒劲吻你
借酒劲吻你 2020-12-13 11:52

How do I serialise a Python Enum member to JSON, so that I can deserialise the resulting JSON back into a Python object?

For example, this code:

相关标签:
7条回答
  • 2020-12-13 12:32

    This worked for me:

    class Status(Enum):
        success = 0
    
        def __json__(self):
            return self.value
    

    Didn't have to change anything else. Obviously, you'll only get the value out of this and will need to do some other work if you want to convert the serialized value back into the enum later.

    0 讨论(0)
提交回复
热议问题