/** * Parses the given input string as JSON. The input must contain a valid JSON value, optionally * padded with whitespace. * * @param string * the input string, must be valid JSON * @return a value that represents the parsed JSON * @throws ParseException * if the input is not valid JSON */ public static JsonValue parse(String string) { if (string == null) { throw new NullPointerException("string is null"); } DefaultHandler handler = new DefaultHandler(); new JsonParser(handler).parse(string); return handler.getValue(); }
private void readNumber() throws IOException { handler.startNumber(); startCapture(); readChar('-'); int firstDigit = current; if (!readDigit()) { throw expected("digit"); } if (firstDigit != '0') { while (readDigit()) { } } readFraction(); readExponent(); handler.endNumber(endCapture()); }
private void readArray() throws IOException { Object array = handler.startArray(); read(); if (++nestingLevel > MAX_NESTING_LEVEL) { throw error("Nesting too deep"); } skipWhiteSpace(); if (readChar(']')) { nestingLevel--; handler.endArray(array); return; } do { skipWhiteSpace(); handler.startArrayValue(array); readValue(); handler.endArrayValue(array); skipWhiteSpace(); } while (readChar(',')); if (!readChar(']')) { throw expected("',' or ']'"); } nestingLevel--; handler.endArray(array); }
private ParseException expected(String expected) { if (isEndOfText()) { return error("Unexpected end of input"); } return error("Expected " + expected); }
private void readValue() throws IOException { switch (current) { case 'n': readNull(); break; case 't': readTrue(); break; case 'f': readFalse(); break; case '"': readString(); break; case '[': readArray(); break; case '{': readObject(); break; case '-': case '8': case '9': readNumber(); break; default: throw expected("value");
private String readStringInternal() throws IOException { read(); startCapture(); while (current != '"') { if (current == '\\') { pauseCapture(); readEscape(); startCapture(); } else if (current < 0x20) { throw expected("valid string character"); } else { read(); } } String string = endCapture(); read(); return string; }
current = 0; captureStart = -1; read(); skipWhiteSpace(); readValue(); skipWhiteSpace(); if (!isEndOfText()) { throw error("Unexpected character");
private void skipWhiteSpace() throws IOException { while (isWhiteSpace()) { read(); } }
private String readName() throws IOException { if (current != '"') { throw expected("name"); } return readStringInternal(); }
/** * Reads the entire input from the given reader and parses it as JSON. The input must contain a * valid JSON value, optionally padded with whitespace. * <p> * Characters are read in chunks into a default-sized input buffer. Hence, wrapping a reader in an * additional <code>BufferedReader</code> likely won't improve reading performance. * </p> * * @param reader * the reader to read the input from * @throws IOException * if an I/O error occurs in the reader * @throws ParseException * if the input is not valid JSON */ public void parse(Reader reader) throws IOException { parse(reader, DEFAULT_BUFFER_SIZE); }
private ParseException error( String message ) { int absIndex = bufferOffset + index; int column = absIndex - lineOffset; int offset = isEndOfText() ? absIndex : absIndex - 1; return new ParseException( message, offset, line, column - 1 ); }
private JsonValue readValue() throws IOException { switch( current ) { case 'n': return readNull(); case 't': return readTrue(); case 'f': return readFalse(); case '"': return readString(); case '[': return readArray(); case '{': return readObject(); case '-': case '0': case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': return readNumber(); default: throw expected( "value" ); } }
private String readStringInternal() throws IOException { read(); startCapture(); while (current != '"') { if (current == '\\') { pauseCapture(); readEscape(); startCapture(); } else if (current < 0x20) { throw expected("valid string character"); } else { read(); } } String string = endCapture(); read(); return string; }
JsonValue parse() throws IOException { read(); skipWhiteSpace(); JsonValue result = readValue(); skipWhiteSpace(); if( !isEndOfText() ) { throw error( "Unexpected character" ); } return result; }