Fast and well tested serialization library
APACHE-2.0 License
Bot releases are hidden (Show)
typing.NewType
typing.Literal
typing_extensions.Literal
typing.Annotated
typing_extensions.Annotated
zoneinfo.ZoneInfo
typing_extensions.OrderedDict
on Python<3.7.2.SerializableType
generic classes.pass_through
object that can be used in serialization_strategy and serialize / deserialize options.msgpack
, pyyaml
dependencies to extras_require
(https://github.com/Fatal1ty/mashumaro/issues/7).DataClassJSONMixin
, DataClassMessagePackMixin
, DataClassYAMLMixin
to mashumaro.mixins.*
subpackages.use_bytes
, use_enum
, use_datetime
parameters from DataClassDictMixin
methods.to_*
, from_*
methods of the serialization mixins in order to pass keyword arguments to underlying to_dict
, from_dict
methods.You can find migration guide here: https://github.com/Fatal1ty/mashumaro/blob/master/docs/2to3.md.
Union[None, X]
with None
on the first place wasn't treated as Optional[X]
.Union[X, T]
where T
was resolved to None
wasn't treated as Optional[X]
.None
as the field type (it's considered equivalent to NoneType
).NoneType
to None
in Unions for convenience. In the previous versions you could see Union[int, str, NoneType]
instead of Union[int, str, None]
if the field was declared as Union[int, str, None]
.to_msgpack
and from_msgpack
methods. See https://github.com/Fatal1ty/mashumaro/issues/63.serialization_strategy
config option in a generic dataclass.Union
and TypeVar
types. See https://github.com/Fatal1ty/mashumaro/issues/61.by_alias
argument of to_dict
method when using serialize_by_alias
config option. In the previous versions by_alias
argument had a default valueFalse
regardless of whether serialize_by_alias
config options was used. Now it could be True
:@dataclass
class MyClass(DataClassDictMixin):
x: int
class Config(BaseConfig):
aliases = {"x": "x_alias"}
serialize_by_alias = True
code_generation_options = [TO_DICT_ADD_BY_ALIAS_FLAG]
print(MyClass(x=1).to_dict()) # {'x_alias': 1}
print(MyClass(x=1).to_dict(by_alias=False)) # {x': 1}
as_dict
and as_list
serialization and deserialization enginesnamedtuple_as_dict
config optionTypedDict
NamedTuple
and untyped namedtuple
Tuple
serialization and deserialization. Before 2.8 all values of tuples were deserialized as if they were values of the first type, no matter how many values the tuple was supposed to take. Now the following types are handled correctly: Tuple[int]
, Tuple[int, ...]
, Tuple[int, str]
, Tuple[()]
, Tuple
.Optional
types inside collections (https://github.com/Fatal1ty/mashumaro/issues/54)np.ndarray
and other third-party collection types (https://github.com/Fatal1ty/mashumaro/issues/53)TypeVar
types. It works like Union
under the hood and both constraints and upper bound can be used to specify a variation of types. Similarly to Union
it's recommended to place more complex variant types at first place like TypeVar("T", Dict[int, int], List[int])
not TypeVar("T", List[int], Dict[int, int])
.List
instead of List[Any]
or Dict
instead of Dict[Any, Any]
.int
instead of builtins.int
or List
instead of typing.List
.MutableMapping
. See https://github.com/Fatal1ty/mashumaro/issues/48.Union
type fields in some cases.to_dict
method of a class that doesn't have omit_none
and by_alias
keyword arguments added in case it has a field of the type that has these arguments added:@dataclass
class A(DataClassDictMixin):
x: Optional[int] = None
class Config(BaseConfig):
aliases = {"x": "x_alias"}
code_generation_options = [
TO_DICT_ADD_OMIT_NONE_FLAG,
TO_DICT_ADD_BY_ALIAS_FLAG,
]
@dataclass
class B(DataClassDictMixin):
a: Optional[A] = None
# This caused an exception NameError: name 'omit_none' is not defined
print(B(a=A(1)).to_dict())
SerializableType
data classes.UnserializableField
for data classes without DataClassDictMixin
parent.ThirdPartyModuleNotFoundError
, that is raised in case you use pendulum
or ciso8601
as the deserialization method, but the corresponding package isn't installed.__slots__
attribute. See https://github.com/Fatal1ty/mashumaro/issues/47.alias
field optionaliases
config optionserialize_by_alias
config optionTO_DICT_ADD_BY_ALIAS_FLAG
code generation optionserialize
method of SerializationStrategy
registered in Config
: https://github.com/Fatal1ty/mashumaro/issues/44