Releases: Fatal1ty/mashumaro
Releases · Fatal1ty/mashumaro
v2.2
v2.1
Changes
- Added support for aliases:
- new
alias
field option - new
aliases
config option - new
serialize_by_alias
config option - new
TO_DICT_ADD_BY_ALIAS_FLAG
code generation option
- new
v2.0.2
v2.0.1
v2.0
Changes
- Added support for
collections.OrderedDict
(3.7+) andcollections.Counter
types - Fixed passing
serialize
anddeserialize
field metadata options inUnion
andOptional
types SerializationStrategy
is now a field option (backward incompatible change). See here for details.- Added ability to configure some options using inner
Config
class. See here for details. - Fixed typing problem on python 3.6. See #38.
v1.24
Changes
- Added support for
Union
types. It's recommended to place more complex variant types at first place likeUnion[Dict[int, int], List[int]]
notUnion[List[int], Dict[int, int]]
. When optional type validation is implemented it will be possible not to follow this rule. - It is now possible to use
serialize
anddeserialize
options for any third-party types andSerializableType
classes if you need it for some reason.
v1.23
v1.22
Changes
- Support for hooks declared in superclasses
class Counter:
deserialize = 0
serialize = 0
@classmethod
def __pre_deserialize__(cls, d):
Counter.deserialize += 1
return d
def __pre_serialize__(self):
Counter.serialize += 1
return self
@dataclass
class Derived(Counter, DataClassDictMixin):
a: int
obj = Derived.from_dict({"a": 1})
obj.to_dict()
print(Counter.deserialize) # 1
print(Counter.serialize) # 1
v1.21
v1.20
Changes
- Added serialization hooks https://github.com/Fatal1ty/mashumaro#serialization-hooks
@dataclass
class User(DataClassJSONMixin):
name: str
password: str
is_deserialized: bool = False
counter: ClassVar[int] = 0
@classmethod
def __pre_deserialize__(cls, d: Dict[Any, Any]) -> Dict[Any, Any]:
return {k.lower(): v for k, v in d.items()}
@classmethod
def __post_deserialize__(cls, obj: "User") -> "User":
obj.is_deserialized = True
return obj
def __pre_serialize__(self) -> "User":
self.counter += 1
return self
def __post_serialize__(self, d: Dict[Any, Any]) -> Dict[Any, Any]:
d.pop("password")
return d
user = User.from_json('{"NAME": "Name", "PASSWORD": "secret"}')
print(user) # User(name='Name', password='secret', is_deserialized=True)
print(user.to_json()) # {"name": "Name", "is_deserialized": true}
print(user.counter) # 1