diff options
author | Raymond Hettinger <python@rcn.com> | 2009-04-21 03:09:17 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-04-21 03:09:17 (GMT) |
commit | 0ad98d8509764ee3947c481103e1e735cc048305 (patch) | |
tree | da572cd1e3429219c461af0a5e170a6a22af09c8 /Lib/json/tests | |
parent | e5925773ba4a06cdeb5a2d52bbbf9c50f3431197 (diff) | |
download | cpython-0ad98d8509764ee3947c481103e1e735cc048305.zip cpython-0ad98d8509764ee3947c481103e1e735cc048305.tar.gz cpython-0ad98d8509764ee3947c481103e1e735cc048305.tar.bz2 |
Forward port r70471: Add object_pairs_hook. Issue 5381.
Diffstat (limited to 'Lib/json/tests')
-rw-r--r-- | Lib/json/tests/test_decode.py | 19 | ||||
-rw-r--r-- | Lib/json/tests/test_unicode.py | 16 |
2 files changed, 35 insertions, 0 deletions
diff --git a/Lib/json/tests/test_decode.py b/Lib/json/tests/test_decode.py index 609f622..67c4c29 100644 --- a/Lib/json/tests/test_decode.py +++ b/Lib/json/tests/test_decode.py @@ -1,7 +1,9 @@ import decimal from unittest import TestCase +from io import StringIO import json +from collections import OrderedDict class TestDecode(TestCase): def test_decimal(self): @@ -13,3 +15,20 @@ class TestDecode(TestCase): rval = json.loads('1', parse_int=float) self.assert_(isinstance(rval, float)) self.assertEquals(rval, 1.0) + + def test_object_pairs_hook(self): + s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}' + p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4), + ("qrt", 5), ("pad", 6), ("hoy", 7)] + self.assertEqual(json.loads(s), eval(s)) + self.assertEqual(json.loads(s, object_pairs_hook = lambda x: x), p) + self.assertEqual(json.load(StringIO(s), + object_pairs_hook=lambda x: x), p) + od = json.loads(s, object_pairs_hook = OrderedDict) + self.assertEqual(od, OrderedDict(p)) + self.assertEqual(type(od), OrderedDict) + # the object_pairs_hook takes priority over the object_hook + self.assertEqual(json.loads(s, + object_pairs_hook = OrderedDict, + object_hook = lambda x: None), + OrderedDict(p)) diff --git a/Lib/json/tests/test_unicode.py b/Lib/json/tests/test_unicode.py index 7b9c956..00bf58e 100644 --- a/Lib/json/tests/test_unicode.py +++ b/Lib/json/tests/test_unicode.py @@ -1,6 +1,7 @@ from unittest import TestCase import json +from collections import OrderedDict class TestUnicode(TestCase): def test_encoding1(self): @@ -53,3 +54,18 @@ class TestUnicode(TestCase): u = chr(i) js = '"\\u{0:04x}"'.format(i) self.assertEquals(json.loads(js), u) + + def test_object_pairs_hook_with_unicode(self): + s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}' + p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4), + ("qrt", 5), ("pad", 6), ("hoy", 7)] + self.assertEqual(json.loads(s), eval(s)) + self.assertEqual(json.loads(s, object_pairs_hook = lambda x: x), p) + od = json.loads(s, object_pairs_hook = OrderedDict) + self.assertEqual(od, OrderedDict(p)) + self.assertEqual(type(od), OrderedDict) + # the object_pairs_hook takes priority over the object_hook + self.assertEqual(json.loads(s, + object_pairs_hook = OrderedDict, + object_hook = lambda x: None), + OrderedDict(p)) |