Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

android - Can not find deserialize for non-concrete Collection type

I'm using the jackson library to map JSON into objects. I've simplified the problem a lot, this is what happens:

public class MyObject{

    public ForeignCollection<MySecondObject> getA(){
        return null;
    }

    public ForeignCollection<MyThirdObject> getB(){
        return null;
    }
}

I'm parsing the an empty JSON string:

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

On 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 which return a ForeignCollection. Removing one of the get methods results in no exceptions.

I'm actually surprised by the fact that the mapper looks at the get methods, it should just set the fields I indicate.

What is happening here?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You need to use the Guava module in your 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 @JsonDeserialize and @JsonSerialize annotations.

More info here.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...