-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add nested json encoding * fix timezones * add changes doc * split tests and rename example * Update changes/3941-lilyminium.md Co-authored-by: Samuel Colvin <samcolvin@gmail.com> * split tests into functions Co-authored-by: Samuel Colvin <samcolvin@gmail.com> Co-authored-by: Samuel Colvin <s@muelcolvin.com>
- Loading branch information
1 parent
8f388e1
commit b42fae0
Showing
5 changed files
with
145 additions
and
0 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 @@ | ||
add `use_nested_encoders` keyword argument to `BaseModel.json` to allow encoding nested subclasses with encoders specified in the inner classes |
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,41 @@ | ||
from datetime import datetime, timedelta | ||
from pydantic import BaseModel | ||
from pydantic.json import timedelta_isoformat | ||
|
||
|
||
class CustomChildModel(BaseModel): | ||
dt: datetime | ||
diff: timedelta | ||
|
||
class Config: | ||
json_encoders = { | ||
datetime: lambda v: v.timestamp(), | ||
timedelta: timedelta_isoformat, | ||
} | ||
|
||
|
||
class ParentModel(BaseModel): | ||
diff: timedelta | ||
child: CustomChildModel | ||
|
||
class Config: | ||
json_encoders = { | ||
timedelta: lambda v: v.total_seconds(), | ||
CustomChildModel: lambda _: 'using parent encoder', | ||
} | ||
|
||
|
||
child = CustomChildModel(dt=datetime(2032, 6, 1), diff=timedelta(hours=100)) | ||
parent = ParentModel(diff=timedelta(hours=3), child=child) | ||
|
||
# default encoder uses total_seconds() for diff | ||
print(parent.json()) | ||
|
||
# nested encoder uses isoformat | ||
print(parent.json(use_nested_encoders=True)) | ||
|
||
# turning off models_as_dict only uses the top-level formatter, however | ||
|
||
print(parent.json(models_as_dict=False, use_nested_encoders=True)) | ||
|
||
print(parent.json(models_as_dict=False, use_nested_encoders=False)) |
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
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
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