Ejemplo n.º 1
0
  public static JSONArray parseArray(String text) {
    if (text == null) {
      return null;
    }

    DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance());

    JSONArray array;

    JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.NULL) {
      lexer.nextToken();
      array = null;
    } else if (lexer.token() == JSONToken.EOF) {
      array = null;
    } else {
      array = new JSONArray();
      parser.parseArray(array);

      parser.handleResovleTask(array);
    }

    parser.close();

    return array;
  }
  public void test_0() throws Exception {
    List<?> res = Arrays.asList(1, 2, 3);
    String[] tests = {
      "[1,2,3]", "[1,,2,3]", "[1,2,,,3]", "[1 2,,,3]", "[1 2 3]", "[1, 2, 3,,]", "[,,1, 2, 3,,]",
    };

    for (String t : tests) {
      DefaultJSONParser ext = new DefaultJSONParser(t);
      ext.config(Feature.AllowArbitraryCommas, true);
      List<Object> extRes = ext.parseArray(Object.class);
      Assert.assertEquals(res, extRes);

      DefaultJSONParser basic = new DefaultJSONParser(t);
      basic.config(Feature.AllowArbitraryCommas, true);
      List<Object> basicRes = new ArrayList<Object>();
      basic.parseArray(basicRes);
      Assert.assertEquals(res, basicRes);
    }
  }
 public void test_error_0() throws Exception {
   Exception error = null;
   try {
     String text = "[{\"old\":false,\"name\":\"校长\",\"age\":3,\"salary\":123456789.0123]";
     DefaultJSONParser parser = new DefaultJSONParser(text);
     parser.parseArray(User.class);
   } catch (Exception ex) {
     error = ex;
   }
   Assert.assertNotNull(error);
 }
Ejemplo n.º 4
0
  public static List<Object> parseArray(String text, Type[] types) {
    if (text == null) {
      return null;
    }

    List<Object> list;

    DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance());
    Object[] objectArray = parser.parseArray(types);
    if (objectArray == null) {
      list = null;
    } else {
      list = Arrays.asList(objectArray);
    }

    parser.handleResovleTask(list);

    parser.close();

    return list;
  }
Ejemplo n.º 5
0
  public static <T> List<T> parseArray(String text, Class<T> clazz) {
    if (text == null) {
      return null;
    }

    List<T> list;

    DefaultJSONParser parser = new DefaultJSONParser(text, ParserConfig.getGlobalInstance());
    JSONLexer lexer = parser.getLexer();
    if (lexer.token() == JSONToken.NULL) {
      lexer.nextToken();
      list = null;
    } else {
      list = new ArrayList<T>();
      parser.parseArray(clazz, list);

      parser.handleResovleTask(list);
    }

    parser.close();

    return list;
  }
Ejemplo n.º 6
0
  @SuppressWarnings({"unchecked", "rawtypes"})
  public final void parseArray(final Collection array, Object fieldName) {
    final JSONLexer lexer = getLexer();

    if (lexer.token() == JSONToken.SET || lexer.token() == JSONToken.TREE_SET) {
      lexer.nextToken();
    }

    if (lexer.token() != JSONToken.LBRACKET) {
      throw new JSONException(
          "syntax error, expect [, actual "
              + JSONToken.name(lexer.token())
              + ", pos "
              + lexer.pos());
    }

    lexer.nextToken(JSONToken.LITERAL_STRING);

    ParseContext context = this.getContext();
    this.setContext(array, fieldName);
    try {
      for (int i = 0; ; ++i) {
        if (isEnabled(Feature.AllowArbitraryCommas)) {
          while (lexer.token() == JSONToken.COMMA) {
            lexer.nextToken();
            continue;
          }
        }

        Object value;
        switch (lexer.token()) {
          case LITERAL_INT:
            value = lexer.integerValue();
            lexer.nextToken(JSONToken.COMMA);
            break;
          case LITERAL_FLOAT:
            if (lexer.isEnabled(Feature.UseBigDecimal)) {
              value = lexer.decimalValue(true);
            } else {
              value = lexer.decimalValue(false);
            }
            lexer.nextToken(JSONToken.COMMA);
            break;
          case LITERAL_STRING:
            String stringLiteral = lexer.stringVal();
            lexer.nextToken(JSONToken.COMMA);

            if (lexer.isEnabled(Feature.AllowISO8601DateFormat)) {
              JSONScanner iso8601Lexer = new JSONScanner(stringLiteral);
              if (iso8601Lexer.scanISO8601DateIfMatch()) {
                value = iso8601Lexer.getCalendar().getTime();
              } else {
                value = stringLiteral;
              }
              iso8601Lexer.close();
            } else {
              value = stringLiteral;
            }

            break;
          case TRUE:
            value = Boolean.TRUE;
            lexer.nextToken(JSONToken.COMMA);
            break;
          case FALSE:
            value = Boolean.FALSE;
            lexer.nextToken(JSONToken.COMMA);
            break;
          case LBRACE:
            JSONObject object = new JSONObject();
            value = parseObject(object, i);
            break;
          case LBRACKET:
            Collection items = new JSONArray();
            parseArray(items, i);
            value = items;
            break;
          case NULL:
            value = null;
            lexer.nextToken(JSONToken.LITERAL_STRING);
            break;
          case RBRACKET:
            lexer.nextToken(JSONToken.COMMA);
            return;
          case EOF:
            throw new JSONException("unclosed jsonArray");
          default:
            value = parse();
            break;
        }

        array.add(value);
        checkListResolve(array);

        if (lexer.token() == JSONToken.COMMA) {
          lexer.nextToken(JSONToken.LITERAL_STRING);
          continue;
        }
      }
    } finally {
      this.setContext(context);
    }
  }
Ejemplo n.º 7
0
 @SuppressWarnings("rawtypes")
 public final void parseArray(final Collection array) {
   parseArray(array, null);
 }
Ejemplo n.º 8
0
 @SuppressWarnings("rawtypes")
 public void parseArray(Type type, Collection array) {
   parseArray(type, array, null);
 }
Ejemplo n.º 9
0
 public void parseArray(Class<?> clazz, @SuppressWarnings("rawtypes") Collection array) {
   parseArray((Type) clazz, array);
 }
Ejemplo n.º 10
0
 public <T> List<T> parseArray(Class<T> clazz) {
   List<T> array = new ArrayList<T>();
   parseArray(clazz, array);
   return array;
 }
Ejemplo n.º 11
0
  public Object parse(Object fieldName) {
    final JSONLexer lexer = getLexer();
    switch (lexer.token()) {
      case SET:
        lexer.nextToken();
        HashSet<Object> set = new HashSet<Object>();
        parseArray(set, fieldName);
        return set;
      case TREE_SET:
        lexer.nextToken();
        TreeSet<Object> treeSet = new TreeSet<Object>();
        parseArray(treeSet, fieldName);
        return treeSet;
      case LBRACKET:
        JSONArray array = new JSONArray();
        parseArray(array, fieldName);
        return array;
      case LBRACE:
        JSONObject object = new JSONObject();
        return parseObject(object, fieldName);
      case LITERAL_INT:
        Number intValue = lexer.integerValue();
        lexer.nextToken();
        return intValue;
      case LITERAL_FLOAT:
        Object value = lexer.decimalValue(isEnabled(Feature.UseBigDecimal));
        lexer.nextToken();
        return value;
      case LITERAL_STRING:
        String stringLiteral = lexer.stringVal();
        lexer.nextToken(JSONToken.COMMA);

        if (lexer.isEnabled(Feature.AllowISO8601DateFormat)) {
          JSONScanner iso8601Lexer = new JSONScanner(stringLiteral);
          try {
            if (iso8601Lexer.scanISO8601DateIfMatch()) {
              return iso8601Lexer.getCalendar().getTime();
            }
          } finally {
            iso8601Lexer.close();
          }
        }

        return stringLiteral;
      case NULL:
        lexer.nextToken();
        return null;
      case TRUE:
        lexer.nextToken();
        return Boolean.TRUE;
      case FALSE:
        lexer.nextToken();
        return Boolean.FALSE;
      case NEW:
        lexer.nextToken(JSONToken.IDENTIFIER);

        if (lexer.token() != JSONToken.IDENTIFIER) {
          throw new JSONException("syntax error");
        }
        lexer.nextToken(JSONToken.LPAREN);

        accept(JSONToken.LPAREN);
        long time = ((Number) lexer.integerValue()).longValue();
        accept(JSONToken.LITERAL_INT);

        accept(JSONToken.RPAREN);

        return new Date(time);
      case EOF:
        if (lexer.isBlankInput()) {
          return null;
        }
        throw new JSONException("unterminated json string, pos " + lexer.getBufferPosition());
      case ERROR:
      default:
        throw new JSONException("syntax error, pos " + lexer.getBufferPosition());
    }
  }
Ejemplo n.º 12
0
 public <T> Collection<T> decodeArray(String text, Class<T> clazz) throws Exception {
   ParserConfig config = new ParserConfig();
   DefaultJSONParser parser = new DefaultJSONParser(text, config);
   parser.config(Feature.DisableCircularReferenceDetect, true);
   return parser.parseArray(clazz);
 }