1from __future__ import absolute_import 2import decimal 3from unittest import TestCase 4 5import simplejson as json 6from simplejson.compat import StringIO, b, binary_type 7from simplejson import OrderedDict 8 9class MisbehavingBytesSubtype(binary_type): 10 def decode(self, encoding=None): 11 return "bad decode" 12 def __str__(self): 13 return "bad __str__" 14 def __bytes__(self): 15 return b("bad __bytes__") 16 17class TestDecode(TestCase): 18 if not hasattr(TestCase, 'assertIs'): 19 def assertIs(self, a, b): 20 self.assertTrue(a is b, '%r is %r' % (a, b)) 21 22 def test_decimal(self): 23 rval = json.loads('1.1', parse_float=decimal.Decimal) 24 self.assertTrue(isinstance(rval, decimal.Decimal)) 25 self.assertEqual(rval, decimal.Decimal('1.1')) 26 27 def test_float(self): 28 rval = json.loads('1', parse_int=float) 29 self.assertTrue(isinstance(rval, float)) 30 self.assertEqual(rval, 1.0) 31 32 def test_decoder_optimizations(self): 33 # Several optimizations were made that skip over calls to 34 # the whitespace regex, so this test is designed to try and 35 # exercise the uncommon cases. The array cases are already covered. 36 rval = json.loads('{ "key" : "value" , "k":"v" }') 37 self.assertEqual(rval, {"key":"value", "k":"v"}) 38 39 def test_empty_objects(self): 40 s = '{}' 41 self.assertEqual(json.loads(s), eval(s)) 42 s = '[]' 43 self.assertEqual(json.loads(s), eval(s)) 44 s = '""' 45 self.assertEqual(json.loads(s), eval(s)) 46 47 def test_object_pairs_hook(self): 48 s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}' 49 p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4), 50 ("qrt", 5), ("pad", 6), ("hoy", 7)] 51 self.assertEqual(json.loads(s), eval(s)) 52 self.assertEqual(json.loads(s, object_pairs_hook=lambda x: x), p) 53 self.assertEqual(json.load(StringIO(s), 54 object_pairs_hook=lambda x: x), p) 55 od = json.loads(s, object_pairs_hook=OrderedDict) 56 self.assertEqual(od, OrderedDict(p)) 57 self.assertEqual(type(od), OrderedDict) 58 # the object_pairs_hook takes priority over the object_hook 59 self.assertEqual(json.loads(s, 60 object_pairs_hook=OrderedDict, 61 object_hook=lambda x: None), 62 OrderedDict(p)) 63 64 def check_keys_reuse(self, source, loads): 65 rval = loads(source) 66 (a, b), (c, d) = sorted(rval[0]), sorted(rval[1]) 67 self.assertIs(a, c) 68 self.assertIs(b, d) 69 70 def test_keys_reuse_str(self): 71 s = u'[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]'.encode('utf8') 72 self.check_keys_reuse(s, json.loads) 73 74 def test_keys_reuse_unicode(self): 75 s = u'[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]' 76 self.check_keys_reuse(s, json.loads) 77 78 def test_empty_strings(self): 79 self.assertEqual(json.loads('""'), "") 80 self.assertEqual(json.loads(u'""'), u"") 81 self.assertEqual(json.loads('[""]'), [""]) 82 self.assertEqual(json.loads(u'[""]'), [u""]) 83 84 def test_raw_decode(self): 85 cls = json.decoder.JSONDecoder 86 self.assertEqual( 87 ({'a': {}}, 9), 88 cls().raw_decode("{\"a\": {}}")) 89 # http://code.google.com/p/simplejson/issues/detail?id=85 90 self.assertEqual( 91 ({'a': {}}, 9), 92 cls(object_pairs_hook=dict).raw_decode("{\"a\": {}}")) 93 # https://github.com/simplejson/simplejson/pull/38 94 self.assertEqual( 95 ({'a': {}}, 11), 96 cls().raw_decode(" \n{\"a\": {}}")) 97 98 def test_bytes_decode(self): 99 cls = json.decoder.JSONDecoder 100 data = b('"\xe2\x82\xac"') 101 self.assertEqual(cls().decode(data), u'\u20ac') 102 self.assertEqual(cls(encoding='latin1').decode(data), u'\xe2\x82\xac') 103 self.assertEqual(cls(encoding=None).decode(data), u'\u20ac') 104 105 data = MisbehavingBytesSubtype(b('"\xe2\x82\xac"')) 106 self.assertEqual(cls().decode(data), u'\u20ac') 107 self.assertEqual(cls(encoding='latin1').decode(data), u'\xe2\x82\xac') 108 self.assertEqual(cls(encoding=None).decode(data), u'\u20ac') 109 110 def test_bounds_checking(self): 111 # https://github.com/simplejson/simplejson/issues/98 112 j = json.decoder.JSONDecoder() 113 for i in [4, 5, 6, -1, -2, -3, -4, -5, -6]: 114 self.assertRaises(ValueError, j.scan_once, '1234', i) 115 self.assertRaises(ValueError, j.raw_decode, '1234', i) 116 x, y = sorted(['128931233', '472389423'], key=id) 117 diff = id(x) - id(y) 118 self.assertRaises(ValueError, j.scan_once, y, diff) 119 self.assertRaises(ValueError, j.raw_decode, y, i) 120