Allow to disable ujson

This commit is contained in:
Alex Root Junior 2017-10-20 11:11:01 +03:00
parent 00400ea88b
commit f3c39ed9f1
2 changed files with 17 additions and 6 deletions

View file

@ -1,9 +1,9 @@
import io
import typing
import ujson
from typing import TypeVar
from .fields import BaseField
from ..utils import json
from ..utils.context import get_value
PROPS_ATTR_NAME = '_props'
@ -165,7 +165,7 @@ class TelegramObject(metaclass=MetaTelegramObject):
:return: JSON
:rtype: :obj:`str`
"""
return ujson.dumps(self.to_python())
return json.dumps(self.to_python())
@classmethod
def create(cls, *args, **kwargs):

View file

@ -1,16 +1,27 @@
import json
try:
import ujson as json
import ujson
IS_UJSON = True
_UJSON_IS_AVAILABLE = True
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):
if _use_ujson:
return ujson.dumps(data)
return json.dumps(data)
def loads(data):
if _use_ujson:
return ujson.loads(data)
return json.loads(data)