Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Lib/test/test_json/test_decode.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@ def test_empty_objects(self):
self.assertEqual(self.loads('[]'), [])
self.assertEqual(self.loads('""'), "")

def test_object_hook(self):
s = '{"a":{"b":{}}}'

expected_result = {"a":{"b":{"x":1}, "x":1}, "x":1}
expected_hook_arguments = [
{}, {"b": {"x":1}}, {"a": {"b": {"x":1}, "x":1}}
]

hook_arguments = []

def hook(x):
hook_arguments.append(x)
return {**x, "x":1}

result = self.loads(s, object_hook=hook)

self.assertEqual(result, expected_result)
self.assertEqual(hook_arguments, expected_hook_arguments)


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),
Expand Down
14 changes: 14 additions & 0 deletions Lib/test/test_json/test_pickleable.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import json

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why a new file?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@encukou advised me not to put this in the file with decoder tests. I saw no existing dedicated file for testing pickling of things from the json module. Where do you propose placing the test?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah I see. I guess, it's fine then (I wasn't aware of the rationale so it was surprsing).

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, all other JSON tests run twice, with speedups (_json) enabled/disabled, but this doesn't sit well with pickle.

import pickle
import unittest

class TestPickleable(unittest.TestCase):
def test_json_decode_error_is_pickleable(self):
e = json.JSONDecodeError(msg="abc", doc="def", pos=7)

pickled = pickle.dumps(e)
unpickled = pickle.loads(pickled)

self.assertEqual(unpickled.msg, e.msg)
self.assertEqual(unpickled.doc, e.doc)
self.assertEqual(unpickled.pos, e.pos)
Loading