Skip to content

source_bundle_serializer.py

ofrak.service.serialization.serializers.source_bundle_serializer

SourceBundleSerializer (SerializerInterface)

Serialize and deserialize Dict[X, Y] into PJSONType.

Implementation: a list of tuples (key, value), because the serialized keys may not be hashable (e.g. if they're themselves serialized as a dictionary), despite the real keys being hashable.

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/source_bundle_serializer.py
def obj_to_pjson(self, obj: SourceBundle, type_hint: Any) -> SerializedDictType:
    return [
        (self._service.to_pjson(key, str), self._serialize_value(value))
        for key, value in obj.items()
    ]

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/source_bundle_serializer.py
def pjson_to_obj(self, pjson_obj: SerializedDictType, type_hint: Any) -> SourceBundle:
    return SourceBundle(
        [
            (self._service.from_pjson(key, str), self._deserialize_value(value))
            for key, value in pjson_obj
        ]
    )