##// END OF EJS Templates
fix(imports): fixed circular import problem
fix(imports): fixed circular import problem

File last commit:

r5073:4a99ba09 default
r5341:115837d2 tip default
Show More
ext_json.py
95 lines | 2.7 KiB | text/x-python | PythonLexer
project: added all source files and assets
r1 import datetime
import decimal
import functools
libs: ext_json improvements for orjson support
r5073 import json as stdlib_json
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012
libs: ext_json improvements for orjson support
r5073 try:
# we keep simplejson for having dump functionality still
# orjson doesn't support it
import simplejson as sjson
except ImportError:
sjson = stdlib_json
try:
import orjson
import orjson as json
except ImportError:
json = stdlib_json
project: added all source files and assets
r1
from rhodecode.lib.datelib import is_aware
deps: replaced simplejson with much faster orjson implementation
r4972 from rhodecode.lib.str_utils import safe_str
project: added all source files and assets
r1
try:
pylons: remove pylons as dependency...
r2351 import rhodecode.translation
project: added all source files and assets
r1 except ImportError:
pylons: remove pylons as dependency...
r2351 rhodecode = None
project: added all source files and assets
r1
__all__ = ['json']
def _obj_dump(obj):
"""
Custom function for dumping objects to JSON, if obj has __json__ attribute
or method defined it will be used for serialization
:param obj:
"""
libs: ext_json improvements for orjson support
r5073
if isinstance(obj, set):
return list(obj)
project: added all source files and assets
r1 # See "Date Time String Format" in the ECMA-262 specification.
# some code borrowed from django 1.4
elif isinstance(obj, datetime.datetime):
r = obj.isoformat()
python3: fix usage of int/long
r4935 if isinstance(obj.microsecond, int):
project: added all source files and assets
r1 r = r[:23] + r[26:]
if r.endswith('+00:00'):
r = r[:-6] + 'Z'
return r
elif isinstance(obj, datetime.date):
return obj.isoformat()
libs: ext_json improvements for orjson support
r5073 elif isinstance(obj, decimal.Decimal):
return str(obj)
project: added all source files and assets
r1 elif isinstance(obj, datetime.time):
if is_aware(obj):
raise TypeError("Time-zone aware times are not JSON serializable")
r = obj.isoformat()
python3: fix usage of int/long
r4935 if isinstance(obj.microsecond, int):
project: added all source files and assets
r1 r = r[:12]
return r
elif hasattr(obj, '__json__'):
if callable(obj.__json__):
return obj.__json__()
else:
return obj.__json__
elif isinstance(obj, complex):
return [obj.real, obj.imag]
pylons: fixed code and test suite after removal of pylons.
r2358 elif rhodecode and isinstance(obj, rhodecode.translation._LazyString):
project: added all source files and assets
r1 return obj.eval()
else:
raise TypeError(repr(obj) + " is not JSON serializable")
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 sjson.dumps = functools.partial(sjson.dumps, default=_obj_dump)
sjson.dump = functools.partial(sjson.dump, default=_obj_dump)
deps: replaced simplejson with much faster orjson implementation
r4972 json.dumps = functools.partial(json.dumps, default=_obj_dump, option=orjson.OPT_NON_STR_KEYS)
ext-json: fixed tests and made the lib more consistent with orjson vs simplejson
r5012 json.dump = functools.partial(sjson.dump, default=_obj_dump)
deps: replaced simplejson with much faster orjson implementation
r4972
def formatted_json(*args, **kwargs):
# alias for formatted json
opts = orjson.OPT_NON_STR_KEYS | orjson.OPT_INDENT_2 | orjson.OPT_SORT_KEYS
return functools.partial(json.dumps, option=opts)(*args, **kwargs)
project: added all source files and assets
r1
deps: replaced simplejson with much faster orjson implementation
r4972 def formatted_str_json(*args, **kwargs):
opts = orjson.OPT_NON_STR_KEYS | orjson.OPT_INDENT_2 | orjson.OPT_SORT_KEYS
closure = functools.partial(json.dumps, option=opts)
return safe_str(closure(*args, **kwargs))
def str_json(*args, **kwargs):
closure = functools.partial(json.dumps)
return safe_str(closure(*args, **kwargs))