Skip to content
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

Closed
lirui-andy opened this issue Sep 15, 2022 · 4 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@lirui-andy
Copy link

问题描述

序列化时如果指定了Filters,IgnoreGetterError配置不起作用

环境信息

请填写以下信息:

  • OS信息: [e.g.:任何版本]
  • JDK信息: [e.g.:任何版本]
  • 版本信息:[e.g.:Fastjson2 2.0.6]

重现步骤

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,
};
测试代码:

       DataVO3 d = new DataVO3();
		d.setV(localDateTime);
    	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;
		}
	}

期待的正确结果

对您期望发生的结果进行清晰简洁的描述。

相关日志输出

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)

@lirui-andy lirui-andy added the bug Something isn't working label Sep 15, 2022
@wsxe9988
Copy link
Contributor

麻烦提供下最小可供复现问题的代码和脱敏后的测试数据

@lirui-andy
Copy link
Author

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;
		}
	}
}

麻烦提供下最小可供复现问题的代码和脱敏后的测试数据

@wenshao wenshao added this to the 2.0.14 milestone Sep 15, 2022
@wenshao
Copy link
Member

wenshao commented Sep 15, 2022

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.14-SNAPSHOT/
问题已修复,请用2.0.14-SNAPSHOT版本帮忙验证,2.0.14版本预计在9月17日前发布

@wenshao
Copy link
Member

wenshao commented Sep 16, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.14
问题已修复,请用新版本

@wenshao wenshao closed this as completed Sep 16, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

3 participants