Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d210aa1

Browse files
author
Ezio Melotti
committed
#9233: Fix json.loads({}) to return a dict (instead of a list), when _json is not available.
1 parent 42368f9 commit d210aa1

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

Lib/json/decoder.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,12 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_hook,
161161
nextchar = s[end:end + 1]
162162
# Trivial empty object
163163
if nextchar == '}':
164+
if object_pairs_hook is not None:
165+
result = object_pairs_hook(pairs)
166+
return result, end
167+
pairs = {}
168+
if object_hook is not None:
169+
pairs = object_hook(pairs)
164170
return pairs, end + 1
165171
elif nextchar != '"':
166172
raise ValueError(errmsg("Expecting property name", s, end))

Lib/json/tests/test_decode.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ def test_float(self):
1616
self.assertTrue(isinstance(rval, float))
1717
self.assertEqual(rval, 1.0)
1818

19+
def test_empty_objects(self):
20+
self.assertEqual(json.loads('{}'), {})
21+
self.assertEqual(json.loads('[]'), [])
22+
self.assertEqual(json.loads('""'), "")
23+
1924
def test_object_pairs_hook(self):
2025
s = '{"xkd":1, "kcw":2, "art":3, "hxm":4, "qrt":5, "pad":6, "hoy":7}'
2126
p = [("xkd", 1), ("kcw", 2), ("art", 3), ("hxm", 4),

0 commit comments

Comments
 (0)