How to use the mashumaro.types.SerializableType function in mashumaro

To help you get started, we’ve selected a few mashumaro examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Fatal1ty / mashumaro / tests / entities.py View on Github external
a = 1
    b = 2


class MyIntFlag(IntFlag):
    a = 1
    b = 2


@dataclass
class MyDataClass(DataClassDictMixin):
    a: int
    b: int


class MutableString(SerializableType):
    def __init__(self, value: str):
        self.characters = [c for c in value]

    def _serialize(self) -> str:
        return str(self)

    @classmethod
    def _deserialize(cls, value: str) -> 'MutableString':
        return MutableString(value)

    def __str__(self):
        return ''.join(self.characters)

    def __eq__(self, other):
        return self.characters == other.characters
github Fatal1ty / mashumaro / mashumaro / serializer / base / metaprogramming.py View on Github external
def _pack_value(self, fname, ftype, parent, value_name='value'):

        if is_dataclass(ftype):
            return f"{value_name}.to_dict(use_bytes, use_enum, use_datetime)"

        with suppress(TypeError):
            if issubclass(ftype, SerializableType):
                return f'{value_name}._serialize()'
        if isinstance(ftype, SerializationStrategy):
            return f"self.__dataclass_fields__['{fname}'].type" \
                f"._serialize({value_name})"

        origin_type = get_type_origin(ftype)
        if is_special_typing_primitive(origin_type):
            if origin_type is typing.Any:
                return value_name
            elif is_union(ftype):
                args = getattr(ftype, '__args__', ())
                if len(args) == 2 and args[1] == NoneType:  # it is Optional
                    return self._pack_value(fname, args[0], parent)
                else:
                    raise UnserializableDataError(
                        'Unions are not supported by mashumaro')
github Fatal1ty / mashumaro / mashumaro / serializer / base / metaprogramming.py View on Github external
def _unpack_field_value(self, fname, ftype, parent, value_name='value'):

        if is_dataclass(ftype):
            return f"{type_name(ftype)}.from_dict({value_name}, " \
                   f"use_bytes, use_enum, use_datetime)"

        with suppress(TypeError):
            if issubclass(ftype, SerializableType):
                return f'{type_name(ftype)}._deserialize({value_name})'
        if isinstance(ftype, SerializationStrategy):
            return f"cls.__dataclass_fields__['{fname}'].type" \
                f"._deserialize({value_name})"

        origin_type = get_type_origin(ftype)
        if is_special_typing_primitive(origin_type):
            if origin_type is typing.Any:
                return value_name
            elif is_union(ftype):
                args = getattr(ftype, '__args__', ())
                if len(args) == 2 and args[1] == NoneType:  # it is Optional
                    return self._unpack_field_value(fname, args[0], parent)
                else:
                    raise UnserializableDataError(
                        'Unions are not supported by mashumaro')