##// END OF EJS Templates
libs: ext_json improvements for orjson support
super-admin -
r5073:4a99ba09 default
parent child Browse files
Show More
@@ -1,12 +1,20 b''
1 import datetime
1 import datetime
2 import decimal
2 import decimal
3 import functools
3 import functools
4 # we keep simplejson for having dump functionality still
4 import json as stdlib_json
5 # orjson doesn't support it
6 import simplejson as sjson
7
5
8 import orjson
6 try:
9 import orjson as json
7 # we keep simplejson for having dump functionality still
8 # orjson doesn't support it
9 import simplejson as sjson
10 except ImportError:
11 sjson = stdlib_json
12
13 try:
14 import orjson
15 import orjson as json
16 except ImportError:
17 json = stdlib_json
10
18
11
19
12 from rhodecode.lib.datelib import is_aware
20 from rhodecode.lib.datelib import is_aware
@@ -27,10 +35,11 b' def _obj_dump(obj):'
27
35
28 :param obj:
36 :param obj:
29 """
37 """
38
39 if isinstance(obj, set):
40 return list(obj)
30 # See "Date Time String Format" in the ECMA-262 specification.
41 # See "Date Time String Format" in the ECMA-262 specification.
31 # some code borrowed from django 1.4
42 # some code borrowed from django 1.4
32 if isinstance(obj, set):
33 return list(obj)
34 elif isinstance(obj, datetime.datetime):
43 elif isinstance(obj, datetime.datetime):
35 r = obj.isoformat()
44 r = obj.isoformat()
36 if isinstance(obj.microsecond, int):
45 if isinstance(obj.microsecond, int):
@@ -40,6 +49,8 b' def _obj_dump(obj):'
40 return r
49 return r
41 elif isinstance(obj, datetime.date):
50 elif isinstance(obj, datetime.date):
42 return obj.isoformat()
51 return obj.isoformat()
52 elif isinstance(obj, decimal.Decimal):
53 return str(obj)
43 elif isinstance(obj, datetime.time):
54 elif isinstance(obj, datetime.time):
44 if is_aware(obj):
55 if is_aware(obj):
45 raise TypeError("Time-zone aware times are not JSON serializable")
56 raise TypeError("Time-zone aware times are not JSON serializable")
@@ -52,8 +63,6 b' def _obj_dump(obj):'
52 return obj.__json__()
63 return obj.__json__()
53 else:
64 else:
54 return obj.__json__
65 return obj.__json__
55 elif isinstance(obj, decimal.Decimal):
56 return str(obj)
57 elif isinstance(obj, complex):
66 elif isinstance(obj, complex):
58 return [obj.real, obj.imag]
67 return [obj.real, obj.imag]
59 elif rhodecode and isinstance(obj, rhodecode.translation._LazyString):
68 elif rhodecode and isinstance(obj, rhodecode.translation._LazyString):
General Comments 0
You need to be logged in to leave comments. Login now