Skip to content

range_serializer.py

ofrak.service.serialization.serializers.range_serializer

RangeSerializer (SerializerInterface)

Serialize and deserialize Range into PJSONType.

Implementation: a Range is serialized as a 2-tuple (start, end).

The default class instance serializer would work, however the space overhead would be significant and Range is used frequently enough in OFRAK to justify this optimization.

obj_to_pjson(self, obj, _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/range_serializer.py
def obj_to_pjson(self, obj: Range, _type_hint: Any) -> Tuple[int, int]:
    return (obj.start, obj.end)

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/range_serializer.py
def pjson_to_obj(self, pjson_obj: Tuple[int, int], _type_hint: Any) -> Range:
    return Range(pjson_obj[0], pjson_obj[1])