Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added docs on dumping dataclasses to JSON #1487

Merged
merged 1 commit into from May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changes/1487-mikegrima.md
@@ -0,0 +1 @@
Added docs about dumping dataclasses to JSON.
15 changes: 15 additions & 0 deletions docs/examples/dataclasses_json_dumps.py
@@ -0,0 +1,15 @@
import dataclasses
import json
from typing import List

from pydantic.dataclasses import dataclass
from pydantic.json import pydantic_encoder

@dataclass
class User:
id: int
name: str = 'John Doe'
friends: List[int] = dataclasses.field(default_factory=lambda: [0])

user = User(id='42')
print(json.dumps(user, indent=4, default=pydantic_encoder))
16 changes: 12 additions & 4 deletions docs/usage/dataclasses.md
Expand Up @@ -11,8 +11,8 @@ _(This script is complete, it should run "as is")_
!!! note
Keep in mind that `pydantic.dataclasses.dataclass` is a drop-in replacement for `dataclasses.dataclass`
with validation, **not** a replacement for `pydantic.BaseModel` (with a small difference in how [initialization hooks](#initialize-hooks) work). There are cases where subclassing
`pydantic.BaseModel` is the better choice.
`pydantic.BaseModel` is the better choice.

For more information and discussion see
[samuelcolvin/pydantic#710](https://github.com/samuelcolvin/pydantic/issues/710).

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

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

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

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

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

## JSON Dumping

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:

```py
{!.tmp_examples/dataclasses_json_dumps.py!}
```