fastjson2
fastjson2 copied to clipboard
[BUG] bean转jsonstring 自动进行方法调用导致异常
问题描述
bean转jsonstring时,自动调用了类中的方法 由于类中的方法有可能出现异常,所以,对bean的序列化过程是否不应该调用类内的方法?
环境信息
- JDK信息: [e.g.:Openjdk 1.8.0_251]
- 版本信息:[e.g.:Fastjson2 2.0.12]
重现步骤
类
@Data
public class TermV3 {
/**
* 词语
*/
private String word;
/**
* 获取本词语在HanLP词库中的频次
*
* @return 频次,0代表这是个OOV
*/
public int getFrequency() {
return 1/0;
}
}
方法
public static void main(String[] args) {
TermV2 termV2 = new TermV2();
System.out.println(JSONObject.toJSONString(termV2));
}
相关日志输出
Exception in thread "main" java.lang.ArithmeticException: / by zero
at com.yx.nlp.nlp.hanlp.v2.TermV3.getFrequency(TermV3.java:26)
at com.alibaba.fastjson2.writer.ObjectWriter_1.write(Unknown Source)
at com.alibaba.fastjson2.JSON.toJSONString(JSON.java:1757)
at com.alibaba.fastjson2.JSONObject.toJSONString(JSONObject.java:1085)
at com.yx.nlp.service.NlpImpl.main(NlpImpl.java:56)
https://github.com/alibaba/fastjson2/blob/main/docs/features_cn.md 参考这个JSONWriter.Feature. IgnoreErrorGetter
https://github.com/alibaba/fastjson2/blob/main/docs/features_cn.md 参考这个JSONWriter.Feature. IgnoreErrorGetter
这个方法并不是一个getter,只是一个以get开头命名的方法 能不能在调用前进行识别呢?
https://github.com/alibaba/fastjson2/blob/main/docs/name_filter_cn.md 可以使用NameFilter
@wszpwsren 也可以用这个JSONWriter.Feature.IgnoreNonFieldGetter