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]大写单字母名称字段序列化与反序列化错误 #924

Closed
kelincxm opened this issue Nov 11, 2022 · 5 comments
Closed

[BUG]大写单字母名称字段序列化与反序列化错误 #924

kelincxm opened this issue Nov 11, 2022 · 5 comments
Labels
bug Something isn't working fixed
Milestone

Comments

@kelincxm
Copy link

问题描述

对包含大写单字母名称的字段进行序列化与反序列化时错误

环境信息

  • OS信息: windows10 19042.1706 Intel(R) Core(TM) i5-7200U CPU @ 2.50GHz 2.70 GHz 8GB
  • JDK信息: Openjdk 1.8.0_301
  • 版本信息:2.0.15

重现步骤

public class Main {
	public static void main(String[] args) {
                System.out.println(new Entity(100L));
		System.out.println(JSON.toJSONString(new Entity(100L)));
		System.out.println(JSON.parseObject(JSON.toJSONString(new Entity(100L)), Entity.class));
	}

	@AllArgsConstructor
	@NoArgsConstructor
	@Data
	public static class Entity {
		private Long C;
	}
}

期待正确结果

Main.Entity(C=100)
{"C":100}
Main.Entity(C=100)

实际输出结果

Main.Entity(C=100)
{"c":100}
Main.Entity(C=100)

附加信息

第二行代码应该被序列化为{"C":100}。但实际上被序列化为{"c":100}。而通过{"c":100}去反序列化Entity时,正确的结果应该是Main.Entity(C=null)。

@kelincxm kelincxm added the bug Something isn't working label Nov 11, 2022
@wenshao wenshao added this to the 2.0.19 milestone Nov 12, 2022
@wenshao wenshao added the fixed label Nov 12, 2022
@wenshao
Copy link
Member

wenshao commented Nov 12, 2022

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

@kelincxm
Copy link
Author

kelincxm commented Nov 12, 2022

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

经过测试,对象序列化为字符串是正确的。但通过字符串{"c": 100}反序列化对象时,对象的C字段仍然可以保存为100。

我认为反序列化后的C字段应该是null,因为C和c大小写不一致。

重现步骤

public class Main {
	public static void main(String[] args) {
		System.out.println(JSON.parseObject("{\"c\":100}", Entity.class));
	}

	public static class Entity {
		private Long C;

		public Entity(Long c) {
			C = c;
		}

		public Entity() {
		}

		public Long getC() {
			return C;
		}

		public void setC(Long c) {
			C = c;
		}

		@Override
		public String toString() {
			return "Entity{" +
				"C=" + C +
				'}';
		}
	}
}

期望结果

Entity{C=null}

实际结果

Entity{C=100}

@wenshao
Copy link
Member

wenshao commented Nov 12, 2022

反序列化这个场景需要自动识别"c"和"C",要不然会有升级兼容问题

@wenshao
Copy link
Member

wenshao commented Nov 12, 2022

https://github.com/alibaba/fastjson2/releases/tag/2.0.19
请用新版本

@wenshao wenshao closed this as completed Nov 12, 2022
@kelincxm
Copy link
Author

# 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

2 participants