• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import decimal
2from StringIO import StringIO
3from collections import OrderedDict
4from json.tests import PyTest, CTest
5
6
7class TestDecode(object):
8    def test_decimal(self):
9        rval = self.loads('1.1', parse_float=decimal.Decimal)
10        self.assertTrue(isinstance(rval, decimal.Decimal))
11        self.assertEqual(rval, decimal.Decimal('1.1'))
12
13    def test_float(self):
14        rval = self.loads('1', parse_int=float)
15        self.assertTrue(isinstance(rval, float))
16        self.assertEqual(rval, 1.0)
17
18    def test_decoder_optimizations(self):
19        # Several optimizations were made that skip over calls to
20        # the whitespace regex, so this test is designed to try and
21        # exercise the uncommon cases. The array cases are already covered.
22        rval = self.loads('{   "key"    :    "value"    ,  "k":"v"    }')
23        self.assertEqual(rval, {"key":"value", "k":"v"})
24
25    def test_empty_objects(self):
26        self.assertEqual(self.loads('{}'), {})
27        self.assertEqual(self.loads('[]'), [])
28        self.assertEqual(self.loads('""'), u"")
29        self.assertIsInstance(self.loads('""'), unicode)
30
31    def test_object_pairs_hook(self):
32        s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
33        p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),
34             ("qrt", 5), ("pad", 6), ("hoy", 7)]
35        self.assertEqual(self.loads(s), eval(s))
36        self.assertEqual(self.loads(s, object_pairs_hook=lambda x: x), p)
37        self.assertEqual(self.json.load(StringIO(s),
38                                        object_pairs_hook=lambda x: x), p)
39        od = self.loads(s, object_pairs_hook=OrderedDict)
40        self.assertEqual(od, OrderedDict(p))
41        self.assertEqual(type(od), OrderedDict)
42        # the object_pairs_hook takes priority over the object_hook
43        self.assertEqual(self.loads(s, object_pairs_hook=OrderedDict,
44                                    object_hook=lambda x: None),
45                         OrderedDict(p))
46        # check that empty object literals work (see #17368)
47        self.assertEqual(self.loads('{}', object_pairs_hook=OrderedDict),
48                         OrderedDict())
49        self.assertEqual(self.loads('{"empty": {}}',
50                                    object_pairs_hook=OrderedDict),
51                         OrderedDict([('empty', OrderedDict())]))
52
53    def test_extra_data(self):
54        s = '[1, 2, 3]5'
55        msg = 'Extra data'
56        self.assertRaisesRegexp(ValueError, msg, self.loads, s)
57
58    def test_invalid_escape(self):
59        s = '["abc\\y"]'
60        msg = 'escape'
61        self.assertRaisesRegexp(ValueError, msg, self.loads, s)
62
63    def test_negative_index(self):
64        d = self.json.JSONDecoder()
65        self.assertRaises(ValueError, d.raw_decode, 'a'*42, -50000)
66        self.assertRaises(ValueError, d.raw_decode, u'a'*42, -50000)
67
68class TestPyDecode(TestDecode, PyTest): pass
69class TestCDecode(TestDecode, CTest): pass
70