コード例 #1
0
  public void test_1() throws Exception {
    JSONObject res = new JSONObject();
    res.put("a", 1);
    res.put("b", 2);
    res.put("c", 3);

    String[] tests = {
      "{ 'a':1, 'b':2, 'c':3 }",
      "{ 'a':1,,'b':2, 'c':3 }",
      "{,'a':1, 'b':2, 'c':3 }",
      "{'a':1, 'b':2, 'c':3,,}",
      "{,,'a':1,,,,'b':2,'c':3,,,,,}",
    };

    for (String t : tests) {
      DefaultJSONParser ext = new DefaultJSONParser(t);
      ext.config(Feature.AllowArbitraryCommas, true);

      JSONObject extRes = ext.parseObject();
      Assert.assertEquals(res, extRes);

      DefaultJSONParser basic = new DefaultJSONParser(t);
      basic.config(Feature.AllowArbitraryCommas, true);
      JSONObject basicRes = basic.parseObject();
      Assert.assertEquals(res, basicRes);
    }
  }
コード例 #2
0
ファイル: JSON.java プロジェクト: xunmeng2015/bibitaxi
  @SuppressWarnings("unchecked")
  public static <T> T parseObject(
      String input,
      Type clazz,
      ParserConfig config,
      ParseProcess processor,
      int featureValues,
      Feature... features) {
    if (input == null) {
      return null;
    }

    for (Feature feature : features) {
      featureValues = Feature.config(featureValues, feature, true);
    }

    DefaultJSONParser parser = new DefaultJSONParser(input, config, featureValues);

    if (processor instanceof ExtraTypeProvider) {
      parser.getExtraTypeProviders().add((ExtraTypeProvider) processor);
    }

    if (processor instanceof ExtraProcessor) {
      parser.getExtraProcessors().add((ExtraProcessor) processor);
    }

    T value = (T) parser.parseObject(clazz);

    parser.handleResovleTask(value);

    parser.close();

    return (T) value;
  }
コード例 #3
0
  public void test_0() throws Exception {
    DefaultJSONParser parser =
        new DefaultJSONParser("{\"id\":3, \"name\":\"xx\"}", ParserConfig.getGlobalInstance());

    Entity entity = new Entity();
    parser.parseObject(entity);
  }
コード例 #4
0
 public void test_error_4() throws Exception {
   JSONException error = null;
   try {
     DefaultJSONParser parser = new DefaultJSONParser("{\"a\"3}");
     parser.parseObject(A.class);
   } catch (JSONException e) {
     error = e;
   }
   Assert.assertNotNull(error);
 }
コード例 #5
0
 public void test_error_6() throws Exception {
   JSONException error = null;
   try {
     DefaultJSONParser parser = new DefaultJSONParser("{'a':3}");
     parser.config(Feature.AllowSingleQuotes, false);
     parser.parseObject(A.class);
   } catch (JSONException e) {
     error = e;
   }
   Assert.assertNotNull(error);
 }
コード例 #6
0
 public void test_error_3() throws Exception {
   JSONException error = null;
   try {
     DefaultJSONParser parser = new DefaultJSONParser("{");
     parser.config(Feature.AllowUnQuotedFieldNames, true);
     parser.parseObject(A.class);
   } catch (JSONException e) {
     error = e;
   }
   Assert.assertNotNull(error);
 }
コード例 #7
0
  public void test_getInput() {
    String text = "{,,}";
    char[] chars = text.toCharArray();
    DefaultJSONParser parser =
        new DefaultJSONParser(chars, chars.length, ParserConfig.getGlobalInstance(), 0);

    JSONException error = null;
    try {
      parser.parseObject();
    } catch (JSONException ex) {
      error = ex;
    }
    Assert.assertNotNull(error);
  }
コード例 #8
0
ファイル: MapDeserializer.java プロジェクト: Goballistic/mt
  @SuppressWarnings({"rawtypes", "unchecked"})
  protected Object deserialze(DefaultJSONParser parser, Type type, Object fieldName, Map map) {
    if (type instanceof ParameterizedType) {
      ParameterizedType parameterizedType = (ParameterizedType) type;
      Type keyType = parameterizedType.getActualTypeArguments()[0];
      Type valueType = parameterizedType.getActualTypeArguments()[1];

      if (String.class == keyType) {
        return parseMap(parser, (Map<String, Object>) map, valueType, fieldName);
      } else {
        return parseMap(parser, map, keyType, valueType, fieldName);
      }
    } else {
      return parser.parseObject(map, fieldName);
    }
  }
コード例 #9
0
ファイル: JSON.java プロジェクト: xunmeng2015/bibitaxi
  @SuppressWarnings("unchecked")
  public static <T> T parseObject(char[] input, int length, Type clazz, Feature... features) {
    if (input == null || input.length == 0) {
      return null;
    }

    int featureValues = DEFAULT_PARSER_FEATURE;
    for (Feature feature : features) {
      featureValues = Feature.config(featureValues, feature, true);
    }

    DefaultJSONParser parser =
        new DefaultJSONParser(input, length, ParserConfig.getGlobalInstance(), featureValues);
    T value = (T) parser.parseObject(clazz);

    parser.handleResovleTask(value);

    parser.close();

    return (T) value;
  }
コード例 #10
0
ファイル: JSON.java プロジェクト: xunmeng2015/bibitaxi
  @SuppressWarnings("unchecked")
  public static <T> T parseObject(
      String input, Type clazz, int featureValues, Feature... features) {
    if (input == null) {
      return null;
    }

    for (Feature feature : features) {
      featureValues = Feature.config(featureValues, feature, true);
    }

    DefaultJSONParser parser =
        new DefaultJSONParser(input, ParserConfig.getGlobalInstance(), featureValues);
    T value = (T) parser.parseObject(clazz);

    parser.handleResovleTask(value);

    parser.close();

    return (T) value;
  }
コード例 #11
0
  public void test_2() throws Exception {
    A res = new A();
    res.setA(1);
    res.setB(2);
    res.setC(3);

    String[] tests = {
      "{ 'a':1, 'b':2, 'c':3 }",
      "{ 'a':1,,'b':2, 'c':3 }",
      "{,'a':1, 'b':2, 'c':3 }",
      "{'a':1, 'b':2, 'c':3,,}",
      "{,,'a':1,,,,'b':2,,'c':3,,,,,}",
    };

    for (String t : tests) {
      DefaultJSONParser ext = new DefaultJSONParser(t);
      ext.config(Feature.AllowArbitraryCommas, true);

      A extRes = ext.parseObject(A.class);
      Assert.assertEquals(res, extRes);
    }
  }
コード例 #12
0
 public <T> T decodeObject(String text, Class<T> clazz) {
   ParserConfig config = new ParserConfig();
   DefaultJSONParser parser = new DefaultJSONParser(text, config);
   parser.config(Feature.DisableCircularReferenceDetect, true);
   return parser.parseObject(clazz);
 }
コード例 #13
0
ファイル: MapDeserializer.java プロジェクト: Goballistic/mt
  @SuppressWarnings("rawtypes")
  public static Map parseMap(
      DefaultJSONParser parser, Map<String, Object> map, Type valueType, Object fieldName) {
    JSONLexer lexer = parser.getLexer();

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

    ParseContext context = parser.getContext();
    try {
      for (; ; ) {
        lexer.skipWhitespace();
        char ch = lexer.getCurrent();
        if (parser.isEnabled(Feature.AllowArbitraryCommas)) {
          while (ch == ',') {
            lexer.next();
            lexer.skipWhitespace();
            ch = lexer.getCurrent();
          }
        }

        String key;
        if (ch == '"') {
          key = lexer.scanSymbol(parser.getSymbolTable(), '"');
          lexer.skipWhitespace();
          ch = lexer.getCurrent();
          if (ch != ':') {
            throw new JSONException("expect ':' at " + lexer.pos());
          }
        } else if (ch == '}') {
          lexer.next();
          lexer.resetStringPosition();
          lexer.nextToken(JSONToken.COMMA);
          return map;
        } else if (ch == '\'') {
          if (!parser.isEnabled(Feature.AllowSingleQuotes)) {
            throw new JSONException("syntax error");
          }

          key = lexer.scanSymbol(parser.getSymbolTable(), '\'');
          lexer.skipWhitespace();
          ch = lexer.getCurrent();
          if (ch != ':') {
            throw new JSONException("expect ':' at " + lexer.pos());
          }
        } else {
          if (!parser.isEnabled(Feature.AllowUnQuotedFieldNames)) {
            throw new JSONException("syntax error");
          }

          key = lexer.scanSymbolUnQuoted(parser.getSymbolTable());
          lexer.skipWhitespace();
          ch = lexer.getCurrent();
          if (ch != ':') {
            throw new JSONException("expect ':' at " + lexer.pos() + ", actual " + ch);
          }
        }

        lexer.next();
        lexer.skipWhitespace();
        ch = lexer.getCurrent();

        lexer.resetStringPosition();

        if (key == JSON.DEFAULT_TYPE_KEY) {
          String typeName = lexer.scanSymbol(parser.getSymbolTable(), '"');
          Class<?> clazz = TypeUtils.loadClass(typeName);

          if (clazz == map.getClass()) {
            lexer.nextToken(JSONToken.COMMA);
            if (lexer.token() == JSONToken.RBRACE) {
              lexer.nextToken(JSONToken.COMMA);
              return map;
            }
            continue;
          }

          ObjectDeserializer deserializer = parser.getConfig().getDeserializer(clazz);

          lexer.nextToken(JSONToken.COMMA);

          parser.setResolveStatus(DefaultJSONParser.TypeNameRedirect);

          if (context != null && !(fieldName instanceof Integer)) {
            parser.popContext();
          }

          return (Map) deserializer.deserialze(parser, clazz, fieldName);
        }

        Object value;
        lexer.nextToken();

        if (lexer.token() == JSONToken.NULL) {
          value = null;
          lexer.nextToken();
        } else {
          value = parser.parseObject(valueType);
        }

        map.put(key, value);
        parser.checkMapResolve(map, key);

        parser.setContext(context, value, key);

        final int tok = lexer.token();
        if (tok == JSONToken.EOF || tok == JSONToken.RBRACKET) {
          return map;
        }

        if (tok == JSONToken.RBRACE) {
          lexer.nextToken();
          return map;
        }
      }
    } finally {
      parser.setContext(context);
    }
  }
コード例 #14
0
 public void test_4() throws Exception {
   DefaultJSONParser parser = new DefaultJSONParser("{a:3}");
   parser.config(Feature.AllowUnQuotedFieldNames, true);
   LinkedHashMap a = parser.parseObject(LinkedHashMap.class);
   Assert.assertEquals(3, a.get("a"));
 }
コード例 #15
0
 public void test_1() throws Exception {
   DefaultJSONParser parser = new DefaultJSONParser("{a:3}");
   parser.config(Feature.AllowUnQuotedFieldNames, true);
   A a = parser.parseObject(A.class);
   Assert.assertEquals(3, a.getA());
 }
コード例 #16
0
 public void test_0() throws Exception {
   DefaultJSONParser parser = new DefaultJSONParser("{'a':3}");
   parser.config(Feature.AllowSingleQuotes, true);
   A a = parser.parseObject(A.class);
   Assert.assertEquals(3, a.getA());
 }
コード例 #17
0
ファイル: OptionalCodec.java プロジェクト: cnoldtree/fastjson
 @SuppressWarnings("unchecked")
 public <T> T deserialze(DefaultJSONParser parser, Type type, Object fieldName) {
   type = TypeUtils.unwrapOptional(type);
   Object value = parser.parseObject(type);
   return (T) Optional.of(value);
 }
コード例 #18
0
 public JSONObject parseObject() {
   JSONObject object = new JSONObject();
   parseObject(object);
   return object;
 }