You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/en/class-attributes.md
+24-2Lines changed: 24 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -75,12 +75,12 @@ New in v0.7.0. See [Union](union.md).
75
75
If you want to use a custom (de)serializer at class level, you can pass your (de)serializer objectin`class_serializer`and`class_deserializer`class attributes. Class custom (de)serializer depends on a python library [plum](https://github.com/beartype/plum) which allows multiple method overloading like C++. With plum, you can write robust custom (de)serializer in a quite neat way.
* If both field and class serializer specified, field serializer is prioritized
98
98
* If both legacy and new class serializer specified, new class serializer is prioritized
99
99
100
+
> 💡 Tip: If you implements multiple `serialize` methods, you will receive "Redefinition of unused `serialize`" warning from type checker. In such case, try using `plum.overload` and `plum.dispatch` to workaround it. See [plum's documentation](https://beartype.github.io/plum/integration.html) for more information.
101
+
>
102
+
> ```python
103
+
>from plum import dispatch, overload
104
+
>
105
+
>classSerializer:
106
+
># use @overload
107
+
>@overload
108
+
>defserialize(self, value: int) -> Any:
109
+
>returnstr(value)
110
+
>
111
+
># use @overload
112
+
>@overload
113
+
>defserialize(self, value: float) -> Any:
114
+
>returnint(value)
115
+
>
116
+
># Add method time and make sure to add @dispatch. Plum will do all the magic to erase warnings from type checker.
117
+
>@dispatch
118
+
>defserialize(self, value: Any) -> Any:
119
+
>...
120
+
>```
121
+
100
122
See [examples/custom_class_serializer.py](https://github.com/yukinarit/pyserde/blob/main/examples/custom_class_serializer.py) for complete example.
pyserde offers three ways to extend pyserde to support non builtin types.
4
+
5
+
## Custom field (de)serializer
6
+
7
+
See [custom field serializer](./field-attributes.md#serializerdeserializer).
8
+
9
+
> 💡 Tip: wrapping `serde.field` with your own field function makes
10
+
>
11
+
> ```python
12
+
>import serde
13
+
>
14
+
>deffield(*args, **kwargs):
15
+
> serde.field(*args, **kwargs, serializer=str)
16
+
>
17
+
>@serde
18
+
>classFoo:
19
+
> a: int= field(default=0) # Configuring field serializer
20
+
>```
21
+
22
+
## Custom class (de)serializer
23
+
24
+
See [custom class serializer](./class-attributes.md#class_serializer--class_deserializer).
25
+
26
+
## Custom global (de)serializer
27
+
28
+
You apply the custom (de)serialization for entire codebase by registering class (de)serializer by `add_serializer`and`add_deserializer`. Registered class (de)serializers are stacked in pyserde's global space and automatically used for all the pyserde classes.
29
+
30
+
e.g. Implementing custom (de)serialization for`datetime.timedelta` using [isodate](https://pypi.org/project/isodate/) package.
31
+
32
+
Here is the code of registering class (de)serializer for`datetime.timedelta`.
Users of this package can reuse custom (de)serialization functionality for`datetime.timedelta` just by calling `serde_timedelta.init()`.
57
+
58
+
```python
59
+
import serde_timedelta
60
+
from serde import serde
61
+
from serde.json import to_json, from_json
62
+
from datetime import timedelta
63
+
64
+
serde_timedelta.init()
65
+
66
+
@serde
67
+
class Foo:
68
+
a: timedelta
69
+
70
+
f= Foo(timedelta(hours=10))
71
+
json= to_json(f)
72
+
print(json)
73
+
print(from_json(Foo, json))
74
+
```
75
+
and you get `datetime.timedelta` to be serialized inISO8601 duration format!
76
+
```bash
77
+
{"a":"PT10H"}
78
+
Foo(a=datetime.timedelta(seconds=36000))
79
+
```
80
+
81
+
> 💡 Tip: You can register as many class (de)serializer as you want. This means you can use as many pyserde extensions as you want.
82
+
> Registered (de)serializers are stacked in the memory. A (de)serializer can be overridden by another (de)serializer.
83
+
>
84
+
> e.g. If you register 3 custom serializers in this order, the first serializer will completely overridden by the 3rd one. 2nd one works because it is implemented for a different type.
0 commit comments