Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add merged json_encoders inheritance (#2064)
* Implement merged json_encoders inheritance * json_encoders inheritance documentation
- Loading branch information
Showing
5 changed files
with
70 additions
and
2 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 merged `json_encoders` inheritance |
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,24 @@ | ||
from datetime import datetime, timedelta | ||
from pydantic import BaseModel | ||
from pydantic.json import timedelta_isoformat | ||
|
||
|
||
class BaseClassWithEncoders(BaseModel): | ||
dt: datetime | ||
diff: timedelta | ||
|
||
class Config: | ||
json_encoders = { | ||
datetime: lambda v: v.timestamp() | ||
} | ||
|
||
|
||
class ChildClassWithEncoders(BaseClassWithEncoders): | ||
class Config: | ||
json_encoders = { | ||
timedelta: timedelta_isoformat | ||
} | ||
|
||
|
||
m = ChildClassWithEncoders(dt=datetime(2032, 6, 1), diff=timedelta(hours=100)) | ||
print(m.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
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