Search code examples
javajsonjacksonjackson-databind

Problem Converting Json to Collection with Jackson ObjectIdGenerator


I am trying to convert the following JSON to a collection in Java

"films" : [
        {
            "fid" : 1, 
            "title" : "The Platform", 
            "year": 2019, 
            "genre": [1,2,3], 
            "director": 1, 
            "cast": [2,3,4] , 
            "description": "A vertical prison with one cell per level. Two people per cell. One only food platform and two minutes per day to feed from up to down. An endless nightmare trapped in The Hole."
        }
],

"tvseries" : [
        {
            "tid" : 1, 
            "title" : "Breaking Bad", 
            "year": 2008, 
            "genre": [9,5,3],
            "creator": 13, 
            "cast": [14,15,16] , 
            "description": " A high school chemistry teacher diagnosed with inoperable lung cancer turns to manufacturing and selling methamphetamine in order to secure his family's future."
        }
],
"people" : [
        { "pid": 1, "name" : "Galder Gaztelu-Urrutia" }
],

 "genres" : [
        { "gid": 1, "genre": "Horror" }
]

The above is just an example of some of the data. My particular problem is with the Film class, specifically the following error: "Problem deserializing property 'director' (expected type: [simple type, class assignment4.model.Person]; actual type: assignment4.model.Film)"

Here is my class structure for Film and Person:

public abstract class Media {
private String title;
private int year;
private String description;
private ArrayList<Genre> genre = new ArrayList<Genre>();
private ArrayList<Person> cast = new ArrayList<Person>();
}

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "fid")
public class Film extends Media {
    private int fid;
    private Person director;

    @Override
    public int hashCode() {
        return Arrays.hashCode(new Object[] { getFid(),  getTitle() });
    }

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "pid")
public class Person {
private int pid;
private String name;

@Override
public int hashCode() {
    return Arrays.hashCode(new Object[] { getPid(),  getName() });
}

public class Collection {

    private Set<Film> films = new TreeSet<Film>();
    private Set<Genre> genres = new TreeSet<Genre>();
    private Set<Person> people = new TreeSet<Person>();
    private Set<Profile> profiles = new TreeSet<Profile>();
    private Set<TV> tvseries = new TreeSet<TV>();
}

All classes have getter and setters for the various properties. I've tried all I can think of, but can't figure out why it can't get the director of the film.


Solution

  • You should have set scope attribute in your POJOs @JsonIdentityInfo annotations:

    @JsonIdentityInfo(
        generator = ObjectIdGenerators.PropertyGenerator.class, 
        property = "pid", scope = Person.class)
    public class Person {
        private int pid;
        private String name;
    // getters/setters/hashcode, etc. ...
    }
    
    @JsonIdentityInfo(
        generator = ObjectIdGenerators.PropertyGenerator.class, 
        property = "gid", scope = Genre.class
    )
    public class Genre {
        private int gid;
        private String genre;
    // getters/setters/hashcode, etc. ...
    }
    
    @JsonIdentityInfo(
        generator = ObjectIdGenerators.PropertyGenerator.class, 
        property = "fid", scope = Film.class
    )
    public class Film extends Media {
        private int fid;
        private String title;
        private Person director;
    // getters/setters/hashcode, etc. ...
    }