[Python-checkins] cpython (3.1): #9233: Fix json.loads({}) to return a dict (instead of a list), when _json is

ezio.melotti python-checkins at python.org
Wed Apr 13 06:21:56 CEST 2011


http://hg.python.org/cpython/rev/a220458179ed
changeset:   69319:a220458179ed
branch:      3.1
user:        Ezio Melotti
date:        Wed Apr 13 07:10:13 2011 +0300
summary:
  #9233: Fix json.loads({}) to return a dict (instead of a list), when _json is not available.

files:
  Lib/json/decoder.py           |  6 ++++++
  Lib/json/tests/test_decode.py |  5 +++++
  2 files changed, 11 insertions(+), 0 deletions(-)


diff --git a/Lib/json/decoder.py b/Lib/json/decoder.py
--- a/Lib/json/decoder.py
+++ b/Lib/json/decoder.py
@@ -161,6 +161,12 @@
             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))
diff --git a/Lib/json/tests/test_decode.py b/Lib/json/tests/test_decode.py
--- a/Lib/json/tests/test_decode.py
+++ b/Lib/json/tests/test_decode.py
@@ -16,6 +16,11 @@
         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),

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list