I am serializing and deserializing following domain object to JSON using Jackson 1.8.3
public class Node { private String key; private Object value; private List<Node> children = new ArrayList<Node>(); /* getters and setters omitted for brevity */ }
Object is then serialized and deserialized using following code
ObjectMapper mapper = new ObjectMapper(); mapper.writeValue(destination, rootNode);
And then later deserialized with
mapper.readValue(destination, Node.class);
The original values of the object are either Strings, Doubles, Longs or Booleans. However, during serialization and deserialization Jackson transforms Long values (such as 4) to Integers.
How can I "force" Jackson to deserialize numeric non-decimal values to Long instead of Integer?
Jackson uses default (no argument) constructor to create object and then sets value using setters. so you only need @NoArgsConstructor and @Setter.
To create a custom deserializer, we need to create a class extending StdDeserializer and then override its deserialize() method. We can use custom deserializer either by registering with ObjectMapper or annotating class with @JsonDeserialize . Now find the JSON used in our demo.
Note that Jackson does not use java.
There is a new feature in Jackson 2.6 specifically for this case:
configure the ObjectMapper to use DeserializationFeature.USE_LONG_FOR_INTS
see https://github.com/FasterXML/jackson-databind/issues/504
cowtowncoder pushed a commit that closed this issue on May 19, 2015 Fix #504 and #797
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With