Skip to content

v1.20

Compare
Choose a tag to compare
@Fatal1ty Fatal1ty released this 09 Jan 20:59
· 993 commits to master since this release

Changes

@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