Show More
@@ -1,16 +1,8 b'' | |||
|
1 | """Utilities to manipulate JSON objects. | |
|
2 | """ | |
|
3 | #----------------------------------------------------------------------------- | |
|
4 | # Copyright (C) 2010-2011 The IPython Development Team | |
|
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 | #----------------------------------------------------------------------------- | |
|
1 | """Utilities to manipulate JSON objects.""" | |
|
2 | ||
|
3 | # Copyright (c) IPython Development Team. | |
|
4 | # Distributed under the terms of the Modified BSD License. | |
|
9 | 5 | |
|
10 | #----------------------------------------------------------------------------- | |
|
11 | # Imports | |
|
12 | #----------------------------------------------------------------------------- | |
|
13 | # stdlib | |
|
14 | 6 | import math |
|
15 | 7 | import re |
|
16 | 8 | import types |
@@ -197,18 +189,6 b' def json_clean(obj):' | |||
|
197 | 189 | encoded as JSON. Note that this function does not *encode* its inputs, |
|
198 | 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 | 193 | # types that are 'atomic' and ok in json as-is. |
|
214 | 194 | atomic_ok = (unicode_type, type(None)) |
@@ -247,14 +227,14 b' def json_clean(obj):' | |||
|
247 | 227 | # key collisions after stringification. This can happen with keys like |
|
248 | 228 | # True and 'true' or 1 and '1', which collide in JSON. |
|
249 | 229 | nkeys = len(obj) |
|
250 |
nkeys_collapsed = len(set(map( |
|
|
230 | nkeys_collapsed = len(set(map(unicode_type, obj))) | |
|
251 | 231 | if nkeys != nkeys_collapsed: |
|
252 |
raise ValueError('dict can |
|
|
232 | raise ValueError('dict cannot be safely converted to JSON: ' | |
|
253 | 233 | 'key collision would lead to dropped values') |
|
254 | 234 | # If all OK, proceed by making the new dict that will be json-safe |
|
255 | 235 | out = {} |
|
256 | 236 | for k,v in iteritems(obj): |
|
257 |
out[ |
|
|
237 | out[unicode_type(k)] = json_clean(v) | |
|
258 | 238 | return out |
|
259 | 239 | |
|
260 | 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. | |
|
2 | """ | |
|
3 | #----------------------------------------------------------------------------- | |
|
4 |
# |
|
|
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 | ||
|
10 | #----------------------------------------------------------------------------- | |
|
11 | # Imports | |
|
12 | #----------------------------------------------------------------------------- | |
|
13 | # stdlib | |
|
1 | # coding: utf-8 | |
|
2 | """Test suite for our JSON utilities.""" | |
|
3 | ||
|
4 | # Copyright (c) IPython Development Team. | |
|
5 | # Distributed under the terms of the Modified BSD License. | |
|
6 | ||
|
14 | 7 | import datetime |
|
15 | 8 | import json |
|
16 | 9 | from base64 import decodestring |
|
17 | 10 | |
|
18 | # third party | |
|
19 | 11 | import nose.tools as nt |
|
20 | 12 | |
|
21 | # our own | |
|
22 | 13 | from IPython.utils import jsonutil, tz |
|
23 | 14 | from ..jsonutil import json_clean, encode_images |
|
24 | 15 | from ..py3compat import unicode_to_str, str_to_bytes, iteritems |
|
25 | 16 | |
|
26 | #----------------------------------------------------------------------------- | |
|
27 | # Test functions | |
|
28 | #----------------------------------------------------------------------------- | |
|
17 | ||
|
29 | 18 | class Int(int): |
|
30 | 19 | def __str__(self): |
|
31 | 20 | return 'Int(%i)' % self |
@@ -148,4 +137,8 b' def test_exception():' | |||
|
148 | 137 | ] |
|
149 | 138 | for d in bad_dicts: |
|
150 | 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