Show More
@@ -1,16 +1,8 b'' | |||||
1 | """Utilities to manipulate JSON objects. |
|
1 | """Utilities to manipulate JSON objects.""" | |
2 | """ |
|
2 | ||
3 | #----------------------------------------------------------------------------- |
|
3 | # Copyright (c) IPython Development Team. | |
4 | # Copyright (C) 2010-2011 The IPython Development Team |
|
4 | # Distributed under the terms of the Modified BSD License. | |
5 | # |
|
|||
6 | # Distributed under the terms of the BSD License. The full license is in |
|
|||
7 | # the file COPYING.txt, distributed as part of this software. |
|
|||
8 | #----------------------------------------------------------------------------- |
|
|||
9 |
|
5 | |||
10 | #----------------------------------------------------------------------------- |
|
|||
11 | # Imports |
|
|||
12 | #----------------------------------------------------------------------------- |
|
|||
13 | # stdlib |
|
|||
14 | import math |
|
6 | import math | |
15 | import re |
|
7 | import re | |
16 | import types |
|
8 | import types | |
@@ -197,18 +189,6 b' def json_clean(obj):' | |||||
197 | encoded as JSON. Note that this function does not *encode* its inputs, |
|
189 | encoded as JSON. Note that this function does not *encode* its inputs, | |
198 | it simply sanitizes it so that there will be no encoding errors later. |
|
190 | it simply sanitizes it so that there will be no encoding errors later. | |
199 |
|
191 | |||
200 | Examples |
|
|||
201 | -------- |
|
|||
202 | >>> json_clean(4) |
|
|||
203 | 4 |
|
|||
204 | >>> json_clean(list(range(10))) |
|
|||
205 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
|
|||
206 | >>> sorted(json_clean(dict(x=1, y=2)).items()) |
|
|||
207 | [('x', 1), ('y', 2)] |
|
|||
208 | >>> sorted(json_clean(dict(x=1, y=2, z=[1,2,3])).items()) |
|
|||
209 | [('x', 1), ('y', 2), ('z', [1, 2, 3])] |
|
|||
210 | >>> json_clean(True) |
|
|||
211 | True |
|
|||
212 | """ |
|
192 | """ | |
213 | # types that are 'atomic' and ok in json as-is. |
|
193 | # types that are 'atomic' and ok in json as-is. | |
214 | atomic_ok = (unicode_type, type(None)) |
|
194 | atomic_ok = (unicode_type, type(None)) | |
@@ -247,14 +227,14 b' def json_clean(obj):' | |||||
247 | # key collisions after stringification. This can happen with keys like |
|
227 | # key collisions after stringification. This can happen with keys like | |
248 | # True and 'true' or 1 and '1', which collide in JSON. |
|
228 | # True and 'true' or 1 and '1', which collide in JSON. | |
249 | nkeys = len(obj) |
|
229 | nkeys = len(obj) | |
250 |
nkeys_collapsed = len(set(map( |
|
230 | nkeys_collapsed = len(set(map(unicode_type, obj))) | |
251 | if nkeys != nkeys_collapsed: |
|
231 | if nkeys != nkeys_collapsed: | |
252 |
raise ValueError('dict can |
|
232 | raise ValueError('dict cannot be safely converted to JSON: ' | |
253 | 'key collision would lead to dropped values') |
|
233 | 'key collision would lead to dropped values') | |
254 | # If all OK, proceed by making the new dict that will be json-safe |
|
234 | # If all OK, proceed by making the new dict that will be json-safe | |
255 | out = {} |
|
235 | out = {} | |
256 | for k,v in iteritems(obj): |
|
236 | for k,v in iteritems(obj): | |
257 |
out[ |
|
237 | out[unicode_type(k)] = json_clean(v) | |
258 | return out |
|
238 | return out | |
259 |
|
239 | |||
260 | # If we get here, we don't know how to handle the object, so we just get |
|
240 | # If we get here, we don't know how to handle the object, so we just get |
@@ -1,31 +1,20 b'' | |||||
1 | """Test suite for our JSON utilities. |
|
1 | # coding: utf-8 | |
2 | """ |
|
2 | """Test suite for our JSON utilities.""" | |
3 | #----------------------------------------------------------------------------- |
|
3 | ||
4 |
# |
|
4 | # Copyright (c) IPython Development Team. | |
5 | # |
|
5 | # Distributed under the terms of the Modified BSD License. | |
6 | # Distributed under the terms of the BSD License. The full license is in |
|
6 | ||
7 | # the file COPYING.txt, distributed as part of this software. |
|
|||
8 | #----------------------------------------------------------------------------- |
|
|||
9 |
|
||||
10 | #----------------------------------------------------------------------------- |
|
|||
11 | # Imports |
|
|||
12 | #----------------------------------------------------------------------------- |
|
|||
13 | # stdlib |
|
|||
14 | import datetime |
|
7 | import datetime | |
15 | import json |
|
8 | import json | |
16 | from base64 import decodestring |
|
9 | from base64 import decodestring | |
17 |
|
10 | |||
18 | # third party |
|
|||
19 | import nose.tools as nt |
|
11 | import nose.tools as nt | |
20 |
|
12 | |||
21 | # our own |
|
|||
22 | from IPython.utils import jsonutil, tz |
|
13 | from IPython.utils import jsonutil, tz | |
23 | from ..jsonutil import json_clean, encode_images |
|
14 | from ..jsonutil import json_clean, encode_images | |
24 | from ..py3compat import unicode_to_str, str_to_bytes, iteritems |
|
15 | from ..py3compat import unicode_to_str, str_to_bytes, iteritems | |
25 |
|
16 | |||
26 | #----------------------------------------------------------------------------- |
|
17 | ||
27 | # Test functions |
|
|||
28 | #----------------------------------------------------------------------------- |
|
|||
29 | class Int(int): |
|
18 | class Int(int): | |
30 | def __str__(self): |
|
19 | def __str__(self): | |
31 | return 'Int(%i)' % self |
|
20 | return 'Int(%i)' % self | |
@@ -148,4 +137,8 b' def test_exception():' | |||||
148 | ] |
|
137 | ] | |
149 | for d in bad_dicts: |
|
138 | for d in bad_dicts: | |
150 | nt.assert_raises(ValueError, json_clean, d) |
|
139 | nt.assert_raises(ValueError, json_clean, d) | |
151 |
|
140 | |||
|
141 | def test_unicode_dict(): | |||
|
142 | data = {u'üniço∂e': u'üniço∂e'} | |||
|
143 | clean = jsonutil.json_clean(data) | |||
|
144 | nt.assert_equal(data, clean) |
General Comments 0
You need to be logged in to leave comments.
Login now