Skip to content

Commit d333e5a

Browse files
authored
Test object_hook and pickleability of some JSON components (GH-154155)
1 parent a6e1b83 commit d333e5a

2 files changed

Lines changed: 34 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),
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import json
2+
import pickle
3+
import unittest
4+
5+
class TestPickleable(unittest.TestCase):
6+
def test_json_decode_error_is_pickleable(self):
7+
e = json.JSONDecodeError(msg="abc", doc="def", pos=7)
8+
9+
pickled = pickle.dumps(e)
10+
unpickled = pickle.loads(pickled)
11+
12+
self.assertEqual(unpickled.msg, e.msg)
13+
self.assertEqual(unpickled.doc, e.doc)
14+
self.assertEqual(unpickled.pos, e.pos)

0 commit comments

Comments
 (0)