Debian Codegraph @

src/java/org/astrogrid/samp/JsonReader.java

raw
1 /*
2 * Copyright (C) 2011 Miami-Dade County.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 * Note: this file incorporates source code from 3d party entities. Such code
17 * is copyrighted by those entities as indicated below.
18 */
19
20 /*
21 * This code is based on the mjson library found here:
22 * http://www.sharegov.org/mjson/Json.java
23 * http://sharegov.blogspot.com/2011/06/json-library.html
24 */
25
26 package org.astrogrid.samp;
27
28 import java.text.CharacterIterator;
29 import java.text.StringCharacterIterator;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.LinkedHashMap;
33 import java.util.List;
34 import java.util.Map;
35
36 /**
37 * Simple JSON parser which only copes with SAMP-friendly JSON,
38 * that is strings, lists and objects.
39 * This code is a stripped-down and somewhat fixed copy of the mjson
40 * libraray written by Borislav Iordanov, from
41 * <a href="http://www.sharegov.org/mjson/Json.java"
42 * >http://www.sharegov.org/mjson/Json.java</a>.
43 *
44 * @author Borislav Iordanov
45 * @author Mark Taylor
46 */
47 class JsonReader {
48 private static final Object OBJECT_END = new Token("OBJECT_END");
49 private static final Object ARRAY_END = new Token("ARRAY_END");
50 private static final Object COLON = new Token("COLON");
51 private static final Object COMMA = new Token("COMMA");
52 public static final int FIRST = 0;
53 public static final int CURRENT = 1;
54 public static final int NEXT = 2;
55
56 private static Map escapes = new HashMap();
57 static
58 {
59 escapes.put(Character.valueOf('"'), Character.valueOf('"'));
60 escapes.put(Character.valueOf('\\'), Character.valueOf('\\'));
61 escapes.put(Character.valueOf('/'), Character.valueOf('/'));
62 escapes.put(Character.valueOf('b'), Character.valueOf('\b'));
63 escapes.put(Character.valueOf('f'), Character.valueOf('\f'));
64 escapes.put(Character.valueOf('n'), Character.valueOf('\n'));
65 escapes.put(Character.valueOf('r'), Character.valueOf('\r'));
66 escapes.put(Character.valueOf('t'), Character.valueOf('\t'));
67 }
68
69 private CharacterIterator it;
70 private char c;
71 private Object token;
72 private StringBuffer buf = new StringBuffer();
73
74 private char next()
75 {
76 if (it.getIndex() == it.getEndIndex())
77 throw new DataException("Reached end of input at the " +
78 it.getIndex() + "th character.");
79 c = it.next();
80 return c;
81 }
82
83 private char previous()
84 {
85 c = it.previous();
86 return c;
87 }
88
89 private void skipWhiteSpace()
90 {
91 do
92 {
93 if (Character.isWhitespace(c))
94 ;
95 else if (c == '/')
96 {
97 next();
98 if (c == '*')
99 {
100 // skip multiline comments
101 while (c != CharacterIterator.DONE)
102 if (next() == '*' && next() == '/')
103 break;
104 if (c == CharacterIterator.DONE)
105 throw new DataException("Unterminated comment while parsing JSON string.");
106 }
107 else if (c == '/')
108 while (c != '\n' && c != CharacterIterator.DONE)
109 next();
110 else
111 {
112 previous();
113 break;
114 }
115 }
116 else
117 break;
118 } while (next() != CharacterIterator.DONE);
119 }
120
121 public Object read(CharacterIterator ci, int start)
122 {
123 it = ci;
124 switch (start)
125 {
126 case FIRST:
127 c = it.first();
128 break;
129 case CURRENT:
130 c = it.current();
131 break;
132 case NEXT:
133 c = it.next();
134 break;
135 }
136 return read();
137 }
138
139 public Object read(CharacterIterator it)
140 {
141 return read(it, NEXT);
142 }
143
144 public Object read(String string)
145 {
146 return read(new StringCharacterIterator(string), FIRST);
147 }
148
149 private Object read()
150 {
151 skipWhiteSpace();
152 char ch = c;
153 next();
154 switch (ch)
155 {
156 case '"': token = readString(); break;
157 case '[': token = readArray(); break;
158 case ']': token = ARRAY_END; break;
159 case ',': token = COMMA; break;
160 case '{': token = readObject(); break;
161 case '}': token = OBJECT_END; break;
162 case ':': token = COLON; break;
163 default: {
164 throw new DataException( "Unexpected character '" + ch + "'" );
165 }
166 }
167 return token;
168 }
169
170 private Map readObject()
171 {
172 Map ret = new LinkedHashMap();
173 read();
174 while (true) {
175 if (token == OBJECT_END) {
176 return ret;
177 }
178 if (!(token instanceof String)) {
179 throw new DataException("Missing/illegal object key");
180 }
181 String key = (String) token;
182 if (read() != COLON) {
183 throw new DataException("Missing colon in JSON object");
184 }
185 Object value = read();
186 ret.put(key, value);
187 read();
188 if (token == COMMA) {
189 read();
190 }
191 else if (token != OBJECT_END) {
192 throw new DataException("Unexpected token " + token);
193 }
194 }
195 }
196
197 private List readArray()
198 {
199 List ret = new ArrayList();
200 Object value = read();
201 while (token != ARRAY_END)
202 {
203 ret.add(value);
204 if (read() == COMMA)
205 value = read();
206 else if (token != ARRAY_END)
207 throw new DataException("Unexpected token in array " + token);
208 }
209 return ret;
210 }
211
212 private String readString()
213 {
214 buf.setLength(0);
215 while (c != '"')
216 {
217 if (c == '\\')
218 {
219 next();
220 if (c == 'u')
221 {
222 add(unicode());
223 }
224 else
225 {
226 Object value = escapes.get(Character.valueOf(c));
227 if (value != null)
228 {
229 add(((Character) value).charValue());
230 }
231 }
232 }
233 else
234 {
235 add();
236 }
237 }
238 next();
239 return buf.toString();
240 }
241
242 private void add(char cc)
243 {
244 buf.append(cc);
245 next();
246 }
247
248 private void add()
249 {
250 add(c);
251 }
252
253 private char unicode()
254 {
255 int value = 0;
256 for (int i = 0; i < 4; ++i)
257 {
258 switch (next())
259 {
260 case '0': case '1': case '2': case '3': case '4':
261 case '5': case '6': case '7': case '8': case '9':
262 value = (value << 4) + c - '0';
263 break;
264 case 'a': case 'b': case 'c': case 'd': case 'e': case 'f':
265 value = (value << 4) + (c - 'a') + 10;
266 break;
267 case 'A': case 'B': case 'C': case 'D': case 'E': case 'F':
268 value = (value << 4) + (c - 'A') + 10;
269 break;
270 }
271 }
272 return (char) value;
273 }
274
275 /**
276 * Named object.
277 */
278 private static class Token {
279 private final String name;
280 Token(String name) {
281 this.name = name;
282 }
283 public String toString() {
284 return this.name;
285 }
286 }
287 }