libs: ext_json improvements for orjson support
This commit is contained in:
parent
c231b4c990
commit
59457072f4
1 changed files with 18 additions and 9 deletions
|
|
@ -1,12 +1,20 @@
|
|||
import datetime
|
||||
import decimal
|
||||
import functools
|
||||
# we keep simplejson for having dump functionality still
|
||||
# orjson doesn't support it
|
||||
import simplejson as sjson
|
||||
import json as stdlib_json
|
||||
|
||||
import orjson
|
||||
import orjson as json
|
||||
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
|
||||
|
||||
|
||||
from rhodecode.lib.datelib import is_aware
|
||||
|
|
@ -27,10 +35,11 @@ def _obj_dump(obj):
|
|||
|
||||
:param obj:
|
||||
"""
|
||||
# See "Date Time String Format" in the ECMA-262 specification.
|
||||
# some code borrowed from django 1.4
|
||||
|
||||
if isinstance(obj, set):
|
||||
return list(obj)
|
||||
# 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()
|
||||
if isinstance(obj.microsecond, int):
|
||||
|
|
@ -40,6 +49,8 @@ def _obj_dump(obj):
|
|||
return r
|
||||
elif isinstance(obj, datetime.date):
|
||||
return obj.isoformat()
|
||||
elif isinstance(obj, decimal.Decimal):
|
||||
return str(obj)
|
||||
elif isinstance(obj, datetime.time):
|
||||
if is_aware(obj):
|
||||
raise TypeError("Time-zone aware times are not JSON serializable")
|
||||
|
|
@ -52,8 +63,6 @@ def _obj_dump(obj):
|
|||
return obj.__json__()
|
||||
else:
|
||||
return obj.__json__
|
||||
elif isinstance(obj, decimal.Decimal):
|
||||
return str(obj)
|
||||
elif isinstance(obj, complex):
|
||||
return [obj.real, obj.imag]
|
||||
elif rhodecode and isinstance(obj, rhodecode.translation._LazyString):
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue