Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added docs on dumping dataclasses to JSON (#1487)
- Loading branch information
Showing
3 changed files
with
28 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Added docs about dumping dataclasses to JSON. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters