@serde is a shortcut of @serialize and @deserialize decorators. This code
@serde
@dataclass
class Foo:
...is equivalent to the following code.
@deserialize
@serialize
@dataclass
class Foo:
...@serde decorator does those for you:
- Automatically add @serialize and @deserialize decorators to a class
- Automatically add @dataclass decorator to a class
- You can pass both (de)serialize attributes to the decorator
serializerattribute is ignored in@deserializeanddeserializerattribute is ignored in@serialize
@serde(serializer=serializer, deserializer=deserializer)
@dataclass
class Foo:
...Note:
@serdeactually works without @dataclass decorator, because it detects and add @dataclass to the declared class automatically. However, mypy will produceToo many argumentsorUnexpected keyword argumenterror. This is due to the current mypy limitation. See the following documentation for more information. https://mypy.readthedocs.io/en/stable/additional_features.html#caveats-known-issues@serde class Foo: ...
@serialize and @deserialize are used by @serde under the hood. It's recommended to use the decorators in such cases:
- You only need either serialization or deserialization functionality
- You want to have different class attributes
@deserialize(rename_all = "snakecase")
@serialize(rename_all = "camelcase")
class Foo:
...