How to use the mashumaro.serializer.base.DataClassDictMixin 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 / mashumaro / serializer / msgpack.py View on Github external
from mashumaro.serializer.base import DataClassDictMixin


DEFAULT_DICT_PARAMS = {
    'use_bytes': True,
    'use_enum': False,
    'use_datetime': False
}
EncodedData = Union[str, bytes, bytearray]
Encoder = Callable[[Dict], EncodedData]
Decoder = Callable[[EncodedData], Dict]
T = TypeVar('T', bound='DataClassMessagePackMixin')


class DataClassMessagePackMixin(DataClassDictMixin):
    def to_msgpack(
            self: T,
            encoder: Optional[Encoder] = partial(
                msgpack.packb, use_bin_type = True
            ),
            dict_params: Optional[Mapping] = MappingProxyType({}),
            **encoder_kwargs) -> EncodedData:

        return encoder(
            self.to_dict(**dict(DEFAULT_DICT_PARAMS, **dict_params)),
            **encoder_kwargs
        )

    @classmethod
    def from_msgpack(
            cls: Type[T],
github Fatal1ty / mashumaro / mashumaro / serializer / json.py View on Github external
from mashumaro.serializer.base import DataClassDictMixin


DEFAULT_DICT_PARAMS = {
    'use_bytes': False,
    'use_enum': False,
    'use_datetime': False
}
EncodedData = Union[str, bytes, bytearray]
Encoder = Callable[[Dict], EncodedData]
Decoder = Callable[[EncodedData], Dict]
T = TypeVar('T', bound='DataClassJSONMixin')


class DataClassJSONMixin(DataClassDictMixin):
    def to_json(
            self: T,
            encoder: Optional[Encoder] = json.dumps,
            dict_params: Optional[Mapping] = MappingProxyType({}),
            **encoder_kwargs) -> EncodedData:

        return encoder(
            self.to_dict(**dict(DEFAULT_DICT_PARAMS, **dict_params)),
            **encoder_kwargs
        )

    @classmethod
    def from_json(
            cls: Type[T],
            data: EncodedData,
            decoder: Optional[Decoder] = json.loads,
github Fatal1ty / mashumaro / mashumaro / serializer / yaml.py View on Github external
from mashumaro.serializer.base import DataClassDictMixin


DEFAULT_DICT_PARAMS = {
    'use_bytes': False,
    'use_enum': False,
    'use_datetime': False
}
EncodedData = Union[str, bytes]
Encoder = Callable[[Dict], EncodedData]
Decoder = Callable[[EncodedData], Dict]
T = TypeVar('T', bound='DataClassYAMLMixin')


class DataClassYAMLMixin(DataClassDictMixin):
    def to_yaml(
            self: T,
            encoder: Optional[Encoder] = yaml.dump,
            dict_params: Optional[Mapping] = MappingProxyType({}),
            **encoder_kwargs) -> EncodedData:

        return encoder(
            self.to_dict(**dict(DEFAULT_DICT_PARAMS, **dict_params)),
            **encoder_kwargs
        )

    @classmethod
    def from_yaml(
            cls: Type[T],
            data: EncodedData,
            decoder: Optional[Decoder] = yaml.safe_load,