##// END OF EJS Templates
Merge pull request #6077 from minrk/json-unicode-keys...
Thomas Kluyver -
r17148:99cdf189 merge
parent child Browse files
Show More
@@ -1,16 +1,8
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
@@ -193,18 +185,6 def json_clean(obj):
193 185 encoded as JSON. Note that this function does not *encode* its inputs,
194 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 189 # types that are 'atomic' and ok in json as-is.
210 190 atomic_ok = (unicode_type, type(None))
@@ -243,14 +223,14 def json_clean(obj):
243 223 # key collisions after stringification. This can happen with keys like
244 224 # True and 'true' or 1 and '1', which collide in JSON.
245 225 nkeys = len(obj)
246 nkeys_collapsed = len(set(map(str, obj)))
226 nkeys_collapsed = len(set(map(unicode_type, obj)))
247 227 if nkeys != nkeys_collapsed:
248 raise ValueError('dict can not be safely converted to JSON: '
228 raise ValueError('dict cannot be safely converted to JSON: '
249 229 'key collision would lead to dropped values')
250 230 # If all OK, proceed by making the new dict that will be json-safe
251 231 out = {}
252 232 for k,v in iteritems(obj):
253 out[str(k)] = json_clean(v)
233 out[unicode_type(k)] = json_clean(v)
254 234 return out
255 235
256 236 # If we get here, we don't know how to handle the object, so we just get
@@ -1,31 +1,20
1 """Test suite for our JSON utilities.
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 #-----------------------------------------------------------------------------
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
@@ -155,4 +144,8 def test_exception():
155 144 ]
156 145 for d in bad_dicts:
157 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