]> granicus.if.org Git - python/commitdiff
#9233: Fix json.loads({}) to return a dict (instead of a list), when _json is not...
authorEzio Melotti <none@none>
Wed, 13 Apr 2011 04:10:13 +0000 (07:10 +0300)
committerEzio Melotti <none@none>
Wed, 13 Apr 2011 04:10:13 +0000 (07:10 +0300)
Lib/json/decoder.py
Lib/json/tests/test_decode.py

index 7b3d7d54a1adb5d3062812cd65cbedb78927e6d2..12fa81542b4013fd6267f217dca856f31d533bed 100644 (file)
@@ -161,6 +161,12 @@ def JSONObject(s_and_end, strict, scan_once, object_hook, object_pairs_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))
index 74d886ac26efc80223da70202be1f687eff24529..bf65e4696ced4af5f55364d9e6d1c690ea546ec5 100644 (file)
@@ -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('{}'), {})
+        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),