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

gson导致long类型序列化后类型为double类型 #116

Open
zhuangjinjin opened this issue Jul 28, 2020 · 3 comments
Open

gson导致long类型序列化后类型为double类型 #116

zhuangjinjin opened this issue Jul 28, 2020 · 3 comments
Labels
FAQ FAQ

Comments

@zhuangjinjin
Copy link

我dubbo接口暴露的接口参数带有Long类型,通过jmeter-plugins-for-apache-dubbo发起测试,而dubbo provider端收到的参数long值和发送端的值不一致,追踪代码发现jmeter-plugins-for-apache-dubbo采用gson作为序列化工具,而gson在对int和long类型的值做序列化时,会统一把它们当成double处理,导致精度受损问题

@ningyu1
Copy link
Collaborator

ningyu1 commented Aug 4, 2020

@zhuangjinjin 是独立的long类型参数?还是参数对象中一个long类型的属性?

@zhuangjinjin
Copy link
Author

@ningyu1 参数对象中一个long类型的属性

@ningyu1
Copy link
Collaborator

ningyu1 commented Aug 20, 2020

@zhuangjinjin
这个问题可以先规避。
规避的方法,把参数类所在的jar(api-jar)包放入jmeter classpath,插件在反序列化时会优先去classpath下寻找,如果找到了直接用明确类型反序列化,这样Long类型是不会变成double的(科学计数)。如果在classpath没有找到会使用Map通用格式反序列化,只有使用通用Map格式时才会将数字类型转换成double。

反序列化为通用Map示例:

HashMap<String, Object> newTest = new HashMap<>();
newTest.put("first", 6906764140092371368L);
String jsonString = JsonUtils.toJson(newTest);
System.out.println(jsonString); // output ok: {"first":6906764140092371368}
Map<String, Object> mapFromJson = JsonUtils.formJson(jsonString, Map.class);
System.out.println(mapFromJson.get("first")); // output ok: {Double@1036} 6.9067641400923709E18

相同问题:google/gson#1084
后续想办法解决掉

@ningyu1 ningyu1 added the type/enhancement New feature or request label Aug 20, 2020
@ningyu1 ningyu1 changed the title gson导致long类型序列化后类型为double类型,导致精度受损 gson导致long类型序列化后类型为double类型,变成科学计数 Oct 15, 2020
@ningyu1 ningyu1 changed the title gson导致long类型序列化后类型为double类型,变成科学计数 gson导致long类型序列化后类型为double类型 Oct 16, 2020
@ningyu1 ningyu1 added FAQ FAQ and removed type/enhancement New feature or request labels Oct 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FAQ FAQ
Projects
None yet
Development

No branches or pull requests

2 participants