Jackson examples
From DarkWiki
Interface members
Sometimes, an (imposed) interface demands that we return a collection of interfaces from a member. Consider a 'Family' which contains a number of 'Person' (people). As both . Family and Person are interfaces, they have concrete implementations FamilyImpl and PersonImpl respectively. This example shows how you can instruct Jackson to deserialise JSON into the object model. As you can't instantiate a Person, you tell it to use the PersonImpl.
package andy.jackson.demo.model.impl;
import andy.jackson.demo.model.Person;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import java.util.ArrayList;
import java.util.List;
public class FamilyImpl implements andy.jackson.demo.model.Family {
/*
As the interface (andy.jackson.demo.model.Family) requires we emit a List of andy.jackson.demo.model.Person (another interface), we
make use the JsonDeserialize annotation to tell Jackson to create the list using the PersonImpl concrete implementation class.
*/
@JsonDeserialize(as=List.class,contentAs=PersonImpl.class)
private List<Person> people = new ArrayList<>();
private String surname;
@Override
public List<Person> getPeople() {
return people;
}
public void setPeople(List<Person> people) {
this.people = people;
}
@Override
public String getSurname() {
return surname;
}
public void setSurname(String surname) {
this.surname = surname;
}
}