-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor/pick a faster json lib (#1152)
* use rapidjson instead * use orjson instead * fix tests; relocate some report properties * fix pyproject.toml * add newsfrag; some tweaks
- Loading branch information
Showing
20 changed files
with
405 additions
and
385 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 @@ | ||
Use a new JSON library ``orjson`` to improve performance when using Python 3.8 or later versions. |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import json | ||
from pathlib import Path | ||
from typing import Union | ||
|
||
_USE_ORJSON = False | ||
|
||
try: | ||
import orjson | ||
except ImportError: | ||
pass | ||
else: | ||
_USE_ORJSON = True | ||
|
||
|
||
def json_loads(data: str): | ||
if _USE_ORJSON: | ||
return orjson.loads(data) | ||
else: | ||
return json.loads(data) | ||
|
||
|
||
def json_dumps(data, indent_2=False, default=None) -> str: | ||
if _USE_ORJSON: | ||
return orjson.dumps( | ||
data, | ||
default=default, | ||
option=orjson.OPT_INDENT_2 if indent_2 else 0, | ||
).decode() | ||
else: | ||
if default: | ||
|
||
class _E(json.JSONEncoder): | ||
def default(self, o): | ||
return default(o) | ||
|
||
else: | ||
_E = None | ||
return json.dumps(data, cls=_E, indent=2 if indent_2 else None) | ||
|
||
|
||
def json_load_from_path(path: Union[str, Path]) -> dict: | ||
with open(path) as fp: | ||
return json_loads(fp.read()) |
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
Oops, something went wrong.