Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 1 | import decimal |
| 2 | from unittest import TestCase |
Raymond Hettinger | 0ad98d8 | 2009-04-21 03:09:17 +0000 | [diff] [blame] | 3 | from io import StringIO |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 4 | from contextlib import contextmanager |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 5 | |
| 6 | import json |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 7 | import json.decoder |
| 8 | import json.scanner |
Raymond Hettinger | 0ad98d8 | 2009-04-21 03:09:17 +0000 | [diff] [blame] | 9 | from collections import OrderedDict |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 10 | |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 11 | |
| 12 | @contextmanager |
| 13 | def use_python_scanner(): |
| 14 | py_scanner = json.scanner.py_make_scanner |
| 15 | old_scanner = json.decoder.make_scanner |
| 16 | json.decoder.make_scanner = py_scanner |
| 17 | try: |
| 18 | yield |
| 19 | finally: |
| 20 | json.decoder.make_scanner = old_scanner |
| 21 | |
| 22 | |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 23 | class TestDecode(TestCase): |
| 24 | def test_decimal(self): |
| 25 | rval = json.loads('1.1', parse_float=decimal.Decimal) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 26 | self.assertTrue(isinstance(rval, decimal.Decimal)) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 27 | self.assertEquals(rval, decimal.Decimal('1.1')) |
| 28 | |
| 29 | def test_float(self): |
| 30 | rval = json.loads('1', parse_int=float) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 31 | self.assertTrue(isinstance(rval, float)) |
Christian Heimes | 9054000 | 2008-05-08 14:29:10 +0000 | [diff] [blame] | 32 | self.assertEquals(rval, 1.0) |
Raymond Hettinger | 0ad98d8 | 2009-04-21 03:09:17 +0000 | [diff] [blame] | 33 | |
| 34 | def test_object_pairs_hook(self): |
| 35 | s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}' |
| 36 | p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4), |
| 37 | ("qrt", 5), ("pad", 6), ("hoy", 7)] |
| 38 | self.assertEqual(json.loads(s), eval(s)) |
| 39 | self.assertEqual(json.loads(s, object_pairs_hook = lambda x: x), p) |
| 40 | self.assertEqual(json.load(StringIO(s), |
| 41 | object_pairs_hook=lambda x: x), p) |
| 42 | od = json.loads(s, object_pairs_hook = OrderedDict) |
| 43 | self.assertEqual(od, OrderedDict(p)) |
| 44 | self.assertEqual(type(od), OrderedDict) |
| 45 | # the object_pairs_hook takes priority over the object_hook |
| 46 | self.assertEqual(json.loads(s, |
| 47 | object_pairs_hook = OrderedDict, |
| 48 | object_hook = lambda x: None), |
| 49 | OrderedDict(p)) |
Benjamin Peterson | c6b607d | 2009-05-02 12:36:44 +0000 | [diff] [blame] | 50 | |
| 51 | def test_decoder_optimizations(self): |
| 52 | # Several optimizations were made that skip over calls to |
| 53 | # the whitespace regex, so this test is designed to try and |
| 54 | # exercise the uncommon cases. The array cases are already covered. |
| 55 | rval = json.loads('{ "key" : "value" , "k":"v" }') |
| 56 | self.assertEquals(rval, {"key":"value", "k":"v"}) |
Antoine Pitrou | 7d6e076 | 2010-09-04 20:16:53 +0000 | [diff] [blame] | 57 | |
| 58 | def check_keys_reuse(self, source, loads): |
| 59 | rval = loads(source) |
| 60 | (a, b), (c, d) = sorted(rval[0]), sorted(rval[1]) |
| 61 | self.assertIs(a, c) |
| 62 | self.assertIs(b, d) |
| 63 | |
| 64 | def test_keys_reuse(self): |
| 65 | s = '[{"a_key": 1, "b_\xe9": 2}, {"a_key": 3, "b_\xe9": 4}]' |
| 66 | self.check_keys_reuse(s, json.loads) |
| 67 | # Disabled: the pure Python version of json simply doesn't work |
| 68 | with use_python_scanner(): |
| 69 | self.check_keys_reuse(s, json.decoder.JSONDecoder().decode) |