mirror of
https://github.com/aiogram/aiogram.git
synced 2025-12-09 17:33:44 +00:00
Allow to disable ujson
This commit is contained in:
parent
00400ea88b
commit
f3c39ed9f1
2 changed files with 17 additions and 6 deletions
|
|
@ -1,9 +1,9 @@
|
||||||
import io
|
import io
|
||||||
import typing
|
import typing
|
||||||
import ujson
|
|
||||||
from typing import TypeVar
|
from typing import TypeVar
|
||||||
|
|
||||||
from .fields import BaseField
|
from .fields import BaseField
|
||||||
|
from ..utils import json
|
||||||
from ..utils.context import get_value
|
from ..utils.context import get_value
|
||||||
|
|
||||||
PROPS_ATTR_NAME = '_props'
|
PROPS_ATTR_NAME = '_props'
|
||||||
|
|
@ -165,7 +165,7 @@ class TelegramObject(metaclass=MetaTelegramObject):
|
||||||
:return: JSON
|
:return: JSON
|
||||||
:rtype: :obj:`str`
|
:rtype: :obj:`str`
|
||||||
"""
|
"""
|
||||||
return ujson.dumps(self.to_python())
|
return json.dumps(self.to_python())
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def create(cls, *args, **kwargs):
|
def create(cls, *args, **kwargs):
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,27 @@
|
||||||
|
import json
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import ujson as json
|
import ujson
|
||||||
|
|
||||||
IS_UJSON = True
|
_UJSON_IS_AVAILABLE = True
|
||||||
except ImportError:
|
except ImportError:
|
||||||
import json
|
_UJSON_IS_AVAILABLE = False
|
||||||
|
|
||||||
IS_UJSON = False
|
_use_ujson = _UJSON_IS_AVAILABLE
|
||||||
|
|
||||||
|
|
||||||
|
def disable_ujson():
|
||||||
|
global _use_ujson
|
||||||
|
_use_ujson = False
|
||||||
|
|
||||||
|
|
||||||
def dumps(data):
|
def dumps(data):
|
||||||
|
if _use_ujson:
|
||||||
|
return ujson.dumps(data)
|
||||||
return json.dumps(data)
|
return json.dumps(data)
|
||||||
|
|
||||||
|
|
||||||
def loads(data):
|
def loads(data):
|
||||||
|
if _use_ujson:
|
||||||
|
return ujson.loads(data)
|
||||||
return json.loads(data)
|
return json.loads(data)
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue