-
-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: move serialization helpers into one place (#40)
- Loading branch information
Showing
3 changed files
with
27 additions
and
22 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,24 @@ | ||
from typing import Any | ||
|
||
__all__ = ("encode_json", "decode_json") | ||
|
||
try: | ||
from msgspec.json import Decoder, Encoder | ||
|
||
encoder, decoder = Encoder(), Decoder() | ||
decode_json = decoder.decode | ||
|
||
def encode_json(data: Any) -> str: | ||
return encoder.encode(data).decode("utf-8") | ||
|
||
except ImportError: | ||
try: | ||
from orjson import dumps as _encode_json | ||
from orjson import loads as decode_json # type: ignore[no-redef] | ||
|
||
def encode_json(data: Any) -> str: | ||
return _encode_json(data).decode("utf-8") # type: ignore[no-any-return] | ||
|
||
except ImportError: | ||
from json import dumps as encode_json # type: ignore[assignment] | ||
from json import loads as decode_json # type: ignore[assignment] |
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