object
{}
{ members }
members
pair
pair , members
pair
string : value
array
[]
[ elements ]
elements
value
value , elements
value
string
number
object
array
true
false
null
/** skip white Space */ publicvoidws(){ while (!isEnd()) { if (Character.isWhitespace(peek())) { idx++; } else { break; } } }
public String number(){ ws(); int initIdx = idx; String numberString = ""; if (peek() == '-'){ next('-'); numberString += '-'; } // digit before . while (!isEnd()) { char c = peek(); if (Character.isDigit(c)) { numberString += c; idx++; } else { break; } } if (isEnd()){ thrownew JSONParseException("Unexpected number at the end"); } // digit after . if (peek() == '.'){ next('.'); numberString += '.'; while (!isEnd()) { char c = peek(); if (Character.isDigit(c)) { numberString += c; idx++; } else { break; } } }
// integer or double try { System.out.println("Parse as Int for " + numberString); return Integer.parseInt(numberString) + ""; } catch (NumberFormatException ex){ try { System.out.println("Parse as double for " + numberString); return Double.parseDouble(numberString) + ""; } catch (NumberFormatException ex2){ thrownew JSONParseException("Expected number at " + initIdx + " but it is not found"); } } }
publicintnextInt(){ ws();
String intStr = ""; while (!isEnd()) { char c = peek(); if (Character.isDigit(c)) { intStr += c; idx++; } else { break; } }
return Integer.parseInt(intStr); }
/* next string without "" */ public String string(){ ws();
String str = null; boolean scanning = false;
char c = peek(); if (c == '"') { scanning = true; str = ""; next(); }