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
+3-1Lines changed: 3 additions & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,8 @@
2
2
3
3
Class attributes can be specified as arguments in the `serialize`/`deserialize` decorators in order to customize the (de)serialization behaviour of the class entirely. If you want to customize a field, please consider using [Field Attributes](field-attributes.md).
4
4
5
+
Class attributes affect how every field is treated, so they are ideal for naming conventions, tagging, or behavior that must be consistent across the whole class.
6
+
5
7
## Attributes offered by dataclasses
6
8
7
9
### **`frozen`**
@@ -141,7 +143,7 @@ New in v0.13.0.
141
143
142
144
>**NOTE:** Deprecated since v0.13.0. Consider using `class_serializer`and`class_deserializer`.
143
145
144
-
If you want to use a custom (de)serializer at class level, you can pass your (de)serializer methods n`serializer`and`deserializer`class attributes.
146
+
If you want to use a custom (de)serializer at class level, you can pass your (de)serializer methods in`serializer`and`deserializer`class attributes.
Copy file name to clipboardExpand all lines: docs/en/data-formats.md
+7-2Lines changed: 7 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,14 +2,17 @@
2
2
3
3
pyserde supports several data formats for serialization and deserialization, including `dict`, `tuple`, `JSON`, `YAML`, `TOML`, `MsgPack`, and `Pickle`. Each API can take additional keyword arguments, which are forwarded to the underlying packages used by pyserde.
4
4
5
-
e.g. If you want to preserve the field order in YAML, you can pass `sort_key=True` in `serde.yaml.to_yaml`
5
+
Use the format-specific modules (e.g. `serde.json`, `serde.yaml`) when you want to control serialization options. For example, if you want to preserve the field order in YAML, you can pass `sort_key=True` in `serde.yaml.to_yaml`.
6
+
6
7
7
8
```python
8
9
serde.yaml.to_yaml(foo, sort_key=True)
9
10
```
10
11
11
12
`sort_key=True` will be passed in the [yaml.safedump](https://github.com/yukinarit/pyserde/blob/a9f44d52d109144a4c3bb93965f831e91d13960b/serde/yaml.py#L18)
12
13
14
+
> **NOTE:** JSON, YAML, TOML, and MsgPack formats require their matching extras. `dict`, `tuple`, and Pickle work without optional dependencies.
15
+
13
16
## dict
14
17
15
18
```python
@@ -50,6 +53,8 @@ See [serde.to_tuple](https://yukinarit.github.io/pyserde/api/serde/se.html#to_tu
50
53
Foo(i=10, s='foo', f=100.0, b=True)
51
54
```
52
55
56
+
If you enable the `orjson` extra, pyserde can use it under the hood for faster JSON encoding/decoding.
57
+
53
58
See [serde.json.to_json](https://yukinarit.github.io/pyserde/api/serde/json.html#to_json) / [serde.json.from_json](https://yukinarit.github.io/pyserde/api/serde/json.html#from_json) for more information.
54
59
55
60
## Yaml
@@ -119,4 +124,4 @@ See [serde.pickle.to_pickle](https://yukinarit.github.io/pyserde/api/serde/pickl
119
124
120
125
## Needs a new data format support?
121
126
122
-
We don't plan to supprot a data format such as XML to keep pyserde as simple as possible. If you need a new data format support, we recommend to create a separate python package. If the data format is interchangable to dict or tuple, implementing the serialize/desrialize API is not that difficult. See [YAML's implementation](https://github.com/yukinarit/pyserde/blob/main/serde/yaml.py) to know how to implement.
127
+
We don't plan to support a data format such as XML to keep pyserde as simple as possible. If you need a new data format support, we recommend to create a separate python package. If the data format is interchangable to dict or tuple, implementing the serialize/deserialize API is not that difficult. See [YAML's implementation](https://github.com/yukinarit/pyserde/blob/main/serde/yaml.py) to know how to implement.
Copy file name to clipboardExpand all lines: docs/en/field-attributes.md
+5-3Lines changed: 5 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Field Attributes
2
2
3
-
Field attributes are options to customize (de)serialization behaviour for a field of a dataclass.
3
+
Field attributes are options to customize (de)serialization behaviour for a field of a dataclass. Use them when you want different wire formats, optional behavior, or custom logic for specific fields.
4
4
5
5
## Attributes offered by dataclasses
6
6
@@ -34,7 +34,7 @@ Here is an example specifying `rename` attribute in both `serde.field` and `data
@@ -65,7 +65,7 @@ See [examples/skip.py](https://github.com/yukinarit/pyserde/blob/main/examples/s
65
65
66
66
### **`skip_if`**
67
67
68
-
`skip` is used to skip (de)serialization of the field if the predicate function returns `True`.
68
+
`skip_if` is used to skip (de)serialization of the field if the predicate function returns `True`.
69
69
70
70
```python
71
71
@serde
@@ -75,6 +75,8 @@ class World:
75
75
76
76
See [examples/skip.py](https://github.com/yukinarit/pyserde/blob/main/examples/skip.py) for the complete example.
77
77
78
+
> **NOTE:**`skip`, `skip_if`, `skip_if_false`, and `skip_if_default` apply to both serialization and deserialization. Use them to keep wire formats compact or to ignore fields you only use locally.
79
+
78
80
### **`skip_if_false`**
79
81
80
82
`skip` is used to skip (de)serialization of the field if the field evaluates to `False`. For example, this code skip (de)serializing if `enemies` is empty.
> **NOTE:** Extras enable additional formats and types, but you can mix them as needed. For example, install only `toml` and `yaml` if you do not need MsgPack or numpy.
51
+
48
52
## Define your first pyserde class
49
53
50
54
Define your class with pyserde's `@serde` decorators. Be careful that module name is `serde`, not `pyserde`. `pyserde` heavily depends on the standard library's `dataclasses` module. If you are new to dataclass, I would recommend to read [dataclasses documentation](https://docs.python.org/3/library/dataclasses.html) first.
@@ -98,7 +102,7 @@ from serde.json import from_json, to_json
You can also serialize to a Python `dict` when you need to manipulate data before writing it to a file or sending it over the network.
117
+
118
+
```python
119
+
from serde import to_dict, from_dict
120
+
121
+
payload = to_dict(f)
122
+
# e.g. add a field before sending
123
+
payload["source"] ="cli"
124
+
print(from_dict(Foo, payload))
125
+
```
126
+
112
127
That's it! pyserde offers many more features. If you're interested, please read the rest of the documentation.
113
128
129
+
> **NOTE:** If you plan to use YAML, TOML, or MsgPack, remember to install the matching extras listed above.
130
+
114
131
> 💡 Tip: which type checker should I use?
115
132
> pyserde depends on [PEP681 dataclass_transform](https://peps.python.org/pep-0681/). [mypy](https://github.com/python/mypy) does not fully support dataclass_transform as of Jan. 2024. My personal recommendation is [pyright](https://github.com/microsoft/pyright).
Copy file name to clipboardExpand all lines: docs/en/introduction.md
+17Lines changed: 17 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,13 @@
2
2
3
3
`pyserde` is a simple yet powerful serialization library on top of [dataclasses](https://docs.python.org/3/library/dataclasses.html). It allows you to convert Python objects to and from JSON, YAML, and other formats easily and efficiently.
4
4
5
+
pyserde focuses on dataclass-first workflows and keeps runtime overhead low by generating serializers when classes are loaded.
Copy file name to clipboardExpand all lines: docs/en/type-check.md
+6-5Lines changed: 6 additions & 5 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,8 @@
2
2
3
3
pyserde offers runtime type checking since v0.9. It was completely reworked at v0.14 using [beartype](https://github.com/beartype/beartype) and it became more sophisticated and reliable. It is highly recommended to enable type checking always as it helps writing type-safe and robust programs.
4
4
5
+
If you need to accept untrusted input, prefer `strict` or `coerce` so invalid data fails early.
6
+
5
7
## `strict`
6
8
7
9
Strict type checking is to check every field value against the declared type during (de)serialization and object construction. This is the default type check mode since v0.14. What will happen with this mode is if you declare a class with `@serde` decorator without any class attributes, `@serde(type_check=strict)` is assumed and strict type checking is enabled.
> The following code mutates the property "s" at the bottom. beartype can not detect this case.
44
46
> ```python
45
47
>@serde
46
-
>class Foo
48
+
>classFoo:
47
49
> s: str
48
50
>
49
51
> f = Foo("foo")
50
52
> f.s =100
51
53
>```
52
54
>
53
-
>2. beartype can not validate every one of elements in containers. This isnot a bug. This is desgin principle of beartype. See [Does beartype actually do anything?](https://beartype.readthedocs.io/en/latest/faq/#faq-o1].
54
-
>```
55
+
>2. beartype can not validate every one of elements in containers. This isnot a bug. This is desgin principle of beartype. See [Does beartype actually do anything?](https://beartype.readthedocs.io/en/latest/faq/#faq-o1).
55
56
56
57
## `coerce`
57
58
58
59
Type coercing automatically converts a value into the declared type during (de)serialization. If the value is incompatible e.g. value is"foo"andtypeisint, pyserde raises an `SerdeError`.
59
60
60
61
```python
61
62
@serde(type_check=coerce)
62
-
class Foo
63
+
class Foo:
63
64
s: str
64
65
65
66
foo= Foo(10)
@@ -74,7 +75,7 @@ This is the default behavior until pyserde v0.8.3 and v0.9.x. No type coercion o
Copy file name to clipboardExpand all lines: docs/en/types.md
+5-3Lines changed: 5 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
1
1
# Types
2
2
3
-
Here is the list of the supported types. See the simple example for each type in the footnotes
3
+
Here is the list of the supported types. See the simple example for each type in the footnotes.
4
+
5
+
Tip: Most standard-library containers and datetime types just work, but you can always add custom serializers for third-party types.
4
6
5
7
* Primitives (int, float, str, bool) [^1]
6
8
* Containers
@@ -34,7 +36,7 @@ Here is the list of the supported types. See the simple example for each type in
34
36
You can write pretty complex class like this:
35
37
```python
36
38
@serde
37
-
classbar:
39
+
classBar:
38
40
i: int
39
41
40
42
@serde
@@ -101,7 +103,7 @@ It's recommended to exercise caution when relying on experimental features in pr
101
103
102
104
## Needs a new type support?
103
105
104
-
If you need to use a type which is currently not supported in the standard library, please creat a Github issue to request. For types in third party python packages, unless it's polular like numpy, we don't plan to support it to keep pyserde as simple as possible. We recommend to use custom class or field serializer.
106
+
If you need to use a type which is currently not supported in the standard library, please create a Github issue to request. For types in third party python packages, unless it's popular like numpy, we don't plan to support it to keep pyserde as simple as possible. We recommend to use custom class or field serializer.
105
107
106
108
[^1]: See [examples/simple.py](https://github.com/yukinarit/pyserde/blob/main/examples/simple.py)
0 commit comments