Server : LiteSpeed System : Linux in-mum-web1112.main-hosting.eu 4.18.0-553.34.1.lve.el8.x86_64 #1 SMP Thu Jan 9 16:30:32 UTC 2025 x86_64 User : u451330669 ( 451330669) PHP Version : 8.2.27 Disable Function : NONE Directory : /opt/alt/python311/lib/python3.11/site-packages/jsons/deserializers/ |
from enum import EnumMeta
from typing import Optional
def default_enum_deserializer(obj: str,
cls: EnumMeta,
*,
use_enum_name: Optional[bool] = None,
**kwargs) -> object:
"""
Deserialize an enum value to an enum instance. The serialized value can be
either the name or the key of an enum entry. If ``use_enum_name`` is set to
``True``, then the value *must* be the key of the enum entry. If
``use_enum_name`` is set to ``False``, the value *must* be the value of the
enum entry. By default, this deserializer tries both.
:param obj: the serialized enum.
:param cls: the enum class.
:param use_enum_name: determines whether the name or the value of an enum
element should be used.
:param kwargs: not used.
:return: the corresponding enum element instance.
"""
if use_enum_name:
result = cls[obj]
elif use_enum_name is False:
result = cls(obj)
else: # use_enum_name is None
try:
result = cls[obj]
except KeyError:
result = cls(obj) # May raise a ValueError (which is expected).
return result