forked from pydantic/pydantic
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
custom json (de)serialisation (pydantic#823)
* custom json (d)encoders, fix pydantic#714 * add docs
- Loading branch information
1 parent
7ea07b8
commit 802cd85
Showing
11 changed files
with
125 additions
and
29 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 @@ | ||
Allow custom JSON decoding and encoding via ``json_loads`` and ``json_dumps`` ``Config`` properties. |
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,21 @@ | ||
from datetime import datetime | ||
import orjson | ||
from pydantic import BaseModel | ||
|
||
def orjson_dumps(v, *, default): | ||
# orjson.dumps returns bytes, to match standard json.dumps we need to decode | ||
return orjson.dumps(v, default=default).decode() | ||
|
||
class User(BaseModel): | ||
id: int | ||
name = 'John Doe' | ||
signup_ts: datetime = None | ||
|
||
class Config: | ||
json_loads = orjson.loads | ||
json_dumps = orjson_dumps | ||
|
||
|
||
user = User.parse_raw('{"id": 123, "signup_ts": 1234567890, "name": "John Doe"}') | ||
print(user.json()) | ||
#> {"id":123,"signup_ts":"2009-02-13T23:31:30+00:00","name":"John Doe"} |
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 @@ | ||
from datetime import datetime | ||
import ujson | ||
from pydantic import BaseModel | ||
|
||
class User(BaseModel): | ||
id: int | ||
name = 'John Doe' | ||
signup_ts: datetime = None | ||
|
||
class Config: | ||
json_loads = ujson.loads | ||
|
||
user = User.parse_raw('{"id": 123, "signup_ts": 1234567890, "name": "John Doe"}') | ||
print(user) | ||
#> User id=123 signup_ts=datetime.datetime(2009, 2, 13, 23, 31, 30, tzinfo=datetime.timezone.utc) name='John Doe' |
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
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
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