Esempio n. 1
0
    def serialize(self, obj: Any) -> Any:
        if isinstance(obj, dict):
            r = {}
            for k, v in obj.items():
                with subserializer('$key'):
                    k = self.serialize(k)
                assert isinstance(k, str), k

                with subserializer('$value'):
                    v = self.serialize(v)

                r[k] = v
            return r
        elif isinstance(obj, list):
            r = []

            for i, v in enumerate(obj):
                with subserializer(i):
                    r.append(self.serialize(v))
            return r
        elif isinstance(obj, str):
            return obj
        elif isinstance(obj, float):
            return obj
        elif isinstance(obj, int):
            return obj
        elif obj is None:
            return obj
        else:
            raise SerializationError(val=obj, reason='not_json_value')
Esempio n. 2
0
    def serialize(self, obj: Any) -> Any:
        with subserializer('$value'):
            obj = self.dependencies['value'].serialize(obj)

        with subserializer('$child'):
            r = self.dependencies['child'].serialize(obj)

        return r
Esempio n. 3
0
    def serialize(self, obj: Any) -> Any:
        r = {}

        for k, v in obj.items():
            with subserializer('$key'):
                k = self.dependencies['key'].serialize(k)

            with subserializer('$value'):
                v = self.dependencies['value'].serialize(v)

            r[k] = v
        return r
Esempio n. 4
0
    def serialize(self, obj: Any) -> FieldValue:
        v = self.dependencies['item']

        with subserializer(self.name):
            obj = v.serialize(obj)

        return FieldValue(self.name, obj)
Esempio n. 5
0
    def serialize(self, obj: Any) -> Any:
        r = b''
        for x in range(len(self.dependencies)):
            with subserializer(str(x)):
                r += self.dependencies[str(x)].serialize(obj)

        return r
Esempio n. 6
0
    def serialize(self, obj: Any) -> Any:
        with subserializer('$discriminant'):
            discriminant = self.dependencies['discriminant'].serialize(obj)

        if discriminant not in self.items:
            raise SerializationError(val=obj, path=['$value'], origin=self,
                                     reason=f'`{discriminant}` is not in the map')

        with subserializer('$value'):
            value = self.dependencies['value'].serialize(obj)

        depk = self.items[discriminant]
        dep = self.dependencies[depk]

        with subserializer('$sub'):
            return dep.serialize(value)
Esempio n. 7
0
    def serialize(self, obj: Any) -> Any:
        with subserializer('$body'):
            val = self.dependencies['body'].serialize(obj)

        try:
            return json.loads(bytes(val))
        except Exception as e:
            raise SerializationError(val=obj, reason='json', exc=e, origin=self)
Esempio n. 8
0
    def serialize(self, obj: Tuple[int, bytes]) -> BinaryNext:
        with subserializer('$size'):
            size = self.dependencies['size'].serialize(obj)

        with subserializer('$body'):
            body = self.dependencies['body'].serialize(obj)

        if not isinstance(size, int):
            raise SerializationError(val=obj, reason='not_int', origin=self)

        if not isinstance(body, bytes) and not isinstance(body, memoryview):
            raise SerializationError(val=obj, reason='not_bytes', origin=self)

        if len(body) < size:
            raise SerializationError(val=obj, reason=BUFFER_NEEDED, origin=self)

        return BinaryNext(body[:size], body[size:])
Esempio n. 9
0
    def serialize(self, obj: Any) -> Any:
        with subserializer('$value'):
            obj2 = self.dependencies['value'].serialize(obj)

        if obj2 not in self.lookup:
            raise SerializationError(val=obj, exc=KeyError(obj2), reason='key_missing')

        return self.lookup[obj2]
Esempio n. 10
0
    def serialize(self, obj: Any) -> Any:
        if not hasattr(obj, '__getitem__'):
            raise SerializationError(val=obj, reason='invalid_obj', origin=self)

        try:
            r = obj[self.name]
        except KeyError as e:
            raise SerializationError(val=obj, reason='key_missing', exc=e, origin=self)

        with subserializer('$item'):
            return self.dependencies['type'].serialize(r)
Esempio n. 11
0
    def serialize(self, obj: Any) -> Any:
        try:
            r = getattr(obj, self.name)
        except AttributeError as e:
            raise SerializationError(val=obj, reason=f'attr_missing', origin=self, exc=e)

        if len(self.dependencies):
            with subserializer('$attr'):
                return self.dependencies['type'].serialize(r)
        else:
            return r
Esempio n. 12
0
    def serialize(self, obj: Any) -> Any:
        with subserializer('$value'):
            obj2 = self.dependencies['value'].serialize(obj)

        try:
            return self.enum(obj2)
        except Exception as e:
            raise SerializationError(path=['$enum'],
                                     val=obj,
                                     exc=e,
                                     reason=f'invalid_enum_key',
                                     origin=self)
Esempio n. 13
0
    def serialize(self, obj: Any) -> Any:
        r = {}
        for idx, v in self.dependencies.items():
            with subserializer(idx):
                item = v.serialize(obj)

                if not isinstance(item, FieldValue):
                    raise SerializationError(val=item,
                                             reason='unsupported_field_defn',
                                             origin=self)

            if item.has_value:
                r[item.name] = item.value

        if self.cls:
            return self.cls(**r)
        else:
            return r
Esempio n. 14
0
    def serialize(self, obj: Any) -> Any:
        with subserializer('value'):
            obj = self.dependencies['value'].serialize(obj)

        if not isinstance(obj, self.enum):
            raise SerializationError(val=obj,
                                     reason='enum_not_enum',
                                     origin=self)

        try:
            obj = obj.value
        except AttributeError as e:
            raise SerializationError(val=obj,
                                     exc=e,
                                     reason='invalid_enum',
                                     origin=self)

        return obj
Esempio n. 15
0
    def serialize(self, obj: Any) -> Any:
        if self.cls and not isinstance(obj, self.cls):
            raise SerializationError(val=obj,
                                     reason='not_instance',
                                     origin=self)

        r = OrderedDict()

        for idx, v in self.dependencies.items():
            with subserializer(idx):
                item = v.serialize(obj)

                if not isinstance(item, FieldValue):
                    raise SerializationError(val=item,
                                             reason='unsupported_field_defn',
                                             origin=self)

            if item.has_value:
                r[item.name] = item.value
        return r
Esempio n. 16
0
 def serialize(self, obj: Any) -> Any:
     with subserializer('$item'):
         obj = self.dependencies['item'].serialize(obj)
     return len(obj)
Esempio n. 17
0
 def serialize(self, obj: Any) -> Any:
     r = []
     for i, x in enumerate(obj):
         with subserializer(i):
             r.append(self.dependencies['value'].serialize(x))
     return r