Skip to content

Commit 1bc9e69

Browse files
committed
add test for object hook
1 parent 1604043 commit 1bc9e69

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

Lib/test/test_json/test_decode.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,26 @@ def test_empty_objects(self):
4848
self.assertEqual(self.loads('[]'), [])
4949
self.assertEqual(self.loads('""'), "")
5050

51+
def test_object_hook(self):
52+
s = '{"a":{"b":{}}}'
53+
54+
expected_result = {"a":{"b":{"x":1}, "x":1}, "x":1}
55+
expected_hook_arguments = [
56+
{}, {"b": {"x":1}}, {"a": {"b": {"x":1}, "x":1}}
57+
]
58+
59+
hook_arguments = []
60+
61+
def hook(x):
62+
hook_arguments.append(x)
63+
return {**x, "x":1}
64+
65+
result = self.loads(s, object_hook=hook)
66+
67+
self.assertEqual(result, expected_result)
68+
self.assertEqual(hook_arguments, expected_hook_arguments)
69+
70+
5171
def test_object_pairs_hook(self):
5272
s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
5373
p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),

0 commit comments

Comments
 (0)