From dbe2a9bb667d258d7eff0d878cb401ec2da56d8c Mon Sep 17 00:00:00 2001 From: grey-cat-1908 Date: Tue, 22 Mar 2022 16:47:06 +0300 Subject: [PATCH] add orjson support --- melisa/core/gateway.py | 3 +-- melisa/utils/json.py | 26 ++++++++++++++++++++++++++ packages/speed.txt | 1 + 3 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 melisa/utils/json.py create mode 100644 packages/speed.txt diff --git a/melisa/core/gateway.py b/melisa/core/gateway.py index eb0ec26..664781d 100644 --- a/melisa/core/gateway.py +++ b/melisa/core/gateway.py @@ -1,7 +1,6 @@ # Copyright MelisaDev 2022 - Present # Full MIT License can be found in `LICENSE.txt` at the project root. -import json import asyncio import sys import zlib @@ -15,7 +14,7 @@ import aiohttp from ..exceptions import GatewayError, PrivilegedIntentsRequired, LoginFailure from ..listeners import listeners from ..models.user import BotActivity -from ..utils import APIModelBase +from ..utils import APIModelBase, json @dataclass diff --git a/melisa/utils/json.py b/melisa/utils/json.py new file mode 100644 index 0000000..aa0e8d6 --- /dev/null +++ b/melisa/utils/json.py @@ -0,0 +1,26 @@ +# Copyright MelisaDev 2022 - Present +# Full MIT License can be found in `LICENSE.txt` at the project root. + +from __future__ import annotations + +from typing import Any + +try: + import orjson +except ModuleNotFoundError: + import json + + HAS_ORJSON = False +else: + HAS_ORJSON = True + +if HAS_ORJSON: + def dumps(obj: Any) -> str: + return orjson.dumps(obj).decode('utf-8') + + loads = orjson.loads +else: + def dumps(obj: Any) -> str: + return json.dumps(obj, separators=(',', ':'), ensure_ascii=True) + + loads = json.loads diff --git a/packages/speed.txt b/packages/speed.txt new file mode 100644 index 0000000..d56bbbd --- /dev/null +++ b/packages/speed.txt @@ -0,0 +1 @@ +orjson==3.6.7 \ No newline at end of file