views:

30

answers:

1

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?

A: 

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 map = objectMapper.readValue(json, new TypeReference>() { });

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?

StaxMan
The deserialized numbers are IDs of objects that I would like to retrieved through the Hibernate. When I use Integer it throw an exception that it can convert from integer to long.
eugenn
Then the easiest way might be to explicitly just convert these, something like 'Long id = numberValue.longValue();'.Unless if map only has numeric values, in which you can declare types as Map<String,Long> (as I mentioned above)
StaxMan