Changes
--------
* Added serialization hooks https://github.com/Fatal1ty/mashumaro#serialization-hooks
python
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