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
_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
}\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
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
\r
}\r
\r
- public JSONObject(JSONTokener x) throws JSONException\r
+ public JSONObject(JSONTokenizer x) throws JSONException\r
{\r
this();\r
char c;\r
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;
+ }
}
}
* @author JSON.org\r
* @version 2008-09-18\r
*/\r
-public class JSONTokener\r
+public class JSONTokenizer\r
{\r
\r
private int index;\r
* @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
* @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
case ONE_OR_MORE:
if (columnData.startsWith("["))
{
- // micro-data format
+ // process JSON array
// 40000 lines/sec
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(",");
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());
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;
}
+ @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)
+
+ }
+
}