private boolean readIf(char ch) throws IOException { if (current!=ch) { return false; } read(); return true; }
private String readStringInternal(boolean allowML) throws IOException { // callees make sure that (current=='"' || current=='\'') int exitCh = current; read(); startCapture(); while (current!=exitCh) { if (current=='\\') readEscape(); else if (current<0x20) throw expected("valid string character"); else read(); } String string=endCapture(); read(); if (allowML && exitCh=='\'' && current=='\'' && string.length()==0) { // ''' indicates a multiline string read(); return readMlString(); } else return string; }
JsonValue parse() throws IOException { // Braces for the root object are optional read(); skipWhiteSpace(); if (legacyRoot) { switch (current) { case '[': case '{': return checkTrailing(readValue()); default: try { // assume we have a root object without braces return checkTrailing(readObject(true)); } catch (Exception exception) { // test if we are dealing with a single JSON value instead (true/false/null/num/"") reset(); read(); skipWhiteSpace(); try { return checkTrailing(readValue()); } catch (Exception exception2) { } throw exception; // throw original error } } } else { return checkTrailing(readValue()); } }
private JsonObject readObject(boolean objectWithoutBraces) throws IOException { if (!objectWithoutBraces) read(); JsonObject object=new JsonObject(); skipWhiteSpace(); while (true) { if (objectWithoutBraces) { if (isEndOfText()) break; } else { if (isEndOfText()) throw error("End of input while parsing an object (did you forget a closing '}'?)"); if (readIf('}')) break; } String name=readName(); skipWhiteSpace(); if (!readIf(':')) { throw expected("':'"); } skipWhiteSpace(); object.add(name, readValue()); skipWhiteSpace(); if (readIf(',')) skipWhiteSpace(); // , is optional } return object; }
private JsonArray readArray() throws IOException { read(); JsonArray array=new JsonArray(); skipWhiteSpace(); if (readIf(']')) { return array; } while (true) { skipWhiteSpace(); array.add(readValue()); skipWhiteSpace(); if (readIf(',')) skipWhiteSpace(); // , is optional if (readIf(']')) break; else if (isEndOfText()) throw error("End of input while parsing an array (did you forget a closing ']'?)"); } return array; }
private String readName() throws IOException { if (current=='"' || current=='\'') return readStringInternal(false); StringBuilder name=new StringBuilder(); int space=-1, start=index; while (true) { if (current==':') { if (name.length()==0) throw error("Found ':' but no key name (for an empty key name use quotes)"); else if (space>=0 && space!=name.length()) { index=start+space; throw error("Found whitespace in your key name (use quotes to include)"); } return name.toString(); } else if (isWhiteSpace(current)) { if (space<0) space=name.length(); } else if (current<' ') { throw error("Name is not closed"); } else if (JsonValue.isPunctuatorChar(current)) { throw error("Found '" + (char)current + "' where a key name was expected (check your syntax or use quotes if the key name includes {}[],: or whitespace)"); } else name.append((char)current); read(); } }
private JsonObject readObject(boolean objectWithoutBraces) throws IOException { if (!objectWithoutBraces) read(); JsonObject object=new JsonObject(); skipWhiteSpace(); while (true) { if (objectWithoutBraces) { if (isEndOfText()) break; } else { if (isEndOfText()) throw error("End of input while parsing an object (did you forget a closing '}'?)"); if (readIf('}')) break; } String name=readName(); skipWhiteSpace(); if (!readIf(':')) { throw expected("':'"); } skipWhiteSpace(); object.add(name, readValue()); skipWhiteSpace(); if (readIf(',')) skipWhiteSpace(); // , is optional } return object; }
private JsonArray readArray() throws IOException { read(); JsonArray array=new JsonArray(); skipWhiteSpace(); if (readIf(']')) { return array; } while (true) { skipWhiteSpace(); array.add(readValue()); skipWhiteSpace(); if (readIf(',')) skipWhiteSpace(); // , is optional if (readIf(']')) break; else if (isEndOfText()) throw error("End of input while parsing an array (did you forget a closing ']'?)"); } return array; }
private String readStringInternal(boolean allowML) throws IOException { // callees make sure that (current=='"' || current=='\'') int exitCh = current; read(); startCapture(); while (current!=exitCh) { if (current=='\\') readEscape(); else if (current<0x20) throw expected("valid string character"); else read(); } String string=endCapture(); read(); if (allowML && exitCh=='\'' && current=='\'' && string.length()==0) { // ''' indicates a multiline string read(); return readMlString(); } else return string; }
JsonValue parse() throws IOException { // Braces for the root object are optional read(); skipWhiteSpace(); if (legacyRoot) { switch (current) { case '[': case '{': return checkTrailing(readValue()); default: try { // assume we have a root object without braces return checkTrailing(readObject(true)); } catch (Exception exception) { // test if we are dealing with a single JSON value instead (true/false/null/num/"") reset(); read(); skipWhiteSpace(); try { return checkTrailing(readValue()); } catch (Exception exception2) { } throw exception; // throw original error } } } else { return checkTrailing(readValue()); } }
private boolean readIf(char ch) throws IOException { if (current!=ch) { return false; } read(); return true; }
private String readName() throws IOException { if (current=='"' || current=='\'') return readStringInternal(false); StringBuilder name=new StringBuilder(); int space=-1, start=index; while (true) { if (current==':') { if (name.length()==0) throw error("Found ':' but no key name (for an empty key name use quotes)"); else if (space>=0 && space!=name.length()) { index=start+space; throw error("Found whitespace in your key name (use quotes to include)"); } return name.toString(); } else if (isWhiteSpace(current)) { if (space<0) space=name.length(); } else if (current<' ') { throw error("Name is not closed"); } else if (JsonValue.isPunctuatorChar(current)) { throw error("Found '" + (char)current + "' where a key name was expected (check your syntax or use quotes if the key name includes {}[],: or whitespace)"); } else name.append((char)current); read(); } }
private void skipIndent(int indent) throws IOException { while (indent-->0) { if (isWhiteSpace(current) && current!='\n') read(); else break; } }
private void skipIndent(int indent) throws IOException { while (indent-->0) { if (isWhiteSpace(current) && current!='\n') read(); else break; } }
private void skipWhiteSpace() throws IOException { while (!isEndOfText()) { while (isWhiteSpace()) read(); if (current=='#' || current=='/' && peek()=='/') { do { read(); } while (current>=0 && current!='\n'); } else if (current=='/' && peek()=='*') { read(); do { read(); } while (current>=0 && !(current=='*' && peek()=='/')); read(); read(); } else break; } }
private void skipWhiteSpace() throws IOException { while (!isEndOfText()) { while (isWhiteSpace()) read(); if (current=='#' || current=='/' && peek()=='/') { do { read(); } while (current>=0 && current!='\n'); } else if (current=='/' && peek()=='*') { read(); do { read(); } while (current>=0 && !(current=='*' && peek()=='/')); read(); read(); } else break; } }
value.append((char)current); for (;;) { read(); boolean isEol=current<0 || current=='\r' || current=='\n'; if (isEol || current==',' ||
value.append((char)current); for (;;) { read(); boolean isEol=current<0 || current=='\r' || current=='\n'; if (isEol || current==',' ||