OSDN Git Service

rename
authorleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Mon, 1 Jun 2009 07:53:41 +0000 (07:53 +0000)
committerleo <leo@ae02f08e-27ec-0310-ae8c-8ba02fe2eafd>
Mon, 1 Jun 2009 07:53:41 +0000 (07:53 +0000)
git-svn-id: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core@3345 ae02f08e-27ec-0310-ae8c-8ba02fe2eafd

src/main/java/org/xerial/json/JSONArray.java
src/main/java/org/xerial/json/JSONObject.java
src/main/java/org/xerial/json/JSONStreamReader.java
src/main/java/org/xerial/json/impl/JSONTokenizer.java [moved from src/main/java/org/xerial/json/impl/JSONTokener.java with 95% similarity]
src/main/java/org/xerial/silk/SilkParser.java
src/test/java/org/xerial/json/JSONArrayTest.java
src/test/java/org/xerial/json/JSONStreamReaderTest.java

index 822b6ca..98e8f5c 100644 (file)
@@ -34,7 +34,7 @@ import org.antlr.runtime.RecognitionException;
 import org.antlr.runtime.tree.CommonTree;\r
 import org.xerial.json.impl.JSONLexer;\r
 import org.xerial.json.impl.JSONParser;\r
-import org.xerial.json.impl.JSONTokener;\r
+import org.xerial.json.impl.JSONTokenizer;\r
 \r
 public class JSONArray extends JSONValueBase implements Iterable<JSONValue>\r
 {\r
@@ -50,9 +50,9 @@ public class JSONArray extends JSONValueBase implements Iterable<JSONValue>
             _array.add(v);\r
     }\r
 \r
-    public JSONArray(JSONTokener x) throws JSONException\r
+    public JSONArray(JSONTokenizer tokenizer) throws JSONException\r
     {\r
-        char c = x.nextClean();\r
+        char c = tokenizer.nextClean();\r
         char q;\r
         if (c == '[')\r
         {\r
@@ -64,52 +64,52 @@ public class JSONArray extends JSONValueBase implements Iterable<JSONValue>
         }\r
         else\r
         {\r
-            throw x.syntaxError("A JSONArray text must start with '['");\r
+            throw tokenizer.syntaxError("A JSONArray text must start with '['");\r
         }\r
-        if (x.nextClean() == ']')\r
+        if (tokenizer.nextClean() == ']')\r
         {\r
             return;\r
         }\r
-        x.back();\r
+        tokenizer.back();\r
         for (;;)\r
         {\r
-            if (x.nextClean() == ',')\r
+            if (tokenizer.nextClean() == ',')\r
             {\r
-                x.back();\r
+                tokenizer.back();\r
                 _array.add(null);\r
             }\r
             else\r
             {\r
-                x.back();\r
-                _array.add(x.nextValue());\r
+                tokenizer.back();\r
+                _array.add(tokenizer.nextValue());\r
             }\r
-            c = x.nextClean();\r
+            c = tokenizer.nextClean();\r
             switch (c)\r
             {\r
             case ';':\r
             case ',':\r
-                if (x.nextClean() == ']')\r
+                if (tokenizer.nextClean() == ']')\r
                 {\r
                     return;\r
                 }\r
-                x.back();\r
+                tokenizer.back();\r
                 break;\r
             case ']':\r
             case ')':\r
                 if (q != c)\r
                 {\r
-                    throw x.syntaxError("Expected a '" + new Character(q) + "'");\r
+                    throw tokenizer.syntaxError("Expected a '" + new Character(q) + "'");\r
                 }\r
                 return;\r
             default:\r
-                throw x.syntaxError("Expected a ',' or ']'");\r
+                throw tokenizer.syntaxError("Expected a ',' or ']'");\r
             }\r
         }\r
     }\r
 \r
     public JSONArray(String jsonStr) throws JSONException\r
     {\r
-        this(new JSONTokener(jsonStr));\r
+        this(new JSONTokenizer(jsonStr));\r
     }\r
 \r
     public static CommonTree parse(String jsonStr) throws JSONException\r
index aedaae3..726b3c3 100644 (file)
@@ -39,7 +39,7 @@ import org.antlr.runtime.tree.CommonTree;
 import org.antlr.runtime.tree.CommonTreeNodeStream;\r
 import org.xerial.json.impl.JSONLexer;\r
 import org.xerial.json.impl.JSONParser;\r
-import org.xerial.json.impl.JSONTokener;\r
+import org.xerial.json.impl.JSONTokenizer;\r
 import org.xerial.json.impl.JSONWalker;\r
 \r
 /**\r
@@ -66,7 +66,7 @@ public class JSONObject extends JSONValueBase
 \r
     }\r
 \r
-    public JSONObject(JSONTokener x) throws JSONException\r
+    public JSONObject(JSONTokenizer x) throws JSONException\r
     {\r
         this();\r
         char c;\r
index 212f008..9dc5807 100644 (file)
@@ -128,14 +128,17 @@ public class JSONStreamReader implements TreeStreamReader
             String value = lastEvent != JSONEvent.Null ? jsonPullParser.getText() : null;
 
             // if first child element is value attribute
-            if (key.equals("value") && !pendingEventQueue.isEmpty())
+            if (key != null)
             {
-                TreeEvent e = pendingEventQueue.peekLast();
-                if (e.event == EventType.VISIT)
+                if (key.equals("value") && !pendingEventQueue.isEmpty())
                 {
-                    pendingEventQueue.removeLast();
-                    pendingEventQueue.addLast(TreeEvent.newVisitEvent(e.nodeName, value));
-                    break;
+                    TreeEvent e = pendingEventQueue.peekLast();
+                    if (e.event == EventType.VISIT)
+                    {
+                        pendingEventQueue.removeLast();
+                        pendingEventQueue.addLast(TreeEvent.newVisitEvent(e.nodeName, value));
+                        break;
+                    }
                 }
             }
 
@@ -73,7 +73,7 @@ SOFTWARE.
  * @author JSON.org\r
  * @version 2008-09-18\r
  */\r
-public class JSONTokener\r
+public class JSONTokenizer\r
 {\r
 \r
     private int index;\r
@@ -87,7 +87,7 @@ public class JSONTokener
      * @param reader\r
      *            A reader.\r
      */\r
-    public JSONTokener(Reader reader)\r
+    public JSONTokenizer(Reader reader)\r
     {\r
         this.reader = reader.markSupported() ? reader : new BufferedReader(reader);\r
         this.useLastChar = false;\r
@@ -100,7 +100,7 @@ public class JSONTokener
      * @param s\r
      *            A source string.\r
      */\r
-    public JSONTokener(String s)\r
+    public JSONTokenizer(String s)\r
     {\r
         this(new StringReader(s));\r
     }\r
index e10a5e9..a8791b6 100644 (file)
@@ -683,7 +683,7 @@ public class SilkParser implements SilkEventHandler
         case ONE_OR_MORE:
             if (columnData.startsWith("["))
             {
-                // micro-data format
+                // process JSON array
 
                 // 40000 lines/sec
 
index 3fe75ae..aa78c7a 100644 (file)
@@ -70,7 +70,8 @@ public class JSONArrayTest
         StringBuilder sample = new StringBuilder();
         sample.append("[");
         int i = 0;
-        for (; i < 1000; i++)
+        final int N = 5000;
+        for (; i < N - 1; i++)
         {
             sample.append(i);
             sample.append(",");
@@ -81,10 +82,10 @@ public class JSONArrayTest
         String json = sample.toString();
 
         StopWatch timer = new StopWatch();
-        for (int n = 0; n < 1000; n++)
+        for (int n = 0; n < 500; n++)
         {
             JSONArray array = new JSONArray(json);
-            assertEquals(1001, array.size());
+            assertEquals(N, array.size());
         }
         _logger.info("time: " + timer.getElapsedTime());
 
index 1cf375e..4564011 100644 (file)
@@ -26,11 +26,14 @@ package org.xerial.json;
 
 import static org.junit.Assert.*;
 
+import java.io.StringReader;
+
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Test;
 import org.xerial.util.FileResource;
 import org.xerial.util.HashedArrayList;
+import org.xerial.util.StopWatch;
 import org.xerial.util.log.Logger;
 import org.xerial.util.tree.TreeEvent;
 
@@ -87,4 +90,41 @@ public class JSONStreamReaderTest
 
     }
 
+    @Test
+    public void testParse() throws Exception
+    {
+
+        // generate a sample JSON array
+        StringBuilder sample = new StringBuilder();
+        sample.append("[");
+        int i = 0;
+        final int N = 5000;
+        for (; i < N; i++)
+        {
+            sample.append(i);
+            sample.append(",");
+        }
+        sample.append(i);
+        sample.append("]");
+
+        String json = sample.toString();
+
+        StopWatch timer = new StopWatch();
+
+        for (int n = 0; n < 500; n++)
+        {
+            JSONStreamReader reader = new JSONStreamReader(new StringReader(json));
+
+            TreeEvent e;
+            while ((e = reader.next()) != null)
+            {}
+
+        }
+        _logger.info("time: " + timer.getElapsedTime());
+
+        // i:1000, n:100   time=18.4 sec (2009.4.23 using ANTLR JSON.g)
+        // i:1000, n:100   time=2.248 (2009. 4.23 using JSONTokener)
+
+    }
+
 }