Show More
@@ -1,86 +1,95 b'' | |||
|
1 | 1 | import datetime |
|
2 | 2 | import decimal |
|
3 | 3 | import functools |
|
4 | # we keep simplejson for having dump functionality still | |
|
5 | # orjson doesn't support it | |
|
6 | import simplejson as sjson | |
|
4 | import json as stdlib_json | |
|
7 | 5 | |
|
8 | import orjson | |
|
9 | import orjson as json | |
|
6 | try: | |
|
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 | 20 | from rhodecode.lib.datelib import is_aware |
|
13 | 21 | from rhodecode.lib.str_utils import safe_str |
|
14 | 22 | |
|
15 | 23 | try: |
|
16 | 24 | import rhodecode.translation |
|
17 | 25 | except ImportError: |
|
18 | 26 | rhodecode = None |
|
19 | 27 | |
|
20 | 28 | __all__ = ['json'] |
|
21 | 29 | |
|
22 | 30 | |
|
23 | 31 | def _obj_dump(obj): |
|
24 | 32 | """ |
|
25 | 33 | Custom function for dumping objects to JSON, if obj has __json__ attribute |
|
26 | 34 | or method defined it will be used for serialization |
|
27 | 35 | |
|
28 | 36 | :param obj: |
|
29 | 37 | """ |
|
38 | ||
|
39 | if isinstance(obj, set): | |
|
40 | return list(obj) | |
|
30 | 41 | # See "Date Time String Format" in the ECMA-262 specification. |
|
31 | 42 | # some code borrowed from django 1.4 |
|
32 | if isinstance(obj, set): | |
|
33 | return list(obj) | |
|
34 | 43 | elif isinstance(obj, datetime.datetime): |
|
35 | 44 | r = obj.isoformat() |
|
36 | 45 | if isinstance(obj.microsecond, int): |
|
37 | 46 | r = r[:23] + r[26:] |
|
38 | 47 | if r.endswith('+00:00'): |
|
39 | 48 | r = r[:-6] + 'Z' |
|
40 | 49 | return r |
|
41 | 50 | elif isinstance(obj, datetime.date): |
|
42 | 51 | return obj.isoformat() |
|
52 | elif isinstance(obj, decimal.Decimal): | |
|
53 | return str(obj) | |
|
43 | 54 | elif isinstance(obj, datetime.time): |
|
44 | 55 | if is_aware(obj): |
|
45 | 56 | raise TypeError("Time-zone aware times are not JSON serializable") |
|
46 | 57 | r = obj.isoformat() |
|
47 | 58 | if isinstance(obj.microsecond, int): |
|
48 | 59 | r = r[:12] |
|
49 | 60 | return r |
|
50 | 61 | elif hasattr(obj, '__json__'): |
|
51 | 62 | if callable(obj.__json__): |
|
52 | 63 | return obj.__json__() |
|
53 | 64 | else: |
|
54 | 65 | return obj.__json__ |
|
55 | elif isinstance(obj, decimal.Decimal): | |
|
56 | return str(obj) | |
|
57 | 66 | elif isinstance(obj, complex): |
|
58 | 67 | return [obj.real, obj.imag] |
|
59 | 68 | elif rhodecode and isinstance(obj, rhodecode.translation._LazyString): |
|
60 | 69 | return obj.eval() |
|
61 | 70 | else: |
|
62 | 71 | raise TypeError(repr(obj) + " is not JSON serializable") |
|
63 | 72 | |
|
64 | 73 | |
|
65 | 74 | sjson.dumps = functools.partial(sjson.dumps, default=_obj_dump) |
|
66 | 75 | sjson.dump = functools.partial(sjson.dump, default=_obj_dump) |
|
67 | 76 | |
|
68 | 77 | json.dumps = functools.partial(json.dumps, default=_obj_dump, option=orjson.OPT_NON_STR_KEYS) |
|
69 | 78 | json.dump = functools.partial(sjson.dump, default=_obj_dump) |
|
70 | 79 | |
|
71 | 80 | |
|
72 | 81 | def formatted_json(*args, **kwargs): |
|
73 | 82 | # alias for formatted json |
|
74 | 83 | opts = orjson.OPT_NON_STR_KEYS | orjson.OPT_INDENT_2 | orjson.OPT_SORT_KEYS |
|
75 | 84 | return functools.partial(json.dumps, option=opts)(*args, **kwargs) |
|
76 | 85 | |
|
77 | 86 | |
|
78 | 87 | def formatted_str_json(*args, **kwargs): |
|
79 | 88 | opts = orjson.OPT_NON_STR_KEYS | orjson.OPT_INDENT_2 | orjson.OPT_SORT_KEYS |
|
80 | 89 | closure = functools.partial(json.dumps, option=opts) |
|
81 | 90 | return safe_str(closure(*args, **kwargs)) |
|
82 | 91 | |
|
83 | 92 | |
|
84 | 93 | def str_json(*args, **kwargs): |
|
85 | 94 | closure = functools.partial(json.dumps) |
|
86 | 95 | return safe_str(closure(*args, **kwargs)) |
General Comments 0
You need to be logged in to leave comments.
Login now