cpython: a220458179ed (original) (raw)
Mercurial > cpython
changeset 69319:a220458179ed 3.1
#9233: Fix json.loads({}) to return a dict (instead of a list), when _json is not available. [#9233]
Ezio Melotti | |
---|---|
date | Wed, 13 Apr 2011 07:10:13 +0300 |
parents | ec6d881f5b02 |
children | b279611146d7 bbed01a7d0d1 |
files | Lib/json/decoder.py Lib/json/tests/test_decode.py |
diffstat | 2 files changed, 11 insertions(+), 0 deletions(-)[+] [-] Lib/json/decoder.py 6 Lib/json/tests/test_decode.py 5 |
line wrap: on
line diff
--- a/Lib/json/decoder.py +++ b/Lib/json/decoder.py @@ -161,6 +161,12 @@ def JSONObject(s_and_end, strict, scan_o nextchar = s[end:end + 1] # Trivial empty object if nextchar == '}':
if object_pairs_hook is not None:[](#l1.7)
result = object_pairs_hook(pairs)[](#l1.8)
return result, end[](#l1.9)
pairs = {}[](#l1.10)
if object_hook is not None:[](#l1.11)
pairs = object_hook(pairs)[](#l1.12) return pairs, end + 1[](#l1.13) elif nextchar != '"':[](#l1.14) raise ValueError(errmsg("Expecting property name", s, end))[](#l1.15)
--- a/Lib/json/tests/test_decode.py +++ b/Lib/json/tests/test_decode.py @@ -16,6 +16,11 @@ class TestDecode(TestCase): self.assertTrue(isinstance(rval, float)) self.assertEqual(rval, 1.0)
- def test_empty_objects(self):
self.assertEqual(json.loads('{}'), {})[](#l2.8)
self.assertEqual(json.loads('[]'), [])[](#l2.9)
self.assertEqual(json.loads('""'), "")[](#l2.10)
+ 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),