| 16 | return b("bad __bytes__") |
| 17 | |
| 18 | class TestDecode(TestCase): |
| 19 | def test_decimal(self): |
| 20 | rval = json.loads('1.1', parse_float=decimal.Decimal) |
| 21 | self.assertTrue(isinstance(rval, decimal.Decimal)) |
| 22 | self.assertEqual(rval, decimal.Decimal('1.1')) |
| 23 | |
| 24 | def test_float(self): |
| 25 | rval = json.loads('1', parse_int=float) |
| 26 | self.assertTrue(isinstance(rval, float)) |
| 27 | self.assertEqual(rval, 1.0) |
| 28 | |
| 29 | def test_decoder_optimizations(self): |
| 30 | # Several optimizations were made that skip over calls to |
| 31 | # the whitespace regex, so this test is designed to try and |
| 32 | # exercise the uncommon cases. The array cases are already covered. |
| 33 | rval = json.loads('{ "key" : "value" , "k":"v" }') |
| 34 | self.assertEqual(rval, {"key":"value", "k":"v"}) |
| 35 | |
| 36 | def test_empty_objects(self): |
| 37 | s = '{}' |
| 38 | self.assertEqual(json.loads(s), eval(s)) |
| 39 | s = '[]' |
| 40 | self.assertEqual(json.loads(s), eval(s)) |
| 41 | s = '""' |
| 42 | self.assertEqual(json.loads(s), eval(s)) |
| 43 | |
| 44 | def test_object_pairs_hook(self): |
| 45 | s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}' |
| 46 | p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4), |
| 47 | ("qrt", 5), ("pad", 6), ("hoy", 7)] |
| 48 | self.assertEqual(json.loads(s), eval(s)) |
| 49 | self.assertEqual(json.loads(s, object_pairs_hook=lambda x: x), p) |
| 50 | self.assertEqual(json.load(StringIO(s), |
| 51 | object_pairs_hook=lambda x: x), p) |
| 52 | od = json.loads(s, object_pairs_hook=OrderedDict) |
| 53 | self.assertEqual(od, OrderedDict(p)) |
| 54 | self.assertEqual(type(od), OrderedDict) |
| 55 | # the object_pairs_hook takes priority over the object_hook |
| 56 | self.assertEqual(json.loads(s, |
| 57 | object_pairs_hook=OrderedDict, |
| 58 | object_hook=lambda x: None), |
| 59 | OrderedDict(p)) |
| 60 | |
| 61 | def check_keys_reuse(self, source, loads): |
| 62 | rval = loads(source) |
| 63 | (a, b), (c, d) = sorted(rval[0]), sorted(rval[1]) |
| 64 | self.assertIs(a, c) |
| 65 | self.assertIs(b, d) |
| 66 | |
| 67 | def test_keys_reuse_str(self): |
| 68 | s = u'[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]'.encode('utf8') |
| 69 | self.check_keys_reuse(s, json.loads) |
| 70 | |
| 71 | def test_keys_reuse_unicode(self): |
| 72 | s = u'[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]' |
| 73 | self.check_keys_reuse(s, json.loads) |
| 74 | |
| 75 | def test_empty_strings(self): |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…