private List<?> readArray(JsonLexer lexer) throws ParseException { expectCurrentToken(lexer, OPEN_BRACKET); expectMoveNext(lexer, NULL, BOOLEAN, NUMBER, STRING, OPEN_BRACKET, OPEN_BRACE, CLOSE_BRACKET); List<Object> array = new ArrayList<Object>(); while (isCurrentToken(lexer, VALUE_START_TOKENS)) { array.add(readValue(lexer)); if (isCurrentToken(lexer, CLOSE_BRACKET)) { break; } else { expectCurrentToken(lexer, COMMA); expectMoveNext(lexer, VALUE_START_TOKENS); } } expectCurrentToken(lexer, CLOSE_BRACKET); lexer.moveNext(); return array; }
private Map<String, Object> readObject(JsonLexer lexer) throws ParseException { expectCurrentToken(lexer, OPEN_BRACE); expectMoveNext(lexer, STRING, CLOSE_BRACE); Map<String, Object> object = new HashMap<String, Object>(); while (isCurrentToken(lexer, STRING)) { String string = readString(lexer); expectCurrentToken(lexer, COLON); expectMoveNext(lexer, VALUE_START_TOKENS); Object value = readValue(lexer); object.put(string, value); if (isCurrentToken(lexer, CLOSE_BRACE)) break; else { expectCurrentToken(lexer, COMMA); expectMoveNext(lexer, VALUE_START_TOKENS); } } expectCurrentToken(lexer, CLOSE_BRACE); lexer.moveNext(); return object; }
private String readString(JsonLexer lexer) throws ParseException { expectCurrentToken(lexer, STRING); String str = unescape((String) lexer.getValue()); lexer.moveNext(); return str.toString(); }
private Number readNumber(JsonLexer lexer) throws ParseException { expectCurrentToken(lexer, NUMBER); Number num = (Number) lexer.getValue(); lexer.moveNext(); return new BigDecimal(num.toString()); }
private Boolean readBoolean(JsonLexer lexer) throws ParseException { expectCurrentToken(lexer, BOOLEAN); Boolean bool = (Boolean) lexer.getValue(); lexer.moveNext(); return bool; }
private Object readNull(JsonLexer lexer) throws ParseException { expectCurrentToken(lexer, NULL); lexer.moveNext(); return null; }
/** * Attempts to move the lexer to the next token, and throws a ParseException with the expected * next token types if not successful */ private void expectMoveNext(JsonLexer lexer, Token... expected) throws ParseException { if (!lexer.moveNext()) { throw new ParseException("no valid tokens from " + lexer.getOffset(), expected); } }