Show More
@@ -3,8 +3,9 b' import decimal' | |||||
3 | import functools |
|
3 | import functools | |
4 | # we keep simplejson for having dump functionality still |
|
4 | # we keep simplejson for having dump functionality still | |
5 | # orjson doesn't support it |
|
5 | # orjson doesn't support it | |
|
6 | import simplejson as sjson | |||
|
7 | ||||
6 | import orjson |
|
8 | import orjson | |
7 | import simplejson |
|
|||
8 | import orjson as json |
|
9 | import orjson as json | |
9 |
|
10 | |||
10 |
|
11 | |||
@@ -26,7 +27,6 b' def _obj_dump(obj):' | |||||
26 |
|
27 | |||
27 | :param obj: |
|
28 | :param obj: | |
28 | """ |
|
29 | """ | |
29 |
|
||||
30 | # See "Date Time String Format" in the ECMA-262 specification. |
|
30 | # See "Date Time String Format" in the ECMA-262 specification. | |
31 | # some code borrowed from django 1.4 |
|
31 | # some code borrowed from django 1.4 | |
32 | if isinstance(obj, set): |
|
32 | if isinstance(obj, set): | |
@@ -62,8 +62,11 b' def _obj_dump(obj):' | |||||
62 | raise TypeError(repr(obj) + " is not JSON serializable") |
|
62 | raise TypeError(repr(obj) + " is not JSON serializable") | |
63 |
|
63 | |||
64 |
|
64 | |||
|
65 | sjson.dumps = functools.partial(sjson.dumps, default=_obj_dump) | |||
|
66 | sjson.dump = functools.partial(sjson.dump, default=_obj_dump) | |||
|
67 | ||||
65 | json.dumps = functools.partial(json.dumps, default=_obj_dump, option=orjson.OPT_NON_STR_KEYS) |
|
68 | json.dumps = functools.partial(json.dumps, default=_obj_dump, option=orjson.OPT_NON_STR_KEYS) | |
66 |
json.dump = functools.partial(s |
|
69 | json.dump = functools.partial(sjson.dump, default=_obj_dump) | |
67 |
|
70 | |||
68 |
|
71 | |||
69 | def formatted_json(*args, **kwargs): |
|
72 | def formatted_json(*args, **kwargs): |
@@ -18,15 +18,14 b'' | |||||
18 | # RhodeCode Enterprise Edition, including its added features, Support services, |
|
18 | # RhodeCode Enterprise Edition, including its added features, Support services, | |
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ |
|
19 | # and proprietary license terms, please see https://rhodecode.com/licenses/ | |
20 |
|
20 | |||
|
21 | import io | |||
21 | import datetime |
|
22 | import datetime | |
22 | import decimal |
|
23 | import decimal | |
23 | import io |
|
|||
24 | import textwrap |
|
24 | import textwrap | |
25 |
|
25 | |||
26 | import pytest |
|
26 | import pytest | |
27 |
|
27 | |||
28 |
from rhodecode.lib |
|
28 | from rhodecode.lib import ext_json | |
29 | from rhodecode.lib.ext_json import formatted_json |
|
|||
30 | from rhodecode.translation import _, _pluralize |
|
29 | from rhodecode.translation import _, _pluralize | |
31 |
|
30 | |||
32 |
|
31 | |||
@@ -38,87 +37,89 b' class Timezone(datetime.tzinfo):' | |||||
38 | return datetime.timedelta(hours=self.hours) |
|
37 | return datetime.timedelta(hours=self.hours) | |
39 |
|
38 | |||
40 |
|
39 | |||
|
40 | class SerializableObject(object): | |||
|
41 | def __json__(self): | |||
|
42 | return 'foo' | |||
|
43 | ||||
|
44 | ||||
41 | def test_dumps_set(): |
|
45 | def test_dumps_set(): | |
42 | result = json.dumps(set((1, 2, 3))) |
|
46 | result = ext_json.json.dumps(set((1, 2, 3))) | |
43 | # We cannot infer what the order of result is going to be |
|
47 | # We cannot infer what the order of result is going to be | |
44 | result = json.loads(result) |
|
48 | result = ext_json.json.loads(result) | |
45 | assert isinstance(result, list) |
|
49 | assert isinstance(result, list) | |
46 | assert [1, 2, 3] == sorted(result) |
|
50 | assert [1, 2, 3] == sorted(result) | |
47 |
|
51 | |||
48 |
|
52 | |||
49 | def test_dumps_decimal(): |
|
53 | def test_dumps_decimal(): | |
50 | assert '"1.5"' == json.dumps(decimal.Decimal('1.5')) |
|
54 | assert b'"1.5"' == ext_json.json.dumps(decimal.Decimal('1.5')) | |
51 |
|
55 | |||
52 |
|
56 | |||
53 | def test_dumps_complex(): |
|
57 | def test_dumps_complex(): | |
54 |
assert "[0.0, |
|
58 | assert b"[0.0,1.0]" == ext_json.json.dumps(1j) | |
55 |
assert "[1.0, |
|
59 | assert b"[1.0,0.0]" == ext_json.json.dumps(1 + 0j) | |
56 |
assert "[1.1, |
|
60 | assert b"[1.1,1.2]" == ext_json.json.dumps(1.1 + 1.2j) | |
57 |
|
61 | |||
58 |
|
62 | |||
59 | def test_dumps_object_with_json_method(): |
|
63 | def test_dumps_object_with_json_method(): | |
60 | class SerializableObject(object): |
|
64 | assert '"foo"' == ext_json.str_json(SerializableObject()) | |
61 | def __json__(self): |
|
|||
62 | return 'foo' |
|
|||
63 |
|
||||
64 | assert '"foo"' == json.dumps(SerializableObject()) |
|
|||
65 |
|
65 | |||
66 |
|
66 | |||
67 | def test_dumps_object_with_json_attribute(): |
|
67 | def test_dumps_object_with_json_attribute(): | |
68 | class SerializableObject(object): |
|
|||
69 | __json__ = 'foo' |
|
|||
70 |
|
68 | |||
71 |
assert '"foo"' == json. |
|
69 | assert '"foo"' == ext_json.str_json(SerializableObject()) | |
72 |
|
70 | |||
73 |
|
71 | |||
74 | def test_dumps_time(): |
|
72 | def test_dumps_time(): | |
75 |
assert '"03:14:15.926"' == json. |
|
73 | assert '"03:14:15.926535"' == ext_json.str_json(datetime.time(3, 14, 15, 926535)) | |
76 |
|
74 | |||
77 |
|
75 | |||
78 | def test_dumps_time_no_microseconds(): |
|
76 | def test_dumps_time_no_microseconds(): | |
79 |
assert '"03:14:15"' == json. |
|
77 | assert '"03:14:15"' == ext_json.str_json(datetime.time(3, 14, 15)) | |
80 |
|
78 | |||
81 |
|
79 | |||
82 | def test_dumps_time_with_timezone(): |
|
80 | def test_dumps_time_with_timezone(): | |
83 | with pytest.raises(TypeError) as excinfo: |
|
81 | with pytest.raises(TypeError) as excinfo: | |
84 | json.dumps(datetime.time(3, 14, 15, 926535, Timezone(0))) |
|
82 | ext_json.json.dumps(datetime.time(3, 14, 15, 926535, Timezone(0))) | |
85 |
|
83 | |||
86 | error_msg = str(excinfo.value) |
|
84 | error_msg = str(excinfo.value) | |
87 | assert 'Time-zone aware times are not JSON serializable' in error_msg |
|
85 | ||
|
86 | assert 'timezone library is not supported' in error_msg | |||
|
87 | # only for simplejson | |||
|
88 | #assert 'Time-zone aware times are not JSON serializable' in error_msg | |||
88 |
|
89 | |||
89 |
|
90 | |||
90 | def test_dumps_date(): |
|
91 | def test_dumps_date(): | |
91 | assert '"1969-07-20"' == json.dumps(datetime.date(1969, 7, 20)) |
|
92 | assert b'"1969-07-20"' == ext_json.json.dumps(datetime.date(1969, 7, 20)) | |
92 |
|
93 | |||
93 |
|
94 | |||
94 | def test_dumps_datetime(): |
|
95 | def test_dumps_datetime(): | |
95 | json_data = json.dumps(datetime.datetime(1969, 7, 20, 3, 14, 15, 926535)) |
|
96 | json_data = ext_json.json.dumps(datetime.datetime(1969, 7, 20, 3, 14, 15, 926535)) | |
96 | assert '"1969-07-20T03:14:15.926"' == json_data |
|
97 | assert b'"1969-07-20T03:14:15.926535"' == json_data | |
97 |
|
98 | |||
98 |
|
99 | |||
99 | def test_dumps_datetime_no_microseconds(): |
|
100 | def test_dumps_datetime_no_microseconds(): | |
100 | json_data = json.dumps(datetime.datetime(1969, 7, 20, 3, 14, 15)) |
|
101 | json_data = ext_json.json.dumps(datetime.datetime(1969, 7, 20, 3, 14, 15)) | |
101 | assert '"1969-07-20T03:14:15"' == json_data |
|
102 | assert b'"1969-07-20T03:14:15"' == json_data | |
102 |
|
103 | |||
103 |
|
104 | |||
104 | def test_dumps_datetime_with_utc_timezone(): |
|
105 | def test_dumps_datetime_with_utc_timezone(): | |
105 | json_data = json.dumps( |
|
106 | json_data = ext_json.json.dumps( | |
106 | datetime.datetime(1969, 7, 20, 3, 14, 15, 926535, Timezone(0))) |
|
107 | datetime.datetime(1969, 7, 20, 3, 14, 15, 926535, Timezone(0))) | |
107 |
assert '"1969-07-20T03:14:15.926 |
|
108 | assert b'"1969-07-20T03:14:15.926535+00:00"' == json_data | |
108 |
|
109 | |||
109 |
|
110 | |||
110 | def test_dumps_datetime_with_plus1_timezone(): |
|
111 | def test_dumps_datetime_with_plus1_timezone(): | |
111 | json_data = json.dumps( |
|
112 | json_data = ext_json.json.dumps( | |
112 | datetime.datetime(1969, 7, 20, 3, 14, 15, 926535, Timezone(1))) |
|
113 | datetime.datetime(1969, 7, 20, 3, 14, 15, 926535, Timezone(1))) | |
113 | assert '"1969-07-20T03:14:15.926+01:00"' == json_data |
|
114 | assert b'"1969-07-20T03:14:15.926535+01:00"' == json_data | |
114 |
|
115 | |||
115 |
|
116 | |||
116 | def test_dumps_unserializable_class(): |
|
117 | def test_dumps_unserializable_class(): | |
117 | unserializable_obj = object() |
|
118 | unserializable_obj = object() | |
118 | with pytest.raises(TypeError) as excinfo: |
|
119 | with pytest.raises(TypeError) as excinfo: | |
119 | json.dumps(unserializable_obj) |
|
120 | ext_json.json.dumps(unserializable_obj) | |
120 |
|
121 | |||
121 |
assert |
|
122 | assert 'object' in str(excinfo.value) | |
122 | assert 'is not JSON serializable' in str(excinfo.value) |
|
123 | assert 'is not JSON serializable' in str(excinfo.value) | |
123 |
|
124 | |||
124 |
|
125 | |||
@@ -131,10 +132,10 b' def test_dump_is_like_dumps():' | |||||
131 | 'time': datetime.time(3, 14, 15, 926535), |
|
132 | 'time': datetime.time(3, 14, 15, 926535), | |
132 | 'date': datetime.date(1969, 7, 20), |
|
133 | 'date': datetime.date(1969, 7, 20), | |
133 | } |
|
134 | } | |
134 | json_buffer = io.BytesIO() |
|
135 | json_buffer = io.StringIO() # StringIO because dump uses simplejson not orjson | |
135 | json.dump(data, json_buffer) |
|
136 | ext_json.json.dump(data, json_buffer) | |
136 |
|
137 | |||
137 | assert json.dumps(data) == json_buffer.getvalue() |
|
138 | assert ext_json.sjson.dumps(data) == json_buffer.getvalue() | |
138 |
|
139 | |||
139 |
|
140 | |||
140 | def test_formatted_json(): |
|
141 | def test_formatted_json(): | |
@@ -155,12 +156,12 b' def test_formatted_json():' | |||||
155 |
|
|
156 | } | |
156 | }''').strip() |
|
157 | }''').strip() | |
157 |
|
158 | |||
158 | assert formatted_json(data) == expected_data |
|
159 | assert expected_data == ext_json.formatted_str_json(data) | |
159 |
|
160 | |||
160 |
|
161 | |||
161 | def test_lazy_translation_string(baseapp): |
|
162 | def test_lazy_translation_string(baseapp): | |
162 | data = {'label': _('hello')} |
|
163 | data = {'label': _('hello')} | |
163 | data2 = {'label2': _pluralize('singular', 'plural', 1)} |
|
164 | data2 = {'label2': _pluralize('singular', 'plural', 1)} | |
164 |
|
165 | |||
165 | assert json.dumps(data) == '{"label": "hello"}' |
|
166 | assert b'{"label":"hello"}' == ext_json.json.dumps(data) | |
166 | assert json.dumps(data2) == '{"label2": "singular"}' |
|
167 | assert b'{"label2":"singular"}' == ext_json.json.dumps(data2) |
General Comments 0
You need to be logged in to leave comments.
Login now