OSDN Git Service

imported from subversion repository
[xerial/xerial-core.git] / src / test / java / org / xerial / json / JSONStreamReaderTest.java
1 /*--------------------------------------------------------------------------\r
2  *  Copyright 2009 Taro L. Saito\r
3  *\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
7  *\r
8  *     http://www.apache.org/licenses/LICENSE-2.0\r
9  *\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
17 // XerialJ\r
18 //\r
19 // JSONStreamReaderTest.java\r
20 // Since: Mar 31, 2009 2:49:53 PM\r
21 //\r
22 // $URL: http://www.xerial.org/svn/project/XerialJ/trunk/xerial-core/src/test/java/org/xerial/json/JSONStreamReaderTest.java $\r
23 // $Author: leo $\r
24 //--------------------------------------\r
25 package org.xerial.json;\r
26 \r
27 import static org.junit.Assert.*;\r
28 \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
41 \r
42 public class JSONStreamReaderTest\r
43 {\r
44     private static Logger _logger = Logger.getLogger(JSONStreamReaderTest.class);\r
45 \r
46     @Before\r
47     public void setUp() throws Exception\r
48     {}\r
49 \r
50     @After\r
51     public void tearDown() throws Exception\r
52     {}\r
53 \r
54     HashedArrayList<String, String> parse(String file) throws Exception\r
55     {\r
56         JSONStreamReader reader = new JSONStreamReader(FileResource.open(JSONStreamReaderTest.class, file));\r
57         TreeEvent e;\r
58 \r
59         HashedArrayList<String, String> data = new HashedArrayList<String, String>();\r
60 \r
61         while ((e = reader.next()) != null)\r
62         {\r
63             _logger.debug(e);\r
64 \r
65             if (e.isVisit())\r
66                 data.put(e.nodeName, e.nodeValue);\r
67         }\r
68 \r
69         return data;\r
70     }\r
71 \r
72     @Test\r
73     public void testNext() throws Exception\r
74     {\r
75         HashedArrayList<String, String> data = parse("sample.json");\r
76 \r
77         assertEquals("Leo", data.get("name").get(0));\r
78         assertEquals("100", data.get("id").get(0));\r
79     }\r
80 \r
81     @Test\r
82     public void testArray() throws Exception\r
83     {\r
84         HashedArrayList<String, String> data = parse("array.json");\r
85 \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
89 \r
90         assertEquals("Relational-Style XML Query", data.get("title").get(0));\r
91         assertNull(data.get("paper").get(0));\r
92 \r
93     }\r
94 \r
95     @Test\r
96     public void testParse() throws Exception\r
97     {\r
98 \r
99         // generate a sample JSON array\r
100         StringBuilder sample = new StringBuilder();\r
101         sample.append("[");\r
102         int i = 0;\r
103         final int N = 5000;\r
104         for (; i < N; i++)\r
105         {\r
106             sample.append(i);\r
107             sample.append(",");\r
108         }\r
109         sample.append(i);\r
110         sample.append("]");\r
111 \r
112         String json = sample.toString();\r
113 \r
114         StopWatch timer = new StopWatch();\r
115 \r
116         for (int n = 0; n < 500; n++)\r
117         {\r
118             JSONPullParser parser = new JSONPullParser(json);\r
119 \r
120             JSONEvent e;\r
121             while ((e = parser.next()) != JSONEvent.EndJSON)\r
122             {}\r
123 \r
124         }\r
125         _logger.info("time: " + timer.getElapsedTime());\r
126 \r
127     }\r
128 \r
129     @Test\r
130     public void testLexerPerformance() throws Exception\r
131     {\r
132 \r
133         // generate a sample JSON array\r
134         StringBuilder sample = new StringBuilder();\r
135         sample.append("[");\r
136         int i = 0;\r
137         final int N = 5000;\r
138         for (; i < N; i++)\r
139         {\r
140             sample.append(i);\r
141             sample.append(",");\r
142         }\r
143         sample.append(i);\r
144         sample.append("]");\r
145 \r
146         String json = sample.toString();\r
147 \r
148         StopWatch timer = new StopWatch();\r
149 \r
150         for (int n = 0; n < 500; n++)\r
151         {\r
152             JSONLexer lexer = new JSONLexer(new ANTLRStringStream(json));\r
153 \r
154             Token t;\r
155             while ((t = lexer.nextToken()).getType() != Token.EOF)\r
156             {}\r
157 \r
158         }\r
159         _logger.info("time: " + timer.getElapsedTime());\r
160 \r
161     }\r
162 \r
163     @Test\r
164     public void testJSONTokenerPeformance() throws Exception\r
165     {\r
166 \r
167         // generate a sample JSON array\r
168         StringBuilder sample = new StringBuilder();\r
169         sample.append("[");\r
170         int i = 0;\r
171         final int N = 5000;\r
172         for (; i < N; i++)\r
173         {\r
174             sample.append(i);\r
175             sample.append(",");\r
176         }\r
177         sample.append(i);\r
178         sample.append("]");\r
179 \r
180         String json = sample.toString();\r
181 \r
182         StopWatch timer = new StopWatch();\r
183 \r
184         for (int n = 0; n < 500; n++)\r
185         {\r
186             JSONTokenizer tokenizer = new JSONTokenizer(json);\r
187 \r
188             parseArray(tokenizer);\r
189 \r
190         }\r
191         _logger.info("time: " + timer.getElapsedTime());\r
192 \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
195 \r
196     }\r
197 \r
198     public void parseArray(JSONTokenizer tokenizer) throws JSONException\r
199     {\r
200         char c = tokenizer.nextClean();\r
201         char q;\r
202         if (c == '[')\r
203         {\r
204             q = ']';\r
205         }\r
206         else if (c == '(')\r
207         {\r
208             q = ')';\r
209         }\r
210         else\r
211         {\r
212             throw tokenizer.syntaxError("A JSONArray text must start with '['");\r
213         }\r
214         if (tokenizer.nextClean() == ']')\r
215         {\r
216             return;\r
217         }\r
218         tokenizer.back();\r
219         for (;;)\r
220         {\r
221             if (tokenizer.nextClean() == ',')\r
222             {\r
223                 tokenizer.back();\r
224                 //_array.add(null);\r
225             }\r
226             else\r
227             {\r
228                 tokenizer.back();\r
229                 tokenizer.nextValue();\r
230                 //_array.add(tokenizer.nextValue());\r
231             }\r
232             c = tokenizer.nextClean();\r
233             switch (c)\r
234             {\r
235             case ';':\r
236             case ',':\r
237                 if (tokenizer.nextClean() == ']')\r
238                 {\r
239                     return;\r
240                 }\r
241                 tokenizer.back();\r
242                 break;\r
243             case ']':\r
244             case ')':\r
245                 if (q != c)\r
246                 {\r
247                     throw tokenizer.syntaxError("Expected a '" + new Character(q) + "'");\r
248                 }\r
249                 return;\r
250             default:\r
251                 throw tokenizer.syntaxError("Expected a ',' or ']'");\r
252             }\r
253         }\r
254     }\r
255 \r
256 }\r