| 1 |
/* |
| 2 |
* Copyright 2009 Borislav Iordanov |
| 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 |
| 13 |
* implied. |
| 14 |
* See the License for the specific language governing permissions and |
| 15 |
* limitations under the License. |
| 16 |
*/ |
| 17 |
|
| 18 |
package org.linkgrammar; |
| 19 |
|
| 20 |
import java.math.BigDecimal; |
| 21 |
import java.math.BigInteger; |
| 22 |
import java.text.CharacterIterator; |
| 23 |
import java.text.StringCharacterIterator; |
| 24 |
import java.util.ArrayList; |
| 25 |
import java.util.HashMap; |
| 26 |
import java.util.List; |
| 27 |
import java.util.Map; |
| 28 |
|
| 29 |
public class JSONReader |
| 30 |
{ |
| 31 |
private static final Object OBJECT_END = new Object(); |
| 32 |
private static final Object ARRAY_END = new Object(); |
| 33 |
private static final Object COLON = new Object(); |
| 34 |
private static final Object COMMA = new Object(); |
| 35 |
public static final int FIRST = 0; |
| 36 |
public static final int CURRENT = 1; |
| 37 |
public static final int NEXT = 2; |
| 38 |
|
| 39 |
private static Map<Character, Character> escapes = new HashMap<Character, Character>(); |
| 40 |
static |
| 41 |
{ |
| 42 |
escapes.put(Character.valueOf('"'), Character.valueOf('"')); |
| 43 |
escapes.put(Character.valueOf('\\'), Character.valueOf('\\')); |
| 44 |
escapes.put(Character.valueOf('/'), Character.valueOf('/')); |
| 45 |
escapes.put(Character.valueOf('b'), Character.valueOf('\b')); |
| 46 |
escapes.put(Character.valueOf('f'), Character.valueOf('\f')); |
| 47 |
escapes.put(Character.valueOf('n'), Character.valueOf('\n')); |
| 48 |
escapes.put(Character.valueOf('r'), Character.valueOf('\r')); |
| 49 |
escapes.put(Character.valueOf('t'), Character.valueOf('\t')); |
| 50 |
} |
| 51 |
|
| 52 |
private CharacterIterator it; |
| 53 |
private char c; |
| 54 |
// private Object token; |
| 55 |
private StringBuffer buf = new StringBuffer(); |
| 56 |
|
| 57 |
private void error(String msg) |
| 58 |
{ |
| 59 |
throw new RuntimeException("JSON parse error near position " + |
| 60 |
it.getIndex() + ":" + msg); |
| 61 |
} |
| 62 |
|
| 63 |
private char next() |
| 64 |
{ |
| 65 |
c = it.next(); |
| 66 |
return c; |
| 67 |
} |
| 68 |
|
| 69 |
private char previous() |
| 70 |
{ |
| 71 |
c = it.previous(); |
| 72 |
return c; |
| 73 |
} |
| 74 |
|
| 75 |
private void skipWhiteSpace() |
| 76 |
{ |
| 77 |
do |
| 78 |
{ |
| 79 |
if (Character.isWhitespace(c)) |
| 80 |
; |
| 81 |
else if (c == '/') |
| 82 |
{ |
| 83 |
next(); |
| 84 |
if (c == '*') |
| 85 |
{ |
| 86 |
// skip multiline comments |
| 87 |
while (c != CharacterIterator.DONE) |
| 88 |
if (next() == '*' && next() == '/') |
| 89 |
break; |
| 90 |
if (c == CharacterIterator.DONE) |
| 91 |
throw new RuntimeException( |
| 92 |
"Unterminated comment while parsing JSON string."); |
| 93 |
} |
| 94 |
else if (c == '/') |
| 95 |
while (c != '\n' && c != CharacterIterator.DONE) |
| 96 |
next(); |
| 97 |
else |
| 98 |
{ |
| 99 |
previous(); |
| 100 |
break; |
| 101 |
} |
| 102 |
} |
| 103 |
else |
| 104 |
break; |
| 105 |
} while (next() != CharacterIterator.DONE); |
| 106 |
} |
| 107 |
|
| 108 |
public Object read(CharacterIterator ci, int start) |
| 109 |
{ |
| 110 |
it = ci; |
| 111 |
switch (start) |
| 112 |
{ |
| 113 |
case FIRST: |
| 114 |
c = it.first(); |
| 115 |
break; |
| 116 |
case CURRENT: |
| 117 |
c = it.current(); |
| 118 |
break; |
| 119 |
case NEXT: |
| 120 |
c = it.next(); |
| 121 |
break; |
| 122 |
} |
| 123 |
return read(); |
| 124 |
} |
| 125 |
|
| 126 |
public Object read(CharacterIterator it) |
| 127 |
{ |
| 128 |
return read(it, NEXT); |
| 129 |
} |
| 130 |
|
| 131 |
public Object read(String string) |
| 132 |
{ |
| 133 |
return read(new StringCharacterIterator(string), FIRST); |
| 134 |
} |
| 135 |
|
| 136 |
private Object read() |
| 137 |
{ |
| 138 |
skipWhiteSpace(); |
| 139 |
char ch = c; |
| 140 |
next(); |
| 141 |
Object value = null; |
| 142 |
switch (ch) |
| 143 |
{ |
| 144 |
case '"': |
| 145 |
value = string(); |
| 146 |
break; |
| 147 |
case '[': |
| 148 |
value = array(); |
| 149 |
break; |
| 150 |
case ']': |
| 151 |
value = ARRAY_END; |
| 152 |
break; |
| 153 |
case ',': |
| 154 |
value = COMMA; |
| 155 |
break; |
| 156 |
case '{': |
| 157 |
value = object(); |
| 158 |
break; |
| 159 |
case '}': |
| 160 |
value = OBJECT_END; |
| 161 |
break; |
| 162 |
case ':': |
| 163 |
value = COLON; |
| 164 |
break; |
| 165 |
case 't': |
| 166 |
if (c != 'r' || next() != 'u' || next() != 'e') |
| 167 |
error("Invalid JSON token: expected 'true' keyword."); |
| 168 |
next(); |
| 169 |
value = Boolean.TRUE; |
| 170 |
break; |
| 171 |
case 'f': |
| 172 |
if (c != 'a' || next() != 'l' || next() != 's' || next() != 'e') |
| 173 |
error( "Invalid JSON token: expected 'false' keyword."); |
| 174 |
next(); |
| 175 |
value = Boolean.FALSE; |
| 176 |
break; |
| 177 |
case 'n': |
| 178 |
if (c != 'u' || next() != 'l' || next() != 'l') |
| 179 |
error("Invalid JSON token: expected 'null' keyword."); |
| 180 |
next(); |
| 181 |
value = null; |
| 182 |
break; |
| 183 |
default: |
| 184 |
c = it.previous(); |
| 185 |
if (Character.isDigit(c) || c == '-') |
| 186 |
value = number(); |
| 187 |
else |
| 188 |
error("Invalid character '" + c + "', expecting a JSON token."); |
| 189 |
} |
| 190 |
return value; |
| 191 |
} |
| 192 |
|
| 193 |
private boolean isValue(Object x) |
| 194 |
{ |
| 195 |
return x == null || |
| 196 |
x instanceof Boolean || |
| 197 |
x instanceof String || |
| 198 |
x instanceof Number || |
| 199 |
x instanceof Map<?,?> || |
| 200 |
x instanceof List<?>; |
| 201 |
} |
| 202 |
|
| 203 |
private Object object() |
| 204 |
{ |
| 205 |
Map<Object, Object> ret = new HashMap<Object, Object>(); |
| 206 |
Object key = read(); |
| 207 |
while (key != OBJECT_END) |
| 208 |
{ |
| 209 |
if (! (key instanceof String)) |
| 210 |
error("Expecting a string as key in object, but got '" + key + "'"); |
| 211 |
Object colon = read(); |
| 212 |
if (colon != COLON) |
| 213 |
error("Expecting a colon, but found '" + |
| 214 |
colon + "' after object key '" + key + "'"); |
| 215 |
Object value = read(); |
| 216 |
if (!isValue(value)) |
| 217 |
error("Unexpected value in object with key '" + key + "' -- '" + value + |
| 218 |
"', expecting boolean, number string, array or object."); |
| 219 |
ret.put(key, value); |
| 220 |
key = read(); |
| 221 |
if (key == COMMA) |
| 222 |
key = read(); |
| 223 |
else if (key != OBJECT_END) |
| 224 |
error("Unexpected token in object '" + key + "', expecting comma or }."); |
| 225 |
} |
| 226 |
return ret; |
| 227 |
} |
| 228 |
|
| 229 |
private Object array() |
| 230 |
{ |
| 231 |
List<Object> ret = new ArrayList<Object>(); |
| 232 |
Object value = read(); |
| 233 |
while (value != ARRAY_END) |
| 234 |
{ |
| 235 |
if (!isValue(value)) |
| 236 |
error("Unexpected value in array '" + value + |
| 237 |
"', expecting boolean, number string, array or object."); |
| 238 |
ret.add(value); |
| 239 |
value = read(); |
| 240 |
if (value == COMMA) |
| 241 |
value = read(); |
| 242 |
else if (value != ARRAY_END) |
| 243 |
error("Unexpected token in array '" + |
| 244 |
value + "', expecting comma or ]."); |
| 245 |
} |
| 246 |
return ret; |
| 247 |
} |
| 248 |
|
| 249 |
private Object number() |
| 250 |
{ |
| 251 |
int length = 0; |
| 252 |
boolean isFloatingPoint = false; |
| 253 |
buf.setLength(0); |
| 254 |
|
| 255 |
if (c == '-') |
| 256 |
{ |
| 257 |
add(c); |
| 258 |
} |
| 259 |
length += addDigits(); |
| 260 |
if (c == '.') |
| 261 |
{ |
| 262 |
add(c); |
| 263 |
length += addDigits(); |
| 264 |
isFloatingPoint = true; |
| 265 |
} |
| 266 |
if (c == 'e' || c == 'E') |
| 267 |
{ |
| 268 |
add(c); |
| 269 |
if (c == '+' || c == '-') |
| 270 |
{ |
| 271 |
add(c); |
| 272 |
} |
| 273 |
addDigits(); |
| 274 |
isFloatingPoint = true; |
| 275 |
} |
| 276 |
|
| 277 |
String s = buf.toString(); |
| 278 |
return isFloatingPoint ? (length < 17) ? (Object) Double.valueOf(s) |
| 279 |
: new BigDecimal(s) : (length < 20) ? (Object) Long.valueOf(s) |
| 280 |
: new BigInteger(s); |
| 281 |
} |
| 282 |
|
| 283 |
private int addDigits() |
| 284 |
{ |
| 285 |
int ret; |
| 286 |
for (ret = 0; Character.isDigit(c); ++ret) |
| 287 |
{ |
| 288 |
add(c); |
| 289 |
} |
| 290 |
return ret; |
| 291 |
} |
| 292 |
|
| 293 |
private Object string() |
| 294 |
{ |
| 295 |
buf.setLength(0); |
| 296 |
while (c != '"') |
| 297 |
{ |
| 298 |
if (c == '\\') |
| 299 |
{ |
| 300 |
next(); |
| 301 |
if (c == 'u') |
| 302 |
{ |
| 303 |
add(unicode()); |
| 304 |
} |
| 305 |
else |
| 306 |
{ |
| 307 |
Character value = escapes.get(Character.valueOf(c)); |
| 308 |
if (value != null) |
| 309 |
add(value); |
| 310 |
else |
| 311 |
add(c); |
| 312 |
} |
| 313 |
} |
| 314 |
else |
| 315 |
{ |
| 316 |
add(c); |
| 317 |
} |
| 318 |
} |
| 319 |
next(); |
| 320 |
return buf.toString(); |
| 321 |
} |
| 322 |
|
| 323 |
private void add(char cc) |
| 324 |
{ |
| 325 |
buf.append(cc); |
| 326 |
next(); |
| 327 |
} |
| 328 |
|
| 329 |
private char unicode() |
| 330 |
{ |
| 331 |
int value = 0; |
| 332 |
for (int i = 0; i < 4; ++i) |
| 333 |
{ |
| 334 |
switch (next()) |
| 335 |
{ |
| 336 |
case '0': |
| 337 |
case '1': |
| 338 |
case '2': |
| 339 |
case '3': |
| 340 |
case '4': |
| 341 |
case '5': |
| 342 |
case '6': |
| 343 |
case '7': |
| 344 |
case '8': |
| 345 |
case '9': |
| 346 |
value = (value << 4) + c - '0'; |
| 347 |
break; |
| 348 |
case 'a': |
| 349 |
case 'b': |
| 350 |
case 'c': |
| 351 |
case 'd': |
| 352 |
case 'e': |
| 353 |
case 'f': |
| 354 |
value = (value << 4) + (c - 'a') + 10; |
| 355 |
break; |
| 356 |
case 'A': |
| 357 |
case 'B': |
| 358 |
case 'C': |
| 359 |
case 'D': |
| 360 |
case 'E': |
| 361 |
case 'F': |
| 362 |
value = (value << 4) + (c - 'A') + 10; |
| 363 |
break; |
| 364 |
} |
| 365 |
} |
| 366 |
return (char) value; |
| 367 |
} |
| 368 |
} |