mirror of
https://github.com/Rapptz/discord.py.git
synced 2025-10-23 00:42:56 +00:00
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.
This commit is contained in:
@@ -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:
|
||||
|
Reference in New Issue
Block a user