Source code for jivago.serialization.object_mapper

import json
from typing import Type, TypeVar

from jivago.lang.annotations import Override, Serializable
from jivago.lang.registry import Registry
from jivago.serialization.deserializer import Deserializer
from jivago.serialization.serializer import Serializer

BUILTIN_TYPES = (str, float, int)

T = TypeVar('T')


[docs] class ObjectMapper(object): def __init__(self, allow_unregistered_types: bool = False): self.deserializer = Deserializer( ObjectMapper.__RegistryStub() if allow_unregistered_types else Registry.INSTANCE) self.serializer = Serializer()
[docs] def deserialize(self, json_str: str, clazz: Type[T]) -> T: dictionary = json.loads(json_str) return self.deserializer.deserialize(dictionary, clazz)
[docs] def serialize(self, serializable: object) -> str: dictionary = self.serializer.serialize(serializable) return json.dumps(dictionary)
class __RegistryStub(Registry): @Override def is_annotated(self, object: object, annotation: "Annotation"): return annotation == Serializable and object not in BUILTIN_TYPES + (dict, list, tuple)