本文介绍了如何反序列化并强制转换为Long所有数字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

杰克逊反序列化并强制转换为Integer所有数字,如果整数范围内的值而不是强制转换为Long。我想将所有值转换为Long。
是否存在简单的问题解决方案?

解决方案

杰克逊反序列化你输入的类型,所以如果你宣布如果属性为long或long,则会将其构造为long。但也许你绑定到无类型结构,如 Map ?如果所有值都是 Long 类型,您可以恰当地声明类型,例如:

 地图<字符串,龙> map = objectMapper.readValue(json,new TypeReference< Map< String,Long>>(){}); 

或者可以为 Object.class 具有不同的处理(默认反序列化器是 org.codehaus.jackson.map.deser.UntypedObjectDeserializer )。



Integer
Long 都是数字,因此,区别并不重要...所以需要Longs的原因是什么?


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?

解决方案

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?

这篇关于如何反序列化并强制转换为Long所有数字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 12:17