[alibaba/fastjson]反序列化成类时报错,这绝对是一个bug,屡试不爽

2024-09-02 630 views
8

FastJsonConfig config = new FastJsonConfig(); config.setSerializerFeatures(new SerializerFeature[]{ SerializerFeature.WriteDateUseDateFormat, SerializerFeature.WriteBigDecimalAsPlain, SerializerFeature.QuoteFieldNames, SerializerFeature.PrettyFormat, SerializerFeature.DisableCircularReferenceDetect});

[ { "auditStatus":30, "id":"03ff2fea48184cdaaafbfc4d74432f5b", "name":"公司", "updateTime":"2021-06-03 14:30:58" } ] 反序列化成类时报错 @Data public class CompanySimpleVo { private String id; private String name; private Date updateTime; }

com.alibaba.fastjson.parser.JSONScanner#scanFieldDate

`

ch = charAt(++bp);//ch可能是空字符串换行符制表符等字符,如果是这样空字符串换行符制表符等字符,后面 matchStat = NOT_MATCH,导致异常 if (ch == ',') { token = JSONToken.COMMA; this.ch = charAt(++bp); } else if (ch == ']') { token = JSONToken.RBRACKET; this.ch = charAt(++bp); } else if (ch == '}') { token = JSONToken.RBRACE; this.ch = charAt(++bp); } else if (ch == EOI) { token = JSONToken.EOF; } else { this.bp = startPos; this.ch = startChar; matchStat = NOT_MATCH; return null; } matchStat = END;

`

回答

5

版本号1.2.83

1

`@Data public class CompanyJsonBugTest {

/**
 * 首字母
 */

private Date updateTime;

private String id;
public static final FastJsonConfig FAST_JSON_CONFIG = configFastJson();

public static FastJsonConfig configFastJson() {
    FastJsonConfig config = new FastJsonConfig();
    //config.getParserConfig().setAsmEnable(false);//默认是true 

    config.setSerializerFeatures(new SerializerFeature[]{
            SerializerFeature.WriteDateUseDateFormat,
            SerializerFeature.WriteBigDecimalAsPlain,
            SerializerFeature.QuoteFieldNames,
            SerializerFeature.PrettyFormat,
            SerializerFeature.DisableCircularReferenceDetect});
    return config;
}
public static void main(String[] args) throws Exception {
    CompanyJsonBugTest bugTest = new CompanyJsonBugTest();
  //  bugTest.setAuditStatus(20);
  //  bugTest.setId("008e2562703c49a09fa5ca7dd151127b");
  //  bugTest.setName("企业全称");
    bugTest.setUpdateTime(new Date());
    List<CompanyJsonBugTest> companyJsonBugTests = new ArrayList<>();
    companyJsonBugTests.add(bugTest);
    FastJsonConfig fastJsonConfig = FAST_JSON_CONFIG;
    List<SerializeFilter> allFilters = new ArrayList<SerializeFilter>(Arrays.asList( fastJsonConfig.getSerializeFilters()));
   try( ByteArrayOutputStream outnew = new ByteArrayOutputStream()) {

       int len = JSON.writeJSONStringWithFastJsonConfig(outnew, //
               fastJsonConfig.getCharset(), //
               companyJsonBugTests, //
               fastJsonConfig.getSerializeConfig(), //
               //fastJsonConfig.getSerializeFilters(), //
               allFilters.toArray(new SerializeFilter[allFilters.size()]),
               fastJsonConfig.getDateFormat(), //
               JSON.DEFAULT_GENERATE_FEATURE, //
               fastJsonConfig.getSerializerFeatures());
       String string = outnew.toString();
       System.out.println(string);
       byte[] bytes = outnew.toByteArray();
       ByteArrayInputStream in = new ByteArrayInputStream(bytes);
       Method getCompanyJsonBugs = CompanyJsonBugTest.class.getDeclaredMethod("getCompanyJsonBugs");
       Object o = JSON.parseObject(in,
               fastJsonConfig.getCharset(),
               (Type) getCompanyJsonBugs.getGenericReturnType(),
               fastJsonConfig.getParserConfig(),
               fastJsonConfig.getParseProcess(),
               JSON.DEFAULT_PARSER_FEATURE,
               fastJsonConfig.getFeatures());
       System.out.println(o);

   }catch (Exception e){
       System.out.println(e);
   }

}

private List<CompanyJsonBugTest> getCompanyJsonBugs(){
    return null;
}

}` 经过反复试验,当去掉字段其他字段只保留updateTime,可以成功,添加其他字段就报错, com.alibaba.fastjson.parser.JSONScanner#scanFieldDate中调用scanISO8601DateIfMatch token = JSONToken.LITERAL_ISO8601_DATE; return true; 返回true 并将token 设置成JSONToken.LITERAL_ISO8601_DATE导致后续在com.alibaba.fastjson.parser.deserializer.JavaBeanDeserializer#deserialze(com.alibaba.fastjson.parser.DefaultJSONParser, java.lang.reflect.Type, java.lang.Object, java.lang.Object, int, int[])第439行成为true,下面直接抛出异常 控制台打印结果 [ { "updateTime":"2022-09-29 13:51:36" } ] com.alibaba.fastjson.JSONException: syntax error, expect {, actual iso8601, pos 0, fastjson-version 1.2.83

3

在以上代码基础上增加name字段 private String name; 运行正常 [ { "name":"企业全称", "updateTime":"2022-09-29 13:46:35" } ] [CompanyJsonBugTest(updateTime=Thu Sep 29 13:46:35 CST 2022, id=null, name=企业全称)]

7

@wenshao 请帮忙看看