Skip to content

enum_serializer.py

ofrak.service.serialization.serializers.enum_serializer

EnumSerializer (SerializerInterface)

Serialize and deserialize enum.Enum instances into PJSONType.

Implementation: the class is serialized, along with the name of the attribute for this instance, into a string.

obj_to_pjson(self, enum_instance, _type_hint)

Serialize the object to PJSON. Note that the generic self._service.to_pjson can be used here.

Source code in ofrak/service/serialization/serializers/enum_serializer.py
def obj_to_pjson(self, enum_instance: Enum, _type_hint: Any) -> str:
    return f"{self._service.to_pjson(enum_instance.__class__, Type)}.{enum_instance.name}"

pjson_to_obj(self, pjson_obj, _type_hint)

Deserialize PJSON into the object. Note that the generic self._service.from_pjson can be used here.

Source code in ofrak/service/serialization/serializers/enum_serializer.py
def pjson_to_obj(self, pjson_obj: str, _type_hint: Any) -> Enum:
    enum_cls_ref_pjson, enum_member_name = pjson_obj.rsplit(".", maxsplit=1)
    enum_cls = self._service.from_pjson(enum_cls_ref_pjson, Type)
    return enum_cls[enum_member_name]