Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1.2.21反序列泛型时报java.lang.ClassCastException,在1.2.12中却不报错 #937

Open
dengyj3 opened this issue Dec 6, 2016 · 4 comments

Comments

@dengyj3
Copy link

dengyj3 commented Dec 6, 2016

1、自定义泛型类(Out类中带泛型类型T):

public class Out<T> {
	private T outPara;
	public void setOutPara(T t) {
		outPara = t;
	}
	public T getOutPara() {
		return outPara;
	}
	public Out() {
	}
	public Out(T t) {
		setOutPara(t);
	}
}

2、对象类:

public class Info {
	private String name;
	private int id;
	private String content;
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
}

3、测试代码:

public class JsonTest {
	public static void main(String[] args) {
		Info info = new Info();
		info.setId(0);
		info.setName("user");
		info.setContent("info");
		Out<Info> out = new Out<Info>(info);//Out中T定义换成Info
		String jString = JSON.toJSONString(out);
		System.out.println(jString);
		
		Out<Info> out1 = returnOut(jString, Info.class);//Out中T定义换成Info
		System.out.println(out1);
               Info info2 = out1.getOutPara();
		System.out.println(info2.getName());
	}
	public static <T> Out<T> returnOut(String jsonStr, Class<T> c2){
		Out<T> out = JSON.parseObject(jsonStr, new TypeReference<Out<T>>(c2){});
		return out;
	}
}

4、报错信息(1.2.21版本下执行结果):

{"outPara":{"content":"info","id":0,"name":"user"}}

Exception in thread "main" java.lang.ClassCastException: fastjson.test.Info cannot be cast to fastjson.test.Out
at fastjson.test.JsonTest.returnOut(JsonTest.java:21)
at fastjson.test.JsonTest.main(JsonTest.java:17)
5、1.2.12版本下执行结果:

{"outPara":{"content":"info","id":0,"name":"user"}}

fastjson.test.Out@433a87a0
user
请问1.2.21和1.2.12 在泛型类的反序列化上有什么差异?

@wuwen5
Copy link
Contributor

wuwen5 commented Dec 7, 2016

目前使用这种方式是ok的

Out<Info> out = JSON.parseObject(jString, new TypeReference<Out<Info>>(){});

@dengyj3
Copy link
Author

dengyj3 commented Dec 8, 2016

@wuwen5 ,这种方式是可以获取到,但是可能不符合我的场景,我的场景下,Info是通过反射获取到的,需要传个参数进去才能知道是Info类,就像1.2.12版本那样,这样显式的我无法通过反射传入Info类型
Out out = JSON.parseObject(jString, new TypeReference<Out>(){});

@wuwen5
Copy link
Contributor

wuwen5 commented Dec 8, 2016

@dengyj3 这个问题是1.2.18版本 修复内存占用问题引入的.(1.2.17可以使用)

建议不要 使用new TypeReference<Out<T>>(c2){}每次new的写法,可以自己先弄个map缓存下,不然fastjson内部的全局ParserConfig中的IdentityHashMap会撑爆得.(见 #849

这个问题我已提交了pull request,在等@wenshao审核.

@wuwen5
Copy link
Contributor

wuwen5 commented Dec 10, 2016

@dengyj3 这个问题已经修复,请使用1.2.22版本。

asa1997 pushed a commit to Be-Secure/fastjson that referenced this issue Feb 8, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants