diff options
| author | Rapptz <[email protected]> | 2021-07-05 04:01:19 -0400 |
|---|---|---|
| committer | Rapptz <[email protected]> | 2021-07-07 20:19:17 -0400 |
| commit | 88d825a803917ab3e0e9797df9f47be7ed79bb9c (patch) | |
| tree | bb2022314c7b1a7159259f80809269dc8fbd27c8 /discord/utils.py | |
| parent | Type-hint backoff.py (diff) | |
| download | discord.py-88d825a803917ab3e0e9797df9f47be7ed79bb9c.tar.xz discord.py-88d825a803917ab3e0e9797df9f47be7ed79bb9c.zip | |
Allow use of orjson instead of json
The difference in speed seems negligible at start up, which is when
most time is taken for actually parsing JSON. I could potentially be
missing something but profiling didn't point to any discernable
difference.
Diffstat (limited to 'discord/utils.py')
| -rw-r--r-- | discord/utils.py | 23 |
1 files changed, 21 insertions, 2 deletions
diff --git a/discord/utils.py b/discord/utils.py index 6070882f..b5563a67 100644 --- a/discord/utils.py +++ b/discord/utils.py @@ -63,6 +63,14 @@ import warnings from .errors import InvalidArgument +try: + import orjson +except ModuleNotFoundError: + HAS_ORJSON = False +else: + HAS_ORJSON = True + + __all__ = ( 'oauth_url', 'snowflake_time', @@ -468,8 +476,19 @@ def _bytes_to_base64_data(data: bytes) -> str: return fmt.format(mime=mime, data=b64) -def to_json(obj: Any) -> str: - return json.dumps(obj, separators=(',', ':'), ensure_ascii=True) +if HAS_ORJSON: + + def to_json(obj: Any) -> str: # type: ignore + return orjson.dumps(obj).decode('utf-8') + + from_json = orjson.loads # type: ignore + +else: + + def to_json(obj: Any) -> str: + return json.dumps(obj, separators=(',', ':'), ensure_ascii=True) + + from_json = json.loads def _parse_ratelimit_header(request: Any, *, use_clock: bool = False) -> float: |