So recently I had a use case where I wanted to use jackson to convert an object into json string/bytes, sent it across wire and then deserialize it back.
The object consisted of attributes which were abstract and hence default jackson deserialization was not working.
I was getting the following Exception while deserializing :
Can not construct instance of <packageName> , problem: abstract types can only be instantiated with additional type information
The issue was : in case of polymorphic objects during deserialization we need additional information in order to know which particular class type was actually present during serialization.
jackson provides a way to add this information by using the following line :
// one of:
objectMapper.enableDefaultTyping(); // default to using DefaultTyping.OBJECT_AND_NON_CONCRETE
objectMapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
So to solve the issue : just add any one of the above lines before serializing the object. During deserialization the jackson would be able to convert the byte string into the original object by using the additional information about the type of the object attributes.
Reference : http://wiki.fasterxml.com/JacksonPolymorphicDeserialization
Posted in 1
Tags: Jackson, Polymorphism
Recent Comments