]> 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 02:37:29 +0000 (05:37 +0300)
committerEzio Melotti <none@none>
Wed, 13 Apr 2011 02:37:29 +0000 (05:37 +0300)
Lib/json/decoder.py
Lib/json/tests/test_decode.py
Misc/NEWS

index b9745f788442b378e9377a9ad5ab67e6914293bd..eeebf458687f76de7f1e3719e1a1eb1078036d27 100644 (file)
@@ -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))
index 6afafe69866858f27dc8fa522ed78e9bd74e305e..95088b454440f89849849dfa3ad3d6ffff2dc389 100644 (file)
@@ -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),
index 4d43f559e99a75e44f036cd31a0cd5d1931d2f8d..4718a69b1c32583f66ebd5537e186aaffff41e6f 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -51,7 +51,10 @@ Core and Builtins
 Library
 -------
 
-- Issue #11703 - urllib2.geturl() does not return correct url when the original
+- Issue #9233: Fix json.loads('{}') to return a dict (instead of a list), when
+  _json is not available.
+
+- Issue #11703: urllib2.geturl() does not return correct url when the original
   url contains #fragment.
 
 - Issue #10019: Fixed regression in json module where an indent of 0 stopped