-
Notifications
You must be signed in to change notification settings - Fork 511
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[BUG]序列化时如果指定了Filters,IgnoreGetterError配置不起作用 #769
Comments
麻烦提供下最小可供复现问题的代码和脱敏后的测试数据 |
import java.time.LocalDateTime;
import org.junit.Test;
import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONWriter;
import com.alibaba.fastjson2.filter.Filter;
import com.alibaba.fastjson2.filter.NameFilter;
import com.alibaba.fastjson2.filter.ValueFilter;
public class MyTest {
@Test
public void testCase(){
NameFilter nameFilter = (o, s, o1) -> s;
ValueFilter localDateFormatter1 = (o, s, source) -> {
return source;
};
Filter[] defaultSerializeFilters = {nameFilter, localDateFormatter1};
JSONWriter.Feature[] serializeFeatures = {
/* 序列化enum使用name */
JSONWriter.Feature.WriteEnumsUsingName,
/* 序列化BigDecimal使用toPlainString,避免科学计数法*/
JSONWriter.Feature.WriteBigDecimalAsPlain,
/* 忽略getter方法异常 */
JSONWriter.Feature.IgnoreErrorGetter,
/*
* 基于字段序列化,如果不配置,会默认基于public的field和getter方法序列化。配置后,会基于非static的field(包括private)做反序列化。
* 注意:如果指定了filter,不配置此参数,IgnoreErrorGetter 会失效。
* */
//JSONWriter.Feature.FieldBased,
};
DataVO3 d = new DataVO3();
d.setV(null);
String json = JSON.toJSONString(d, "yyyy-MM-dd HH:mm:ss", defaultSerializeFilters, serializeFeatures);
}
static class DataVO3 {
LocalDateTime v;
public LocalDateTime getV() {
throw new RuntimeException("mock an exception");
}
public void setV(LocalDateTime v) {
this.v = v;
}
}
}
|
https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.14-SNAPSHOT/ |
wenshao
added a commit
that referenced
this issue
Sep 15, 2022
# for free
to join this conversation on GitHub.
Already have an account?
# to comment
问题描述
序列化时如果指定了Filters,IgnoreGetterError配置不起作用
环境信息
请填写以下信息:
重现步骤
JSON.toJSONString(obj, dateFormat, defaultSerializeFilters, serializeFeatures);
其中:defaultSerializeFilters指定了一个ValueFilter和一个NameFilter;
serializeFeatures如下:
JSONWriter.Feature[] serializeFeatures = {
/* 序列化enum使用name /
JSONWriter.Feature.WriteEnumsUsingName,
/ 序列化BigDecimal使用toPlainString,避免科学计数法*/
JSONWriter.Feature.WriteBigDecimalAsPlain,
/* 忽略getter方法异常 */
JSONWriter.Feature.IgnoreErrorGetter,
};
测试代码:
期待的正确结果
对您期望发生的结果进行清晰简洁的描述。
相关日志输出
com.alibaba.fastjson2.JSONException: invoke getter method error, v
at com.alibaba.fastjson2.writer.FieldWriterObjectMethod.getFieldValue(FieldWriterObjectMethod.java:37)
at com.alibaba.fastjson2.writer.ObjectWriterAdapter.writeWithFilter(ObjectWriterAdapter.java:364)
at com.alibaba.fastjson2.writer.ObjectWriterAdapter.write(ObjectWriterAdapter.java:233)
at com.alibaba.fastjson2.JSON.toJSONString(JSON.java:1762)
The text was updated successfully, but these errors were encountered: