Skip to content

Commit 421772d

Browse files
authored
Refine pyserde guide (#700)
1 parent 2206803 commit 421772d

22 files changed

+131
-25
lines changed

docs/en/class-attributes.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
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).
44

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+
57
## Attributes offered by dataclasses
68

79
### **`frozen`**
@@ -141,7 +143,7 @@ New in v0.13.0.
141143
142144
> **NOTE:** Deprecated since v0.13.0. Consider using `class_serializer` and `class_deserializer`.
143145
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.
145147
146148
```python
147149
def serializer(cls, o):

docs/en/data-formats.md

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@
22

33
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.
44

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+
67

78
```python
89
serde.yaml.to_yaml(foo, sort_key=True)
910
```
1011

1112
`sort_key=True` will be passed in the [yaml.safedump](https://github.com/yukinarit/pyserde/blob/a9f44d52d109144a4c3bb93965f831e91d13960b/serde/yaml.py#L18)
1213

14+
> **NOTE:** JSON, YAML, TOML, and MsgPack formats require their matching extras. `dict`, `tuple`, and Pickle work without optional dependencies.
15+
1316
## dict
1417

1518
```python
@@ -50,6 +53,8 @@ See [serde.to_tuple](https://yukinarit.github.io/pyserde/api/serde/se.html#to_tu
5053
Foo(i=10, s='foo', f=100.0, b=True)
5154
```
5255

56+
If you enable the `orjson` extra, pyserde can use it under the hood for faster JSON encoding/decoding.
57+
5358
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.
5459

5560
## Yaml
@@ -119,4 +124,4 @@ See [serde.pickle.to_pickle](https://yukinarit.github.io/pyserde/api/serde/pickl
119124

120125
## Needs a new data format support?
121126

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.

docs/en/decorators.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ class Foo:
2727
* You can pass both (de)serialize attributes to the decorator
2828
* `serializer` attribute is ignored in `@deserialize` and `deserializer` attribute is ignored in `@serialize`
2929

30+
Use `@serde` for most classes unless you need separate serialization/deserialization configs.
31+
3032
```python
3133
@serde(serializer=serializer, deserializer=deserializer)
3234
@dataclass

docs/en/extension.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
pyserde offers three ways to extend pyserde to support non builtin types.
44

5+
Choose field-level serializers for one-off fields, class serializers for reusable types, and global serializers to apply extensions project-wide.
6+
57
## Custom field (de)serializer
68

79
See [custom field serializer](./field-attributes.md#serializerdeserializer).

docs/en/faq.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ pyserde provides `inspect` submodule that works as commandline:
77
python -m serde.inspect <PATH_TO_FILE> <CLASS>
88
```
99

10+
This is useful when you want to understand generated code, debug custom serializers, or confirm how pyserde handles a specific type.
11+
1012
e.g. in pyserde project
1113

1214
```

docs/en/field-attributes.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Field Attributes
22

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.
44

55
## Attributes offered by dataclasses
66

@@ -34,7 +34,7 @@ Here is an example specifying `rename` attribute in both `serde.field` and `data
3434
@serde.serde
3535
class Foo:
3636
a: str = serde.field(rename="A")
37-
b: str = dataclasses.field(metadata={"serde_rename"="B"})
37+
b: str = dataclasses.field(metadata={"serde_rename": "B"})
3838
```
3939

4040
### **`rename`**
@@ -65,7 +65,7 @@ See [examples/skip.py](https://github.com/yukinarit/pyserde/blob/main/examples/s
6565

6666
### **`skip_if`**
6767

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`.
6969

7070
```python
7171
@serde
@@ -75,6 +75,8 @@ class World:
7575

7676
See [examples/skip.py](https://github.com/yukinarit/pyserde/blob/main/examples/skip.py) for the complete example.
7777

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+
7880
### **`skip_if_false`**
7981

8082
`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.

docs/en/getting-started.md

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
Install pyserde from PyPI. pyserde requires Python>=3.10.
66

7+
If you want faster JSON handling, you can also install `orjson` and use the `orjson` extra for optional acceleration.
8+
79
```
810
pip install pyserde
911
```
@@ -45,6 +47,8 @@ Here are the available extras
4547
* `orjson`: Install [orjson](https://github.com/ijl/orjson)
4648
* `sqlalchemy`: Install [sqlalchemy](https://github.com/sqlalchemy/sqlalchemy)
4749

50+
> **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+
4852
## Define your first pyserde class
4953

5054
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
98102
```
99103
100104
Use `to_json` to serialize the object into JSON.
101-
```
105+
```python
102106
f = Foo(i=10, s='foo', f=100.0, b=True)
103107
print(to_json(f))
104108
```
@@ -109,7 +113,20 @@ s = '{"i": 10, "s": "foo", "f": 100.0, "b": true}'
109113
print(from_json(Foo, s))
110114
```
111115
116+
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+
112127
That's it! pyserde offers many more features. If you're interested, please read the rest of the documentation.
113128

129+
> **NOTE:** If you plan to use YAML, TOML, or MsgPack, remember to install the matching extras listed above.
130+
114131
> 💡 Tip: which type checker should I use?
115132
> 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).

docs/en/introduction.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,13 @@
22

33
`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.
44

5+
pyserde focuses on dataclass-first workflows and keeps runtime overhead low by generating serializers when classes are loaded.
6+
7+
Highlights:
8+
- Multiple formats (`dict`, `tuple`, JSON, YAML, TOML, MsgPack, Pickle)
9+
- Rich type support (Optional, Union, Literals, enums, datetime, numpy, etc.)
10+
- Runtime type checking and extensibility hooks
11+
512
Declare your class with `@serde` decorator and annotate fields using [PEP484](https://peps.python.org/pep-0484/) as below.
613

714
```python
@@ -30,6 +37,16 @@ You can deserialize JSON into `Foo` object.
3037
Foo(i=10, s='foo', f=100.0, b=True)
3138
```
3239

40+
pyserde also provides in-memory formats like `dict` and `tuple`, plus YAML, TOML, MsgPack, and Pickle when their extras are installed.
41+
42+
```python
43+
>>> from serde import to_dict, from_dict
44+
>>> to_dict(Foo(i=10, s='foo', f=100.0, b=True))
45+
{"i": 10, "s": "foo", "f": 100.0, "b": True}
46+
>>> from_dict(Foo, {"i": 10, "s": "foo", "f": 100.0, "b": True})
47+
Foo(i=10, s='foo', f=100.0, b=True)
48+
```
49+
3350
## Next Steps
3451

3552
* [Getting started](getting-started.md)

docs/en/type-check.md

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
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.
44

5+
If you need to accept untrusted input, prefer `strict` or `coerce` so invalid data fails early.
6+
57
## `strict`
68

79
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.
@@ -43,23 +45,22 @@ serde.compat.SerdeError: Method __main__.Foo.__init__() parameter s=10 violates
4345
> The following code mutates the property "s" at the bottom. beartype can not detect this case.
4446
> ```python
4547
> @serde
46-
> class Foo
48+
> class Foo:
4749
> s: str
4850
>
4951
> f = Foo("foo")
5052
> f.s = 100
5153
> ```
5254
>
53-
> 2. beartype can not validate every one of elements in containers. This is not 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 is not a bug. This is desgin principle of beartype. See [Does beartype actually do anything?](https://beartype.readthedocs.io/en/latest/faq/#faq-o1).
5556
5657
## `coerce`
5758
5859
Type coercing automatically converts a value into the declared type during (de)serialization. If the value is incompatible e.g. value is "foo" and type is int, pyserde raises an `SerdeError`.
5960
6061
```python
6162
@serde(type_check=coerce)
62-
class Foo
63+
class Foo:
6364
s: str
6465
6566
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
7475
7576
```python
7677
@serde
77-
class Foo
78+
class Foo:
7879
s: str
7980
8081
foo = Foo(10)

docs/en/types.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
# Types
22

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.
46

57
* Primitives (int, float, str, bool) [^1]
68
* Containers
@@ -34,7 +36,7 @@ Here is the list of the supported types. See the simple example for each type in
3436
You can write pretty complex class like this:
3537
```python
3638
@serde
37-
class bar:
39+
class Bar:
3840
i: int
3941

4042
@serde
@@ -101,7 +103,7 @@ It's recommended to exercise caution when relying on experimental features in pr
101103

102104
## Needs a new type support?
103105

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.
105107

106108
[^1]: See [examples/simple.py](https://github.com/yukinarit/pyserde/blob/main/examples/simple.py)
107109

0 commit comments

Comments
 (0)