1 /*--------------------------------------------------------------------------
\r
2 * Copyright 2009 Taro L. Saito
\r
4 * Licensed under the Apache License, Version 2.0 (the "License");
\r
5 * you may not use this file except in compliance with the License.
\r
6 * You may obtain a copy of the License at
\r
8 * http://www.apache.org/licenses/LICENSE-2.0
\r
10 * Unless required by applicable law or agreed to in writing, software
\r
11 * distributed under the License is distributed on an "AS IS" BASIS,
\r
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
13 * See the License for the specific language governing permissions and
\r
14 * limitations under the License.
\r
15 *--------------------------------------------------------------------------*/
\r
16 //--------------------------------------
\r
19 // JSONStreamReaderTest.java
\r
20 // Since: Mar 31, 2009 2:49:53 PM
\r
22 // $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/test/java/org/xerial/json/JSONStreamReaderTest.java $
\r
24 //--------------------------------------
\r
25 package org.xerial.json;
\r
27 import static org.junit.Assert.*;
\r
29 import org.antlr.runtime.ANTLRStringStream;
\r
30 import org.antlr.runtime.Token;
\r
31 import org.junit.After;
\r
32 import org.junit.Before;
\r
33 import org.junit.Test;
\r
34 import org.xerial.json.impl.JSONLexer;
\r
35 import org.xerial.json.impl.JSONTokenizer;
\r
36 import org.xerial.util.FileResource;
\r
37 import org.xerial.util.HashedArrayList;
\r
38 import org.xerial.util.StopWatch;
\r
39 import org.xerial.util.log.Logger;
\r
40 import org.xerial.util.tree.TreeEvent;
\r
42 public class JSONStreamReaderTest
\r
44 private static Logger _logger = Logger.getLogger(JSONStreamReaderTest.class);
\r
47 public void setUp() throws Exception
\r
51 public void tearDown() throws Exception
\r
54 HashedArrayList<String, String> parse(String file) throws Exception
\r
56 JSONStreamReader reader = new JSONStreamReader(FileResource.open(JSONStreamReaderTest.class, file));
\r
59 HashedArrayList<String, String> data = new HashedArrayList<String, String>();
\r
61 while ((e = reader.next()) != null)
\r
66 data.put(e.nodeName, e.nodeValue);
\r
73 public void testNext() throws Exception
\r
75 HashedArrayList<String, String> data = parse("sample.json");
\r
77 assertEquals("Leo", data.get("name").get(0));
\r
78 assertEquals("100", data.get("id").get(0));
\r
82 public void testArray() throws Exception
\r
84 HashedArrayList<String, String> data = parse("array.json");
\r
86 assertEquals(2, data.get("author").size());
\r
87 assertEquals("leo", data.get("author").get(0));
\r
88 assertEquals("yui", data.get("author").get(1));
\r
90 assertEquals("Relational-Style XML Query", data.get("title").get(0));
\r
91 assertNull(data.get("paper").get(0));
\r
96 public void testParse() throws Exception
\r
99 // generate a sample JSON array
\r
100 StringBuilder sample = new StringBuilder();
\r
101 sample.append("[");
\r
103 final int N = 5000;
\r
107 sample.append(",");
\r
110 sample.append("]");
\r
112 String json = sample.toString();
\r
114 StopWatch timer = new StopWatch();
\r
116 for (int n = 0; n < 500; n++)
\r
118 JSONPullParser parser = new JSONPullParser(json);
\r
121 while ((e = parser.next()) != JSONEvent.EndJSON)
\r
125 _logger.info("time: " + timer.getElapsedTime());
\r
130 public void testLexerPerformance() throws Exception
\r
133 // generate a sample JSON array
\r
134 StringBuilder sample = new StringBuilder();
\r
135 sample.append("[");
\r
137 final int N = 5000;
\r
141 sample.append(",");
\r
144 sample.append("]");
\r
146 String json = sample.toString();
\r
148 StopWatch timer = new StopWatch();
\r
150 for (int n = 0; n < 500; n++)
\r
152 JSONLexer lexer = new JSONLexer(new ANTLRStringStream(json));
\r
155 while ((t = lexer.nextToken()).getType() != Token.EOF)
\r
159 _logger.info("time: " + timer.getElapsedTime());
\r
164 public void testJSONTokenerPeformance() throws Exception
\r
167 // generate a sample JSON array
\r
168 StringBuilder sample = new StringBuilder();
\r
169 sample.append("[");
\r
171 final int N = 5000;
\r
175 sample.append(",");
\r
178 sample.append("]");
\r
180 String json = sample.toString();
\r
182 StopWatch timer = new StopWatch();
\r
184 for (int n = 0; n < 500; n++)
\r
186 JSONTokenizer tokenizer = new JSONTokenizer(json);
\r
188 parseArray(tokenizer);
\r
191 _logger.info("time: " + timer.getElapsedTime());
\r
193 // i:1000, n:100 time=18.4 sec (2009.4.23 using ANTLR JSON.g)
\r
194 // i:1000, n:100 time=2.248 (2009. 4.23 using JSONTokener)
\r
198 public void parseArray(JSONTokenizer tokenizer) throws JSONException
\r
200 char c = tokenizer.nextClean();
\r
212 throw tokenizer.syntaxError("A JSONArray text must start with '['");
\r
214 if (tokenizer.nextClean() == ']')
\r
221 if (tokenizer.nextClean() == ',')
\r
224 //_array.add(null);
\r
229 tokenizer.nextValue();
\r
230 //_array.add(tokenizer.nextValue());
\r
232 c = tokenizer.nextClean();
\r
237 if (tokenizer.nextClean() == ']')
\r
247 throw tokenizer.syntaxError("Expected a '" + new Character(q) + "'");
\r
251 throw tokenizer.syntaxError("Expected a ',' or ']'");
\r