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 | |
@@ -193,18 +185,6 b' def json_clean(obj):' | |||||
193 | encoded as JSON. Note that this function does not *encode* its inputs, |
|
185 | encoded as JSON. Note that this function does not *encode* its inputs, | |
194 | it simply sanitizes it so that there will be no encoding errors later. |
|
186 | it simply sanitizes it so that there will be no encoding errors later. | |
195 |
|
187 | |||
196 | Examples |
|
|||
197 | -------- |
|
|||
198 | >>> json_clean(4) |
|
|||
199 | 4 |
|
|||
200 | >>> json_clean(list(range(10))) |
|
|||
201 | [0, 1, 2, 3, 4, 5, 6, 7, 8, 9] |
|
|||
202 | >>> sorted(json_clean(dict(x=1, y=2)).items()) |
|
|||
203 | [('x', 1), ('y', 2)] |
|
|||
204 | >>> sorted(json_clean(dict(x=1, y=2, z=[1,2,3])).items()) |
|
|||
205 | [('x', 1), ('y', 2), ('z', [1, 2, 3])] |
|
|||
206 | >>> json_clean(True) |
|
|||
207 | True |
|
|||
208 | """ |
|
188 | """ | |
209 | # types that are 'atomic' and ok in json as-is. |
|
189 | # types that are 'atomic' and ok in json as-is. | |
210 | atomic_ok = (unicode_type, type(None)) |
|
190 | atomic_ok = (unicode_type, type(None)) | |
@@ -243,14 +223,14 b' def json_clean(obj):' | |||||
243 | # key collisions after stringification. This can happen with keys like |
|
223 | # key collisions after stringification. This can happen with keys like | |
244 | # True and 'true' or 1 and '1', which collide in JSON. |
|
224 | # True and 'true' or 1 and '1', which collide in JSON. | |
245 | nkeys = len(obj) |
|
225 | nkeys = len(obj) | |
246 |
nkeys_collapsed = len(set(map( |
|
226 | nkeys_collapsed = len(set(map(unicode_type, obj))) | |
247 | if nkeys != nkeys_collapsed: |
|
227 | if nkeys != nkeys_collapsed: | |
248 |
raise ValueError('dict can |
|
228 | raise ValueError('dict cannot be safely converted to JSON: ' | |
249 | 'key collision would lead to dropped values') |
|
229 | 'key collision would lead to dropped values') | |
250 | # If all OK, proceed by making the new dict that will be json-safe |
|
230 | # If all OK, proceed by making the new dict that will be json-safe | |
251 | out = {} |
|
231 | out = {} | |
252 | for k,v in iteritems(obj): |
|
232 | for k,v in iteritems(obj): | |
253 |
out[ |
|
233 | out[unicode_type(k)] = json_clean(v) | |
254 | return out |
|
234 | return out | |
255 |
|
235 | |||
256 | # If we get here, we don't know how to handle the object, so we just get |
|
236 | # 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 | |
@@ -155,4 +144,8 b' def test_exception():' | |||||
155 | ] |
|
144 | ] | |
156 | for d in bad_dicts: |
|
145 | for d in bad_dicts: | |
157 | nt.assert_raises(ValueError, json_clean, d) |
|
146 | nt.assert_raises(ValueError, json_clean, d) | |
158 |
|
147 | |||
|
148 | def test_unicode_dict(): | |||
|
149 | data = {u'üniço∂e': u'üniço∂e'} | |||
|
150 | clean = jsonutil.json_clean(data) | |||
|
151 | nt.assert_equal(data, clean) |
General Comments 0
You need to be logged in to leave comments.
Login now