Example #1
0
 private Object readValue(JsonLexer lexer) throws ParseException {
   if (isCurrentToken(lexer, NULL)) {
     return readNull(lexer);
   } else if (isCurrentToken(lexer, BOOLEAN)) {
     return readBoolean(lexer);
   } else if (isCurrentToken(lexer, NUMBER)) {
     return readNumber(lexer);
   } else if (isCurrentToken(lexer, STRING)) {
     return readString(lexer);
   } else if (isCurrentToken(lexer, OPEN_BRACKET)) {
     return readArray(lexer);
   } else if (isCurrentToken(lexer, OPEN_BRACE)) {
     return readObject(lexer);
   } else {
     throw new ParseException(lexer.getLexeme(), VALUE_START_TOKENS);
   }
 }
Example #2
0
  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;
  }
Example #3
0
  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;
  }
Example #4
0
 private String readString(JsonLexer lexer) throws ParseException {
   expectCurrentToken(lexer, STRING);
   String str = unescape((String) lexer.getValue());
   lexer.moveNext();
   return str.toString();
 }
Example #5
0
 private Number readNumber(JsonLexer lexer) throws ParseException {
   expectCurrentToken(lexer, NUMBER);
   Number num = (Number) lexer.getValue();
   lexer.moveNext();
   return new BigDecimal(num.toString());
 }
Example #6
0
 private Boolean readBoolean(JsonLexer lexer) throws ParseException {
   expectCurrentToken(lexer, BOOLEAN);
   Boolean bool = (Boolean) lexer.getValue();
   lexer.moveNext();
   return bool;
 }
Example #7
0
 private Object readNull(JsonLexer lexer) throws ParseException {
   expectCurrentToken(lexer, NULL);
   lexer.moveNext();
   return null;
 }
Example #8
0
 private boolean isCurrentToken(JsonLexer lexer, Token... toks) {
   for (Token tok : toks) {
     if (lexer.getToken().equals(tok)) return true;
   }
   return false;
 }
Example #9
0
 /**
  * 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);
   }
 }
Example #10
0
 /**
  * Checks that the current token is one of <tt>expected</tt>, and if not throws a ParseException
  */
 private void expectCurrentToken(JsonLexer lexer, Token... expected) throws ParseException {
   if (!isCurrentToken(lexer, expected)) {
     throw new ParseException(lexer.getLexeme(), expected);
   }
 }