##// END OF EJS Templates
Merge pull request #4599 from minrk/json_clean_enum...
Thomas Kluyver -
r13709:f2d32afc merge
parent child Browse files
Show More
@@ -194,9 +194,8 b' def json_clean(obj):'
194 >>> json_clean(True)
194 >>> json_clean(True)
195 True
195 True
196 """
196 """
197 # types that are 'atomic' and ok in json as-is. bool doesn't need to be
197 # types that are 'atomic' and ok in json as-is.
198 # listed explicitly because bools pass as int instances
198 atomic_ok = (unicode_type, type(None))
199 atomic_ok = (unicode_type, int, type(None))
200
199
201 # containers that we need to convert into lists
200 # containers that we need to convert into lists
202 container_to_list = (tuple, set, types.GeneratorType)
201 container_to_list = (tuple, set, types.GeneratorType)
@@ -205,7 +204,14 b' def json_clean(obj):'
205 # cast out-of-range floats to their reprs
204 # cast out-of-range floats to their reprs
206 if math.isnan(obj) or math.isinf(obj):
205 if math.isnan(obj) or math.isinf(obj):
207 return repr(obj)
206 return repr(obj)
208 return obj
207 return float(obj)
208
209 if isinstance(obj, int):
210 # cast int to int, in case subclasses override __str__ (e.g. boost enum, #4598)
211 if isinstance(obj, bool):
212 # bools are ints, but we don't want to cast them to 0,1
213 return obj
214 return int(obj)
209
215
210 if isinstance(obj, atomic_ok):
216 if isinstance(obj, atomic_ok):
211 return obj
217 return obj
@@ -26,6 +26,9 b' from ..py3compat import unicode_to_str, str_to_bytes, iteritems'
26 #-----------------------------------------------------------------------------
26 #-----------------------------------------------------------------------------
27 # Test functions
27 # Test functions
28 #-----------------------------------------------------------------------------
28 #-----------------------------------------------------------------------------
29 class Int(int):
30 def __str__(self):
31 return 'Int(%i)' % self
29
32
30 def test():
33 def test():
31 # list of input/expected output. Use None for the expected output if it
34 # list of input/expected output. Use None for the expected output if it
@@ -48,6 +51,7 b' def test():'
48 # More exotic objects
51 # More exotic objects
49 ((x for x in range(3)), [0, 1, 2]),
52 ((x for x in range(3)), [0, 1, 2]),
50 (iter([1, 2]), [1, 2]),
53 (iter([1, 2]), [1, 2]),
54 (Int(5), 5),
51 ]
55 ]
52
56
53 for val, jval in pairs:
57 for val, jval in pairs:
General Comments 0
You need to be logged in to leave comments. Login now