Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to deserialize and cast to Long all numbers?

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?

like image 453
eugenn Avatar asked Jun 29 '10 12:06

eugenn


1 Answers

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?

like image 65
StaxMan Avatar answered Oct 23 '22 04:10

StaxMan