1 | n/a | import decimal |
---|
2 | n/a | from io import StringIO, BytesIO |
---|
3 | n/a | from collections import OrderedDict |
---|
4 | n/a | from test.test_json import PyTest, CTest |
---|
5 | n/a | |
---|
6 | n/a | |
---|
7 | n/a | class TestDecode: |
---|
8 | n/a | def test_decimal(self): |
---|
9 | n/a | rval = self.loads('1.1', parse_float=decimal.Decimal) |
---|
10 | n/a | self.assertTrue(isinstance(rval, decimal.Decimal)) |
---|
11 | n/a | self.assertEqual(rval, decimal.Decimal('1.1')) |
---|
12 | n/a | |
---|
13 | n/a | def test_float(self): |
---|
14 | n/a | rval = self.loads('1', parse_int=float) |
---|
15 | n/a | self.assertTrue(isinstance(rval, float)) |
---|
16 | n/a | self.assertEqual(rval, 1.0) |
---|
17 | n/a | |
---|
18 | n/a | def test_empty_objects(self): |
---|
19 | n/a | self.assertEqual(self.loads('{}'), {}) |
---|
20 | n/a | self.assertEqual(self.loads('[]'), []) |
---|
21 | n/a | self.assertEqual(self.loads('""'), "") |
---|
22 | n/a | |
---|
23 | n/a | def test_object_pairs_hook(self): |
---|
24 | n/a | s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}' |
---|
25 | n/a | p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4), |
---|
26 | n/a | ("qrt", 5), ("pad", 6), ("hoy", 7)] |
---|
27 | n/a | self.assertEqual(self.loads(s), eval(s)) |
---|
28 | n/a | self.assertEqual(self.loads(s, object_pairs_hook=lambda x: x), p) |
---|
29 | n/a | self.assertEqual(self.json.load(StringIO(s), |
---|
30 | n/a | object_pairs_hook=lambda x: x), p) |
---|
31 | n/a | od = self.loads(s, object_pairs_hook=OrderedDict) |
---|
32 | n/a | self.assertEqual(od, OrderedDict(p)) |
---|
33 | n/a | self.assertEqual(type(od), OrderedDict) |
---|
34 | n/a | # the object_pairs_hook takes priority over the object_hook |
---|
35 | n/a | self.assertEqual(self.loads(s, object_pairs_hook=OrderedDict, |
---|
36 | n/a | object_hook=lambda x: None), |
---|
37 | n/a | OrderedDict(p)) |
---|
38 | n/a | # check that empty object literals work (see #17368) |
---|
39 | n/a | self.assertEqual(self.loads('{}', object_pairs_hook=OrderedDict), |
---|
40 | n/a | OrderedDict()) |
---|
41 | n/a | self.assertEqual(self.loads('{"empty": {}}', |
---|
42 | n/a | object_pairs_hook=OrderedDict), |
---|
43 | n/a | OrderedDict([('empty', OrderedDict())])) |
---|
44 | n/a | |
---|
45 | n/a | def test_decoder_optimizations(self): |
---|
46 | n/a | # Several optimizations were made that skip over calls to |
---|
47 | n/a | # the whitespace regex, so this test is designed to try and |
---|
48 | n/a | # exercise the uncommon cases. The array cases are already covered. |
---|
49 | n/a | rval = self.loads('{ "key" : "value" , "k":"v" }') |
---|
50 | n/a | self.assertEqual(rval, {"key":"value", "k":"v"}) |
---|
51 | n/a | |
---|
52 | n/a | def check_keys_reuse(self, source, loads): |
---|
53 | n/a | rval = loads(source) |
---|
54 | n/a | (a, b), (c, d) = sorted(rval[0]), sorted(rval[1]) |
---|
55 | n/a | self.assertIs(a, c) |
---|
56 | n/a | self.assertIs(b, d) |
---|
57 | n/a | |
---|
58 | n/a | def test_keys_reuse(self): |
---|
59 | n/a | s = '[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]' |
---|
60 | n/a | self.check_keys_reuse(s, self.loads) |
---|
61 | n/a | self.check_keys_reuse(s, self.json.decoder.JSONDecoder().decode) |
---|
62 | n/a | |
---|
63 | n/a | def test_extra_data(self): |
---|
64 | n/a | s = '[1, 2, 3]5' |
---|
65 | n/a | msg = 'Extra data' |
---|
66 | n/a | self.assertRaisesRegex(self.JSONDecodeError, msg, self.loads, s) |
---|
67 | n/a | |
---|
68 | n/a | def test_invalid_escape(self): |
---|
69 | n/a | s = '["abc\\y"]' |
---|
70 | n/a | msg = 'escape' |
---|
71 | n/a | self.assertRaisesRegex(self.JSONDecodeError, msg, self.loads, s) |
---|
72 | n/a | |
---|
73 | n/a | def test_invalid_input_type(self): |
---|
74 | n/a | msg = 'the JSON object must be str' |
---|
75 | n/a | for value in [1, 3.14, [], {}, None]: |
---|
76 | n/a | self.assertRaisesRegex(TypeError, msg, self.loads, value) |
---|
77 | n/a | |
---|
78 | n/a | def test_string_with_utf8_bom(self): |
---|
79 | n/a | # see #18958 |
---|
80 | n/a | bom_json = "[1,2,3]".encode('utf-8-sig').decode('utf-8') |
---|
81 | n/a | with self.assertRaises(self.JSONDecodeError) as cm: |
---|
82 | n/a | self.loads(bom_json) |
---|
83 | n/a | self.assertIn('BOM', str(cm.exception)) |
---|
84 | n/a | with self.assertRaises(self.JSONDecodeError) as cm: |
---|
85 | n/a | self.json.load(StringIO(bom_json)) |
---|
86 | n/a | self.assertIn('BOM', str(cm.exception)) |
---|
87 | n/a | # make sure that the BOM is not detected in the middle of a string |
---|
88 | n/a | bom_in_str = '"{}"'.format(''.encode('utf-8-sig').decode('utf-8')) |
---|
89 | n/a | self.assertEqual(self.loads(bom_in_str), '\ufeff') |
---|
90 | n/a | self.assertEqual(self.json.load(StringIO(bom_in_str)), '\ufeff') |
---|
91 | n/a | |
---|
92 | n/a | def test_negative_index(self): |
---|
93 | n/a | d = self.json.JSONDecoder() |
---|
94 | n/a | self.assertRaises(ValueError, d.raw_decode, 'a'*42, -50000) |
---|
95 | n/a | |
---|
96 | n/a | class TestPyDecode(TestDecode, PyTest): pass |
---|
97 | n/a | class TestCDecode(TestDecode, CTest): pass |
---|