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

[BUG] JsonObject 转为 Java对象时Map里面的key没有转为正确的类型 #1269

Closed
talentyao opened this issue Mar 23, 2023 · 2 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@talentyao
Copy link

talentyao commented Mar 23, 2023

class JavaObj {
	HashMap<Integer, Integer> map = new HashMap<>();

	public JavaObj() {
	}

	public JavaObj(HashMap<Integer, Integer> map) {
		super();
		this.map = map;
	}

	public HashMap<Integer, Integer> getMap() {
		return map;
	}

	public void setMap(HashMap<Integer, Integer> map) {
		this.map = map;
	}

	public static void main(String[] args) {
		HashMap<Integer, Integer> map = new HashMap<>();
		map.put(1, 1);
		map.put(2, 2);

		JavaObj o = new JavaObj(map);
		byte[] bt = JSON.toJSONBytes(o, JSONWriter.Feature.WriteNonStringKeyAsString); // 兼容其他语言
		JSONObject jsonObj = JSON.parseObject(bt);
		System.out.println(jsonObj.toString());

		o = jsonObj.to(JavaObj.class, JSONReader.Feature.FieldBased);
		System.out.println(o.getMap().get(1)); // 这里泛型不匹配, String的key没有自动转为int 仍是String类型。
		System.out.println("end");
	}
}
@talentyao talentyao added the bug Something isn't working label Mar 23, 2023
@wenshao wenshao added this to the 2.0.27 milestone Mar 25, 2023
@wenshao
Copy link
Member

wenshao commented Mar 26, 2023

https://oss.sonatype.org/content/repositories/snapshots/com/alibaba/fastjson2/fastjson2/2.0.27-SNAPSHOT/
问题已修复,请帮忙用2.0.27-SNAPSHOT版本验证

@wenshao wenshao added the fixed label Mar 26, 2023
@wenshao
Copy link
Member

wenshao commented Apr 8, 2023

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed
Projects
None yet
Development

No branches or pull requests

2 participants