Skip to content

Commit

Permalink
Added docs on dumping dataclasses to JSON (#1487)
Browse files Browse the repository at this point in the history
  • Loading branch information
mikegrima committed May 18, 2020
1 parent 0b9b308 commit 28c2ac7
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
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!}
```

0 comments on commit 28c2ac7

Please sign in to comment.