Show More
@@ -0,0 +1,104 b'' | |||||
|
1 | import datetime | |||
|
2 | import functools | |||
|
3 | import decimal | |||
|
4 | ||||
|
5 | __all__ = ['json', 'simplejson', 'stdjson'] | |||
|
6 | ||||
|
7 | ||||
|
8 | def _is_aware(value): | |||
|
9 | """ | |||
|
10 | Determines if a given datetime.time is aware. | |||
|
11 | ||||
|
12 | The logic is described in Python's docs: | |||
|
13 | http://docs.python.org/library/datetime.html#datetime.tzinfo | |||
|
14 | """ | |||
|
15 | return (value.tzinfo is not None | |||
|
16 | and value.tzinfo.utcoffset(value) is not None) | |||
|
17 | ||||
|
18 | ||||
|
19 | def _obj_dump(obj): | |||
|
20 | """ | |||
|
21 | Custom function for dumping objects to JSON, if obj has __json__ attribute | |||
|
22 | or method defined it will be used for serialization | |||
|
23 | ||||
|
24 | :param obj: | |||
|
25 | """ | |||
|
26 | ||||
|
27 | if isinstance(obj, complex): | |||
|
28 | return [obj.real, obj.imag] | |||
|
29 | # See "Date Time String Format" in the ECMA-262 specification. | |||
|
30 | # some code borrowed from django 1.4 | |||
|
31 | elif isinstance(obj, datetime.datetime): | |||
|
32 | r = obj.isoformat() | |||
|
33 | if obj.microsecond: | |||
|
34 | r = r[:23] + r[26:] | |||
|
35 | if r.endswith('+00:00'): | |||
|
36 | r = r[:-6] + 'Z' | |||
|
37 | return r | |||
|
38 | elif isinstance(obj, datetime.date): | |||
|
39 | return obj.isoformat() | |||
|
40 | elif isinstance(obj, decimal.Decimal): | |||
|
41 | return str(obj) | |||
|
42 | elif isinstance(obj, datetime.time): | |||
|
43 | if _is_aware(obj): | |||
|
44 | raise ValueError("JSON can't represent timezone-aware times.") | |||
|
45 | r = obj.isoformat() | |||
|
46 | if obj.microsecond: | |||
|
47 | r = r[:12] | |||
|
48 | return r | |||
|
49 | elif isinstance(obj, set): | |||
|
50 | return list(obj) | |||
|
51 | elif hasattr(obj, '__json__'): | |||
|
52 | if callable(obj.__json__): | |||
|
53 | return obj.__json__() | |||
|
54 | else: | |||
|
55 | return obj.__json__ | |||
|
56 | else: | |||
|
57 | raise NotImplementedError | |||
|
58 | ||||
|
59 | ||||
|
60 | # Import simplejson | |||
|
61 | try: | |||
|
62 | # import simplejson initially | |||
|
63 | import simplejson as _sj | |||
|
64 | ||||
|
65 | def extended_encode(obj): | |||
|
66 | try: | |||
|
67 | return _obj_dump(obj) | |||
|
68 | except NotImplementedError: | |||
|
69 | pass | |||
|
70 | raise TypeError("%r is not JSON serializable" % (obj,)) | |||
|
71 | # we handle decimals our own it makes unified behavior of json vs | |||
|
72 | # simplejson | |||
|
73 | _sj.dumps = functools.partial(_sj.dumps, default=extended_encode, | |||
|
74 | use_decimal=False) | |||
|
75 | _sj.dump = functools.partial(_sj.dump, default=extended_encode, | |||
|
76 | use_decimal=False) | |||
|
77 | simplejson = _sj | |||
|
78 | ||||
|
79 | except ImportError: | |||
|
80 | # no simplejson set it to None | |||
|
81 | _sj = None | |||
|
82 | ||||
|
83 | ||||
|
84 | # simplejson not found try out regular json module | |||
|
85 | import json as _json | |||
|
86 | ||||
|
87 | ||||
|
88 | # extended JSON encoder for json | |||
|
89 | class ExtendedEncoder(_json.JSONEncoder): | |||
|
90 | def default(self, obj): | |||
|
91 | try: | |||
|
92 | return _obj_dump(obj) | |||
|
93 | except NotImplementedError: | |||
|
94 | pass | |||
|
95 | return _json.JSONEncoder.default(self, obj) | |||
|
96 | # monkey-patch JSON encoder to use extended version | |||
|
97 | _json.dumps = functools.partial(_json.dumps, cls=ExtendedEncoder) | |||
|
98 | _json.dump = functools.partial(_json.dump, cls=ExtendedEncoder) | |||
|
99 | stdlib = _json | |||
|
100 | ||||
|
101 | # set all available json modules | |||
|
102 | simplejson = _sj | |||
|
103 | stdjson = _json | |||
|
104 | json = _sj if _sj else _json |
@@ -25,92 +25,12 b'' | |||||
25 | # along with this program. If not, see <http://www.gnu.org/licenses/>. |
|
25 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | |
26 |
|
26 | |||
27 | import os |
|
27 | import os | |
28 | import datetime |
|
|||
29 | import functools |
|
|||
30 | import decimal |
|
|||
31 | from rhodecode import __platform__, PLATFORM_WIN |
|
28 | from rhodecode import __platform__, PLATFORM_WIN | |
32 |
|
29 | |||
33 | #============================================================================== |
|
30 | #============================================================================== | |
34 | # json |
|
31 | # json | |
35 | #============================================================================== |
|
32 | #============================================================================== | |
36 |
|
33 | from rhodecode.lib.ext_json import json | ||
37 |
|
||||
38 | def _is_aware(value): |
|
|||
39 | """ |
|
|||
40 | Determines if a given datetime.time is aware. |
|
|||
41 |
|
||||
42 | The logic is described in Python's docs: |
|
|||
43 | http://docs.python.org/library/datetime.html#datetime.tzinfo |
|
|||
44 | """ |
|
|||
45 | return (value.tzinfo is not None |
|
|||
46 | and value.tzinfo.utcoffset(value) is not None) |
|
|||
47 |
|
||||
48 |
|
||||
49 | def _obj_dump(obj): |
|
|||
50 | """ |
|
|||
51 | Custom function for dumping objects to JSON, if obj has __json__ attribute |
|
|||
52 | or method defined it will be used for serialization |
|
|||
53 |
|
||||
54 | :param obj: |
|
|||
55 | """ |
|
|||
56 |
|
||||
57 | if isinstance(obj, complex): |
|
|||
58 | return [obj.real, obj.imag] |
|
|||
59 | # See "Date Time String Format" in the ECMA-262 specification. |
|
|||
60 | # some code borrowed from django 1.4 |
|
|||
61 | elif isinstance(obj, datetime.datetime): |
|
|||
62 | r = obj.isoformat() |
|
|||
63 | if obj.microsecond: |
|
|||
64 | r = r[:23] + r[26:] |
|
|||
65 | if r.endswith('+00:00'): |
|
|||
66 | r = r[:-6] + 'Z' |
|
|||
67 | return r |
|
|||
68 | elif isinstance(obj, datetime.date): |
|
|||
69 | return obj.isoformat() |
|
|||
70 | elif isinstance(obj, decimal.Decimal): |
|
|||
71 | return str(obj) |
|
|||
72 | elif isinstance(obj, datetime.time): |
|
|||
73 | if _is_aware(obj): |
|
|||
74 | raise ValueError("JSON can't represent timezone-aware times.") |
|
|||
75 | r = obj.isoformat() |
|
|||
76 | if obj.microsecond: |
|
|||
77 | r = r[:12] |
|
|||
78 | return r |
|
|||
79 | elif isinstance(obj, set): |
|
|||
80 | return list(obj) |
|
|||
81 | elif isinstance(obj, OrderedDict): |
|
|||
82 | return obj.as_dict() |
|
|||
83 | elif hasattr(obj, '__json__'): |
|
|||
84 | if callable(obj.__json__): |
|
|||
85 | return obj.__json__() |
|
|||
86 | else: |
|
|||
87 | return obj.__json__ |
|
|||
88 | else: |
|
|||
89 | raise NotImplementedError |
|
|||
90 |
|
||||
91 | try: |
|
|||
92 | import json |
|
|||
93 |
|
||||
94 | # extended JSON encoder for json |
|
|||
95 | class ExtendedEncoder(json.JSONEncoder): |
|
|||
96 | def default(self, obj): |
|
|||
97 | try: |
|
|||
98 | return _obj_dump(obj) |
|
|||
99 | except NotImplementedError: |
|
|||
100 | pass |
|
|||
101 | return json.JSONEncoder.default(self, obj) |
|
|||
102 | # monkey-patch JSON encoder to use extended version |
|
|||
103 | json.dumps = functools.partial(json.dumps, cls=ExtendedEncoder) |
|
|||
104 | except ImportError: |
|
|||
105 | import simplejson as json |
|
|||
106 |
|
||||
107 | def extended_encode(obj): |
|
|||
108 | try: |
|
|||
109 | return _obj_dump(obj) |
|
|||
110 | except NotImplementedError: |
|
|||
111 | pass |
|
|||
112 | raise TypeError("%r is not JSON serializable" % (obj,)) |
|
|||
113 | json.dumps = functools.partial(json.dumps, default=extended_encode) |
|
|||
114 |
|
34 | |||
115 |
|
35 | |||
116 | #============================================================================== |
|
36 | #============================================================================== |
General Comments 0
You need to be logged in to leave comments.
Login now