mirror of
https://github.com/python/cpython.git
synced 2025-12-04 00:30:19 +00:00
#9233: Fix json.loads({}) to return a dict (instead of a list), when _json is not available.
This commit is contained in:
parent
179816df59
commit
2b96f0987a
3 changed files with 15 additions and 1 deletions
|
|
@ -161,6 +161,12 @@ def JSONObject(s_and_end, encoding, strict, scan_once, object_hook,
|
|||
nextchar = s[end:end + 1]
|
||||
# Trivial empty object
|
||||
if nextchar == '}':
|
||||
if object_pairs_hook is not None:
|
||||
result = object_pairs_hook(pairs)
|
||||
return result, end
|
||||
pairs = {}
|
||||
if object_hook is not None:
|
||||
pairs = object_hook(pairs)
|
||||
return pairs, end + 1
|
||||
elif nextchar != '"':
|
||||
raise ValueError(errmsg("Expecting property name", s, end))
|
||||
|
|
|
|||
|
|
@ -23,6 +23,11 @@ class TestDecode(TestCase):
|
|||
rval = json.loads('{ "key" : "value" , "k":"v" }')
|
||||
self.assertEqual(rval, {"key":"value", "k":"v"})
|
||||
|
||||
def test_empty_objects(self):
|
||||
self.assertEqual(json.loads('{}'), {})
|
||||
self.assertEqual(json.loads('[]'), [])
|
||||
self.assertEqual(json.loads('""'), "")
|
||||
|
||||
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),
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue