Cannot find deserialization for non-concrete Collection type

I am using jackson library to map JSON to objects. I greatly simplified the problem, here is what happens:

public class MyObject{ public ForeignCollection<MySecondObject> getA(){ return null; } public ForeignCollection<MyThirdObject> getB(){ return null; } } 

I am parsing an empty JSON string:

 ObjectMapper mapper = new ObjectMapper(); mapper.readValue("{}", MyObject.class); 

In readValue I get this exception:

 com.fasterxml.jackson.databind.JsonMappingException: Can not find a deserializer for non-concrete Collection type [collection type; class com.j256.ormlite.dao.ForeignCollection, contains [simple type, class com.test.MyThirdObject]] 

This happens when I have two get methods in the MyObject class that return a ForeignCollection . Removing one of the get methods does not throw an exception.

I am really surprised that the handler is looking at get methods, it should just set the fields that I specify.

What's going on here?

+1
json android jackson ormlite
source share
3 answers

I fixed this by converting ForeignCollection to List :

 private ForeignCollection<MyObject> myObjects; public List<MyObject> getMyObjects(){ return new ArrayList<MyObject>(myObjects); } 
+2
source share

You may need to define a custom deserializer for ForeignCollection ; or, if there is a known implementation class, use the annotation:

 @JsonDeserialize(as=ForeignCollectionImpl.class) 

to indicate which particular subclass to use for this abstract type.

+1
source share

You need to use the Guava module in ObjectMapper. Here is the Maven dependency:

 <dependency> <groupId>com.fasterxml.jackson.datatype</groupId> <artifactId>jackson-datatype-guava</artifactId> <version>{whatever is the latest}</version> </dependency> 

In your code:

 ObjectMapper mapper = new ObjectMapper(); // register module with object mapper mapper.registerModule(new GuavaModule()); 

You can omit the annotations @JsonDeserialize and @JsonSerialize .

More details here .

0
source share

All Articles