8000 Added docs on dumping dataclasses to JSON (#1487) · ag-python/pydantic@28c2ac7 · GitHub
[go: up one dir, main page]

Skip to content 8000

Commit 28c2ac7

Browse files
authored
Added docs on dumping dataclasses to JSON (pydantic#1487)
1 parent 0b9b308 commit 28c2ac7

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

changes/1487-mikegrima.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Added docs about dumping dataclasses to JSON.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import dataclasses
2+
import json
3+
from typing import List
4+
5+
from pydantic.dataclasses import dataclass
6+
from pydantic.json import pydantic_encoder
7+
8+
@dataclass
9+
class User:
10+
id: int
11+
name: str = 'John Doe'
12+
friends: List[int] = dataclasses.field(default_factory=lambda: [0])
13+
14+
user = User(id='42')
15+
print(json.dumps(user, indent=4, default=pydantic_encoder))

docs/usage/dataclasses.md

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ _(This script is complete, it should run "as is")_
1111
!!! note
1212
Keep in mind that `pydantic.dataclasses.dataclass` is a drop-in replacement for `dataclasses.dataclass`
1313
with validation, **not** a replacement for `pydantic.BaseModel` (with a small difference in how [initialization hooks](#initialize-hooks) work). There are cases where subclassing
14-
`pydantic.BaseModel` is the better choice.
15-
14+
`pydantic.BaseModel` is the better choice.
15+
1616
For more information and discussion see
1717
[samuelcolvin/pydantic#710](https://github.com/samuelcolvin/pydantic/issues/710).
1818

@@ -33,7 +33,7 @@ keyword argument `config` which has the same meaning as [Config](model_config.md
3333
!!! warning
3434
After v1.2, [The Mypy plugin](/mypy_plugin.md) must be installed to type check pydantic dataclasses.
3535

36-
For more information about combining validators with dataclasses, see
36+
For more information about combining validators with dataclasses, see
3737
[dataclass validators](validators.md#dataclass-validators).
3838

3939
## Nested dataclasses
@@ -70,4 +70,12 @@ _(This script is complete, it should run "as is")_
7070

7171
Note that the `dataclasses.dataclass` from python stdlib implements only the `__post_init__` method since it doesn't run a validation step.
7272

73-
When substituting usage of `dataclasses.dataclass` with `pydantic.dataclasses.dataclass`, it is recommended to move the code executed in the `__post_init__` method to the `__post_init_post_parse__` method, and only leave behind part of code which needs to be executed before validation.
73+
When substituting usage of `dataclasses.dataclass` with `pydantic.dataclasses.dataclass`, it is recommended to move the code executed in the `__post_init__` method to the `__post_init_post_parse__` method, and only leave behind part of code which needs to be executed before validation.
74+
75+
## JSON Dumping
76+
77+
Pydantic dataclasses do not feature a `.json()` function. To dump them as JSON, you will need to make use of the `pydantic_encoder` as follows:
78+
79+
```py
80+
{!.tmp_examples/dataclasses_json_dumps.py!}
81+
```

0 commit comments

Comments
 (0)
0