class DtoSerializationHandler(object):
    def __init__(self, registry: Registry):
        self.deserializer = Deserializer(registry)
        self.serializer = Serializer()

    def deserialize(self, object, type):
        return self.deserializer.deserialize(object, type)

    def serialize(self, obj):
        return self.serializer.serialize(obj)
示例#2
0
class ObjectMapper(object):

    def __init__(self):
        self.deserializer = Deserializer(ObjectMapper.__RegistryStub())
        self.serializer = Serializer()

    def deserialize(self, json_str: str, clazz: Type[T]) -> T:
        dictionary = json.loads(json_str)
        return self.deserializer.deserialize(dictionary, clazz)

    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,)
示例#3
0
 def __init__(self, allow_unregistered_types: bool = False):
     self.deserializer = Deserializer(
         ObjectMapper.__RegistryStub() if allow_unregistered_types else Registry.INSTANCE)
     self.serializer = Serializer()
 def __init__(self, registry: Registry):
     self.deserializer = Deserializer(registry)
     self.serializer = Serializer()
示例#5
0
    def configure_service_locator(self):
        # Automatic binding of user-defined components
        AnnotatedClassBinder(self.root_package_name, self.registry,
                             Component).bind(self.serviceLocator)
        AnnotatedClassBinder(self.root_package_name, self.registry,
                             Resource).bind(self.serviceLocator)
        AnnotatedClassBinder(self.root_package_name, self.registry,
                             BackgroundWorker).bind(self.serviceLocator)
        AnnotatedClassBinder(self.root_package_name, self.registry,
                             Scheduled).bind(self.serviceLocator)
        AnnotatedClassBinder(self.root_package_name, self.registry,
                             Init).bind(self.serviceLocator)
        AnnotatedClassBinder(self.root_package_name, self.registry,
                             PreInit).bind(self.serviceLocator)
        AnnotatedClassBinder(self.root_package_name, self.registry,
                             PostInit).bind(self.serviceLocator)
        AnnotatedClassBinder(self.root_package_name, self.registry,
                             EventHandlerClass).bind(self.serviceLocator)
        AnnotatedClassBinder(self.root_package_name, self.registry,
                             RequestFilter).bind(self.serviceLocator)
        RunnableEventHandlerBinder(self.root_package_name,
                                   self.registry).bind(self.serviceLocator)
        ExceptionMapperBinder().bind(self.serviceLocator)
        ProviderBinder(self.root_package_name,
                       self.registry).bind(self.serviceLocator)

        # Component scope management
        for scope in [Singleton, BackgroundWorker]:
            scoped_classes = Stream(self.registry.get_annotated_in_package(scope, self.root_package_name)) \
                .map(lambda registration: registration.registered) \
                .toList()
            cache = SingletonScopeCache(str(scope), scoped_classes)
            self.serviceLocator.register_scope(cache)

        request_scope_cache = RequestScopeCache(
            Stream(
                self.registry.get_annotated_in_package(
                    RequestScoped, self.root_package_name)).map(
                        lambda registration: registration.registered).toList())
        self.serviceLocator.register_scope(request_scope_cache)

        # Jivago dependencies
        Stream(JIVAGO_DEFAULT_FILTERS).forEach(
            lambda f: self.serviceLocator.bind(f, f))
        self.serviceLocator.bind(Registry, Registry.INSTANCE)
        self.serviceLocator.bind(TaskScheduler,
                                 TaskScheduler(self.serviceLocator))
        self.serviceLocator.bind(Deserializer, Deserializer(Registry.INSTANCE))
        self.serviceLocator.bind(Serializer, Serializer())
        self.serviceLocator.bind(
            ViewTemplateRepository,
            ViewTemplateRepository(self.get_views_folder_path()))
        self.serviceLocator.bind(CorsHeadersInjectionFilter,
                                 CorsHeadersInjectionFilter)
        self.serviceLocator.bind(BodySerializationFilter,
                                 BodySerializationFilter)
        self.serviceLocator.bind(PartialContentHandler, PartialContentHandler)
        self.serviceLocator.bind(HttpStatusCodeResolver,
                                 HttpStatusCodeResolver)
        self.serviceLocator.bind(ObjectMapper, ObjectMapper)
        self.serviceLocator.bind(EventBus, self.create_event_bus())
        self.serviceLocator.bind(SynchronousEventBus,
                                 self.serviceLocator.get(EventBus))
        self.serviceLocator.bind(
            AsyncEventBus, AsyncEventBus(self.serviceLocator.get(EventBus)))
        self.serviceLocator.bind(RequestScopeCache, request_scope_cache)

        if not self.banner:
            self.serviceLocator.bind(BannerFilter, DummyBannerFilter)
示例#6
0
 def __init__(self):
     self.deserializer = Deserializer(ObjectMapper.__RegistryStub())
     self.serializer = Serializer()