##// END OF EJS Templates
allow unicode keys in dicts in json_clean
MinRK -
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
@@ -247,14 +239,14 b' def json_clean(obj):'
247 # key collisions after stringification. This can happen with keys like
239 # key collisions after stringification. This can happen with keys like
248 # True and 'true' or 1 and '1', which collide in JSON.
240 # True and 'true' or 1 and '1', which collide in JSON.
249 nkeys = len(obj)
241 nkeys = len(obj)
250 nkeys_collapsed = len(set(map(str, obj)))
242 nkeys_collapsed = len(set(map(unicode_type, obj)))
251 if nkeys != nkeys_collapsed:
243 if nkeys != nkeys_collapsed:
252 raise ValueError('dict can not be safely converted to JSON: '
244 raise ValueError('dict cannot be safely converted to JSON: '
253 'key collision would lead to dropped values')
245 'key collision would lead to dropped values')
254 # If all OK, proceed by making the new dict that will be json-safe
246 # If all OK, proceed by making the new dict that will be json-safe
255 out = {}
247 out = {}
256 for k,v in iteritems(obj):
248 for k,v in iteritems(obj):
257 out[str(k)] = json_clean(v)
249 out[unicode_type(k)] = json_clean(v)
258 return out
250 return out
259
251
260 # If we get here, we don't know how to handle the object, so we just get
252 # 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 # Copyright (C) 2010-2011 The IPython Development Team
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