Reputation: 109
I want to know if there is some way to get the following deserializing properly:
public class MyClass {
Map<String, Serializable> map;
OtherObject object;
[...]
}
ObjectMapper mapper = new ObjectMapper();
MyClass instance = mapper.readValue(someJson, MyClass);
Currently, I'm trying and I get an exception
com.fasterxml.jackson.databind.JsonMappingException: Can not construct instance of java.io.Serializable: abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
Upvotes: 3
Views: 578
Reputation: 26
As of v2.10.0, Jackson supports naive deserialization of 'Serializable' values, i.e., it will consider 'Serializable' the same as 'Object'. See this Github issue for more details.
So update Jackson to 2.10.0+ and try.
Upvotes: 1
Reputation: 2024
You can change your data class's Map property declaration to not reference Serializable:
public class MyClass {
Map<String, ?> map;
OtherObject object;
[...]
}
This will affect the signature of the Map's getter and setter as well. This does loosen type-checking, however, and the the map's values could be any subclass of Object instead of implementations of Serializable. If that's an important detail, you could do post-serialization validation of the map values.
Or you can configure a custom Deserializer, as those in your question's comments have suggested. It's just a matter of where you want to add the code, and that depends on how widespread this case is in your application.
Upvotes: 0