[pypy-commit] pypy fastjson: decoding arrays

antocuni noreply at buildbot.pypy.org
Wed Jun 5 16:52:43 CEST 2013


Author: Antonio Cuni <anto.cuni at gmail.com>
Branch: fastjson
Changeset: r64799:0fce473a480a
Date: 2013-06-05 15:53 +0200
http://bitbucket.org/pypy/pypy/changeset/0fce473a480a/

Log:	decoding arrays

diff --git a/pypy/module/_fastjson/interp_decoder.py b/pypy/module/_fastjson/interp_decoder.py
--- a/pypy/module/_fastjson/interp_decoder.py
+++ b/pypy/module/_fastjson/interp_decoder.py
@@ -72,6 +72,9 @@
             return self.decode_string()
         elif ch.isdigit() or ch == '-':
             return self.decode_numeric()
+        elif ch == '[':
+            self.next()
+            return self.decode_array()
         elif ch == '{':
             self.next()
             return self.decode_object()
@@ -134,6 +137,30 @@
             self._raise("Expected digit at char %d", self.i)
         return intval, count
         
+    def decode_array(self):
+        start = self.i
+        w_list = self.space.newlist([])
+        self.skip_whitespace()
+        while not self.eof():
+            ch = self.peek()
+            if ch == ']':
+                self.next()
+                return w_list
+            w_item = self.decode_any()
+            self.space.call_method(w_list, 'append', w_item)
+            self.skip_whitespace()
+            if self.eof():
+                break
+            ch = self.next()
+            if ch == ']':
+                return w_list
+            elif ch == ',':
+                pass
+            else:
+                self._raise("Unexpected '%s' when decoding array (char %d)",
+                            ch, self.i)
+        self._raise("Unterminated array starting at char %d", start)
+
 
     def decode_object(self):
         start = self.i
@@ -150,7 +177,7 @@
             if self.last_type != TYPE_STRING:
                 self._raise("Key name must be string for object starting at char %d", start)
             self.skip_whitespace()
-            ch = self.next()
+            ch = self.next() # XXX
             if ch != ':':
                 self._raise("No ':' found at char %d", self.i)
             self.skip_whitespace()
diff --git a/pypy/module/_fastjson/test/test__fastjson.py b/pypy/module/_fastjson/test/test__fastjson.py
--- a/pypy/module/_fastjson/test/test__fastjson.py
+++ b/pypy/module/_fastjson/test/test__fastjson.py
@@ -109,3 +109,11 @@
         import _fastjson
         raises(ValueError, "_fastjson.loads('{42: 43}')")
         
+    def test_decode_array(self):
+        import _fastjson
+        assert _fastjson.loads('[]') == []
+        assert _fastjson.loads('[  ]') == []
+        assert _fastjson.loads('[1]') == [1]
+        assert _fastjson.loads('[1, 2]') == [1, 2]
+        raises(ValueError, "_fastjson.loads('[1: 2]')")
+        raises(ValueError, "_fastjson.loads('[1, 2')")


More information about the pypy-commit mailing list