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

[FEATURE]实现了ObjectReader如何获得Type #594

Closed
leonchen83 opened this issue Jul 29, 2022 · 4 comments
Closed

[FEATURE]实现了ObjectReader如何获得Type #594

leonchen83 opened this issue Jul 29, 2022 · 4 comments
Labels
enhancement New feature or request
Milestone

Comments

@leonchen83
Copy link

请描述您的需求或者改进建议

对您想要需求或建议的清晰简洁的描述。

在Fastjson1中
实现ObjectDeserializer可以获得反序列化的Type如下

public final <T> T deserialze(final DefaultJSONParser parser, Type type, Object name) {
}

在Fastjson2中
实现ObjectReader的readObject方法时,发现这个type是null, 如下

@Override
public T readObject(JSONReader parser, Type type, Object name, long features) {

}

那么在fastjson2中,如何像fastjson1中那样获得反序列化的Type呢

请描述你建议的实现方案

对您想要需求或建议的实现方案的详细描述。

描述您考虑过的替代方案

对您考虑过的任何替代解决方案或功能的描述。

附加信息

如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。

@leonchen83 leonchen83 added the enhancement New feature or request label Jul 29, 2022
@wenshao
Copy link
Member

wenshao commented Jul 29, 2022

能提供重现问题的testcase么?

@leonchen83
Copy link
Author

leonchen83 commented Jul 29, 2022

@wenshao

public class Main {
	public static void main(String[] args) {
		JSON.register(Field.class, new FieldDeserializer());
		Test t = JSON.parseObject("{\"field\":\"this is a test\"}", Test.class);
		System.out.println(t);
	}
	
	public static class Test {
		Field field;
		
		public Field getField() {
			return field;
		}
		
		public void setField(Field field) {
			this.field = field;
		}
	}
	
	public static class Field {
		String field;
		
		public String getField() {
			return field;
		}
		
		public void setField(String field) {
			this.field = field;
		}
	}
	
	public static class FieldDeserializer implements ObjectReader<Field> {
		
		@Override
		public Field readObject(JSONReader jsonReader, Type fieldType, Object fieldName, long features) {
			System.out.println(fieldType);
			Field test = new Field();
			test.setField(jsonReader.readString());
			return test;
		}
	}
}

System.out.println(fieldType);

print null

@wenshao
Copy link
Member

wenshao commented Jul 30, 2022

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

@wenshao wenshao added this to the 2.0.11 milestone Jul 30, 2022
@wenshao
Copy link
Member

wenshao commented Aug 7, 2022

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

@wenshao wenshao closed this as completed Aug 7, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants