Serialising an Enum member to JSON

后端 未结 7 1925
借酒劲吻你
借酒劲吻你 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:25

    If you want to encode an arbitrary enum.Enum member to JSON and then decode it as the same enum member (rather than simply the enum member's value attribute), you can do so by writing a custom JSONEncoder class, and a decoding function to pass as the object_hook argument to json.load() or json.loads():

    PUBLIC_ENUMS = {
        'Status': Status,
        # ...
    }
    
    class EnumEncoder(json.JSONEncoder):
        def default(self, obj):
            if type(obj) in PUBLIC_ENUMS.values():
                return {"__enum__": str(obj)}
            return json.JSONEncoder.default(self, obj)
    
    def as_enum(d):
        if "__enum__" in d:
            name, member = d["__enum__"].split(".")
            return getattr(PUBLIC_ENUMS[name], member)
        else:
            return d
    

    The as_enum function relies on the JSON having been encoded using EnumEncoder, or something which behaves identically to it.

    The restriction to members of PUBLIC_ENUMS is necessary to avoid a maliciously crafted text being used to, for example, trick calling code into saving private information (e.g. a secret key used by the application) to an unrelated database field, from where it could then be exposed (see http://chat.stackoverflow.com/transcript/message/35999686#35999686).

    Example usage:

    >>> data = {
    ...     "action": "frobnicate",
    ...     "status": Status.success
    ... }
    >>> text = json.dumps(data, cls=EnumEncoder)
    >>> text
    '{"status": {"__enum__": "Status.success"}, "action": "frobnicate"}'
    >>> json.loads(text, object_hook=as_enum)
    {'status': , 'action': 'frobnicate'}
    

提交回复
热议问题