Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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],
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,
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,