如何反序列化并将所有数字转换为 Long ?

发布于 2024-09-07 15:27:39 字数 82 浏览 9 评论 0原文

Jackson 反序列化并将所有数字(如果值在整数范围内)转换为整数,而不是转换为长整型。我想将所有值转换为 Long。 是否存在简单的问题解决方案?

The Jackson deserialize and cast to Integer all numbers if value in range of Integers instead it cast to Long. I would like to cast ALL values to Long.
Is it exist easy solution of issue?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑着哭最痛 2024-09-14 15:27:39

Jackson 反序列化为您指定的类型,因此如果您将属性声明为 long 或 Long 类型,它会将其构造为 long。但也许您正在绑定到像 Map 这样的“无类型”结构?如果所有值都是 Long 类型,您可以适当地声明类型,例如:

Map<String,Long> map = objectMapper.readValue(json, new TypeReference<Map<String,Long>>() { });

或者可以为 Object.class 添加自定义反序列化器,并进行不同的处理(默认反序列化器是org.codehaus.jackson.map.deser.UntypedObjectDeserializer)。

如果我知道你实际上想要做什么可能会有所帮助 - IntegerLong 都是数字,所以通常区别并不重要......所以什么是需要多头的原因是什么?

Jackson deserializes to type you tell it to, so if you declare property to be of type long or Long it would construct it as long. But maybe you are binding to "untyped" structure like Map? If all values are of type Long, you could just declare type appropriately, like:

Map<String,Long> map = objectMapper.readValue(json, new TypeReference<Map<String,Long>>() { });

Alternatively might be able to add custom Deserializer for Object.class with different handling (default deserializer is org.codehaus.jackson.map.deser.UntypedObjectDeserializer).

It might help if I knew what you are actually trying to do -- Integer and Long are both numbers, so often distinction does not matter a lot... so what is the reason to require Longs?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文