| 1 |
package json; |
| 2 |
|
| 3 |
import java.io.PrintStream; |
| 4 |
import java.util.ArrayList; |
| 5 |
|
| 6 |
import structures.ByteBuilder; |
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
public class JsonParser { |
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
public static void main(String[] args){ |
| 24 |
String s; |
| 25 |
|
| 26 |
s="{\n" |
| 27 |
+" \"33154\": {\n" |
| 28 |
+" \"name\": \"Opisthokonta\",\n" |
| 29 |
+" \"tax_id\": 33154,\n" |
| 30 |
+" \"level\": \"no rank\",\n" |
| 31 |
+" \"no rank\": {\n" |
| 32 |
+" \"name\": \"Opisthokonta\",\n" |
| 33 |
+" \"tax_id\": 33154\n" |
| 34 |
+" },\n" |
| 35 |
+" \"foo\": {\n" |
| 36 |
+" \"bar\": \"bam\",\n" |
| 37 |
+" \"sam\": \"cram\"\n" |
| 38 |
+" },\n" |
| 39 |
+" \"foo2\": {\n" |
| 40 |
+" \"true\": false\n" |
| 41 |
+" },\n" |
| 42 |
+" \"foo3\": {\n" |
| 43 |
+" \"null\": null\n" |
| 44 |
+" },\n" |
| 45 |
+" \"foo4\": {\n" |
| 46 |
+" \"null\": invalid\n" |
| 47 |
+" },\n" |
| 48 |
+" \"superkingdom\": {\n" |
| 49 |
+" \"name\": \"Eukaryota\",\n" |
| 50 |
+" \"tax_id\": 2759,\n" |
| 51 |
+" \"number1\": 2759,\n" |
| 52 |
+" \"number2\": -2759,\n" |
| 53 |
+" \"number3\": .2759,\n" |
| 54 |
+" \"number4\": 2.759,\n" |
| 55 |
+" \"number5\": -2.759,\n" |
| 56 |
+" \"number6\": -2.759e17,\n" |
| 57 |
+" \"number7\": -2.759e-1,\n" |
| 58 |
+" \"number8\": -2.759E-1,\n" |
| 59 |
+" \"number9\": -2E-1,\n" |
| 60 |
+" \"slash\": \"hello \\\"world\\\"\",\n" |
| 61 |
+" \"slash\": \"hello world\",\n" |
| 62 |
+" \"complex\": [\"hello world\", 1, {\"tax_id\": 2759}, [3, 4, 5]]\n" |
| 63 |
+" }\n" |
| 64 |
+" }\n" |
| 65 |
+"}"; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
System.out.println("Original:\n"+s); |
| 70 |
JsonParser jp=new JsonParser(s); |
| 71 |
JsonObject j=jp.parseJsonObject(); |
| 72 |
System.out.println("Original:\n"+s); |
| 73 |
System.out.println("Regenerated:\n"+j); |
| 74 |
|
| 75 |
s="[\"complex\", 1, {\"b\": 2}, [3, 4, 5]]"; |
| 76 |
|
| 77 |
System.out.println("Original:\n"+s); |
| 78 |
jp.set(s.getBytes()); |
| 79 |
Object[] array=jp.parseJsonArray(); |
| 80 |
System.out.println("Original:\n"+s); |
| 81 |
System.out.println("Regenerated:\n"+JsonObject.toString(array)); |
| 82 |
} |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public JsonParser(){} |
| 89 |
|
| 90 |
public JsonParser(String s){ |
| 91 |
set(s.getBytes()); |
| 92 |
} |
| 93 |
|
| 94 |
public JsonParser(byte[] s){ |
| 95 |
set(s); |
| 96 |
} |
| 97 |
|
| 98 |
public static JsonObject parseJsonObjectStatic(String s){ |
| 99 |
if(s==null || s.length()<1) {return null;} |
| 100 |
return new JsonParser(s).parseJsonObject(); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public JsonParser set(byte[] s){ |
| 108 |
text=s; |
| 109 |
pos=0; |
| 110 |
errorState=false; |
| 111 |
return this; |
| 112 |
} |
| 113 |
|
| 114 |
public JsonObject parseJsonObject(String s){ |
| 115 |
set(s.getBytes()); |
| 116 |
return parseJsonObject(); |
| 117 |
} |
| 118 |
|
| 119 |
public JsonObject parseJsonObject(byte[] s){ |
| 120 |
set(s); |
| 121 |
return parseJsonObject(); |
| 122 |
} |
| 123 |
|
| 124 |
public Object[] parseJsonArray(String s){ |
| 125 |
set(s.getBytes()); |
| 126 |
return parseJsonArray(); |
| 127 |
} |
| 128 |
|
| 129 |
public Object[] parseJsonArray(byte[] s){ |
| 130 |
set(s); |
| 131 |
return parseJsonArray(); |
| 132 |
} |
| 133 |
|
| 134 |
public JsonObject parseJsonObject(){ |
| 135 |
if(text==null || text.length<1){return null;} |
| 136 |
assert(text[0]=='{') : text[0]+"\n"+new String(text); |
| 137 |
JsonObject o=makeObject(); |
| 138 |
return o; |
| 139 |
} |
| 140 |
|
| 141 |
public Object[] parseJsonArray(){ |
| 142 |
if(text==null || text.length<1){return null;} |
| 143 |
assert(text[0]=='[') : text[0]+"\n"+new String(text); |
| 144 |
Object[] array=makeArray(); |
| 145 |
return array; |
| 146 |
} |
| 147 |
|
| 148 |
public boolean validate(){ |
| 149 |
if(text==null || text.length<1){return true;} |
| 150 |
try { |
| 151 |
if(text[0]=='['){ |
| 152 |
Object[] array=parseJsonArray(); |
| 153 |
return !errorState; |
| 154 |
}else if(text[0]=='{'){ |
| 155 |
JsonObject o=parseJsonObject(); |
| 156 |
return !errorState; |
| 157 |
} |
| 158 |
} catch (Throwable e) {} |
| 159 |
return false; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
private Object bufferToObject(ByteBuilder bb){ |
| 168 |
String s=bb.toString(); |
| 169 |
bb.clear(); |
| 170 |
final char firstLetter=s.length()>0 ? s.charAt(0) : 0; |
| 171 |
Object value; |
| 172 |
try { |
| 173 |
if(Character.isLetter(firstLetter)){ |
| 174 |
if(verbose){outstream.println("Letter");} |
| 175 |
if(s.equalsIgnoreCase("null")){ |
| 176 |
value=null; |
| 177 |
}else{ |
| 178 |
|
| 179 |
value=parseBoolean(s); |
| 180 |
} |
| 181 |
}else{ |
| 182 |
if(verbose){outstream.println("Number");} |
| 183 |
if(s.indexOf('.')>=0 || s.indexOf('e')>=0 || s.indexOf('E')>=0){ |
| 184 |
value=Double.parseDouble(s); |
| 185 |
}else{ |
| 186 |
value=Long.parseLong(s); |
| 187 |
} |
| 188 |
} |
| 189 |
} catch (Exception e) { |
| 190 |
|
| 191 |
errorState=true; |
| 192 |
value=s; |
| 193 |
} |
| 194 |
return value; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
private static boolean parseBoolean(String s) throws Exception{ |
| 199 |
if(s==null){throw INVALID_JSON;} |
| 200 |
if(s.equalsIgnoreCase("true") || s.equalsIgnoreCase("t")){return true;} |
| 201 |
if(s.equalsIgnoreCase("false") || s.equalsIgnoreCase("f")){return false;} |
| 202 |
throw INVALID_JSON; |
| 203 |
} |
| 204 |
|
| 205 |
|
| 206 |
private JsonObject makeObject(){ |
| 207 |
assert(text[pos]=='{'); |
| 208 |
pos++; |
| 209 |
|
| 210 |
if(verbose){outstream.println("Entering makeObject.");} |
| 211 |
|
| 212 |
JsonObject current=new JsonObject(); |
| 213 |
ByteBuilder bb=new ByteBuilder(); |
| 214 |
boolean quoteMode=false; |
| 215 |
boolean slashMode=false; |
| 216 |
String key=null; |
| 217 |
|
| 218 |
for(; pos<text.length; pos++){ |
| 219 |
final byte b=text[pos]; |
| 220 |
|
| 221 |
|
| 222 |
if(quoteMode){ |
| 223 |
if(slashMode){ |
| 224 |
if(verbose){outstream.println(">SlashModeEnd, buffer="+bb);} |
| 225 |
bb.append(b); |
| 226 |
slashMode=false; |
| 227 |
}else if(b=='"'){ |
| 228 |
if(verbose){outstream.println(">Quote; quote mode="+quoteMode+", key="+key+", buffer="+bb);} |
| 229 |
String s=bb.toString(); |
| 230 |
bb.clear(); |
| 231 |
if(key==null){ |
| 232 |
key=s; |
| 233 |
if(verbose){outstream.println("Set key to \""+key+"\"");} |
| 234 |
}else{ |
| 235 |
current.add(key, s); |
| 236 |
if(verbose){outstream.println("Added \""+key+"\": \""+s+"\"");} |
| 237 |
key=null; |
| 238 |
} |
| 239 |
quoteMode=!quoteMode; |
| 240 |
}else{ |
| 241 |
if(verbose){outstream.println(">QuoteMode, buffer="+bb);} |
| 242 |
if(b=='\\'){ |
| 243 |
if(verbose){outstream.println(">SlashMode, buffer="+bb);} |
| 244 |
slashMode=true; |
| 245 |
} |
| 246 |
bb.append(b); |
| 247 |
} |
| 248 |
}else if(b=='"'){ |
| 249 |
if(verbose){outstream.println(">Quote; quote mode="+quoteMode+", key="+key+", buffer="+bb);} |
| 250 |
quoteMode=!quoteMode; |
| 251 |
}else if(b==','){ |
| 252 |
if(verbose){outstream.println(">Comma; key="+key+", buffer=\""+bb+"\"");} |
| 253 |
if(key!=null){ |
| 254 |
final Object value=bufferToObject(bb); |
| 255 |
current.add(key, value); |
| 256 |
key=null; |
| 257 |
if(verbose){outstream.println("Added "+value+"; current=\n"+current+"\n");} |
| 258 |
} |
| 259 |
}else if(b==':'){ |
| 260 |
if(verbose){outstream.println(">Colon");} |
| 261 |
assert(key!=null); |
| 262 |
}else if(b=='{'){ |
| 263 |
if(verbose){outstream.println(">{, key="+key+", A) current object is:\n"+current+"\n");} |
| 264 |
JsonObject j=makeObject(); |
| 265 |
if(key==null){ |
| 266 |
if(verbose){outstream.println("Returning.");} |
| 267 |
return j; |
| 268 |
}else{ |
| 269 |
current.add(key, j); |
| 270 |
if(verbose){outstream.println("Added new object:\n"+j+"\n");} |
| 271 |
key=null; |
| 272 |
} |
| 273 |
}else if(b=='}'){ |
| 274 |
if(verbose){outstream.println(">}, key="+key+", B) current object is:\n"+current+"\n");} |
| 275 |
if(key!=null){ |
| 276 |
final Object value=bufferToObject(bb); |
| 277 |
current.add(key, value); |
| 278 |
key=null; |
| 279 |
if(verbose){outstream.println("Added "+value+"; current=\n"+current+"\n");} |
| 280 |
} |
| 281 |
pos++; |
| 282 |
return current; |
| 283 |
}else if(b=='['){ |
| 284 |
if(verbose){outstream.println(">[, C) current object is:\n"+current+"\n");} |
| 285 |
Object[] array=makeArray(); |
| 286 |
assert(key!=null) : "Should be in makeArray."; |
| 287 |
current.add(key, array); |
| 288 |
key=null; |
| 289 |
assert(bb.length()==0); |
| 290 |
}else if(b==']'){ |
| 291 |
if(verbose){outstream.println(">], D) current object is:\n"+current+"\n");} |
| 292 |
assert(false); |
| 293 |
}else if(b==' ' || b=='\t' || b=='\n' || b=='\r'){ |
| 294 |
if(verbose){outstream.println(">Other");} |
| 295 |
|
| 296 |
}else{ |
| 297 |
if(verbose){outstream.println(">NormalMode, buffer="+bb);} |
| 298 |
bb.append(b); |
| 299 |
} |
| 300 |
} |
| 301 |
return current; |
| 302 |
} |
| 303 |
|
| 304 |
|
| 305 |
private Object[] makeArray(){ |
| 306 |
assert(text[pos]=='['); |
| 307 |
pos++; |
| 308 |
|
| 309 |
if(verbose){outstream.println("Entering makeArray.");} |
| 310 |
|
| 311 |
ArrayList<Object> current=new ArrayList<Object>(); |
| 312 |
ByteBuilder bb=new ByteBuilder(); |
| 313 |
boolean quoteMode=false; |
| 314 |
boolean slashMode=false; |
| 315 |
|
| 316 |
for(; pos<text.length; pos++){ |
| 317 |
final byte b=text[pos]; |
| 318 |
|
| 319 |
if(quoteMode){ |
| 320 |
if(slashMode){ |
| 321 |
if(verbose){outstream.println(">SlashModeEnd, buffer="+bb);} |
| 322 |
bb.append(b); |
| 323 |
slashMode=false; |
| 324 |
}else if(b=='"'){ |
| 325 |
if(verbose){outstream.println(">Quote; quote mode="+quoteMode+", buffer="+bb);} |
| 326 |
String s=bb.toString(); |
| 327 |
bb.clear(); |
| 328 |
current.add(s); |
| 329 |
quoteMode=!quoteMode; |
| 330 |
}else{ |
| 331 |
if(verbose){outstream.println(">QuoteMode, buffer="+bb);} |
| 332 |
if(b=='\\'){ |
| 333 |
if(verbose){outstream.println(">SlashMode, buffer="+bb);} |
| 334 |
slashMode=true; |
| 335 |
} |
| 336 |
bb.append(b); |
| 337 |
} |
| 338 |
}else if(b=='"'){ |
| 339 |
if(verbose){outstream.println(">Quote; quote mode="+quoteMode+", buffer="+bb);} |
| 340 |
quoteMode=!quoteMode; |
| 341 |
}else if(b==','){ |
| 342 |
if(verbose){outstream.println(">Comma; buffer=\""+bb+"\"");} |
| 343 |
if(bb.length()>0){ |
| 344 |
final Object value=bufferToObject(bb); |
| 345 |
current.add(value); |
| 346 |
if(verbose){outstream.println("Added "+value+"; current=\n"+current+"\n");} |
| 347 |
} |
| 348 |
}else if(b==':'){ |
| 349 |
if(verbose){outstream.println(">Colon");} |
| 350 |
assert(false); |
| 351 |
}else if(b=='{'){ |
| 352 |
if(verbose){outstream.println(">{, E) current object is:\n"+current+"\n");} |
| 353 |
JsonObject j=makeObject(); |
| 354 |
current.add(j); |
| 355 |
if(verbose){outstream.println("Added new object:\n"+j+"\n");} |
| 356 |
}else if(b=='}'){ |
| 357 |
if(verbose){outstream.println(">}, current array is:\n"+current+"\n");} |
| 358 |
assert(false); |
| 359 |
}else if(b=='['){ |
| 360 |
if(verbose){outstream.println(">[, F) current object is:\n"+current+"\n");} |
| 361 |
Object[] array=makeArray(); |
| 362 |
current.add(array); |
| 363 |
}else if(b==']'){ |
| 364 |
if(verbose){outstream.println(">], G) current object is:\n"+current+"\n");} |
| 365 |
if(bb.length()>0){ |
| 366 |
final Object value=bufferToObject(bb); |
| 367 |
current.add(value); |
| 368 |
if(verbose){outstream.println("Added "+value+"; current=\n"+current+"\n");} |
| 369 |
} |
| 370 |
if(verbose){outstream.println("Returning "+current+"; text="+new String(text, 0, pos));} |
| 371 |
return current.toArray(); |
| 372 |
}else if(b==' ' || b=='\t' || b=='\n' || b=='\r'){ |
| 373 |
if(verbose){outstream.println(">Other");} |
| 374 |
|
| 375 |
}else{ |
| 376 |
if(verbose){outstream.println(">NormalMode, buffer="+bb);} |
| 377 |
bb.append(b); |
| 378 |
} |
| 379 |
} |
| 380 |
return current.toArray(); |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
byte[] text; |
| 388 |
int pos=0; |
| 389 |
boolean errorState; |
| 390 |
|
| 391 |
|
| 392 |
private static final boolean verbose=false; |
| 393 |
private static final PrintStream outstream=System.err; |
| 394 |
private static final Exception INVALID_JSON=new Exception("Invalid Json"); |
| 395 |
|
| 396 |
} |