Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java unmarshilling JSON data containg abstract type

We are using Jersey/Jackson to unmarshall JSON data to java DTOs. One of my DTO is an abstract class, and i would like to unmarshall the JSON data to one of his extended DTO. For example, assuming i have these DTOs :

public abstract class AnimalDTO{}

public class DogDTO extends AnimalDTO{}

public class CatDTO extends AnimalDTO{}

I would like to unmarshall this JSON data:

{Zoo: {Animals:[{"type"="DogDTO", "code"="001", "name"="chihuahua"}, {"type"="CatDTO", "code"="002", "name"="felix"}]}}

As "type" would give the type of DTO i would like to unmarshall to. But it seems that this property isn't considered. Is there something I missed, or mistook in the JSON syntax?

Thanks.

like image 326
trin86 Avatar asked Dec 26 '22 03:12

trin86


1 Answers

In your case you should use @JsonTypeInfo annotation.

For more information, please see below links:

  • JacksonFAQ.
  • Jackson 1.5: Polymorphic Type Handling, first steps.

Using above links I have created a simple example which serialize POJO objects with class names:

import java.io.StringWriter;
import java.util.Arrays;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.databind.ObjectMapper;

public class JacksonProgram {

    public static void main(String[] args) throws Exception {
        DogDTO dog = new DogDTO();
        dog.setCode("001");
        dog.setName("chihuahua");

        CatDTO cat = new CatDTO();
        cat.setCode("002");
        cat.setName("felix");

        Zoo zoo = new Zoo();
        zoo.setAnimals(new AnimalDTO[] { dog, cat });

        Data data = new Data();
        data.setZoo(zoo);

        ObjectMapper objectMapper = new ObjectMapper();

        StringWriter writer = new StringWriter();
        objectMapper.writeValue(writer, data);
        System.out.println(writer);
    }
}

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type")
abstract class AnimalDTO {

    private String code;
    private String name;

    public String getCode() {
        return code;
    }

    public void setCode(String code) {
        this.code = code;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "AnimalDTO [code=" + code + ", name=" + name + "]";
    }
}

class DogDTO extends AnimalDTO {
}

class CatDTO extends AnimalDTO {
}

class Zoo {

    @JsonProperty(value = "Animals")
    private AnimalDTO[] animals;

    public AnimalDTO[] getAnimals() {
        return animals;
    }

    public void setAnimals(AnimalDTO[] animals) {
        this.animals = animals;
    }

    @Override
    public String toString() {
        return "Zoo [animals=" + Arrays.toString(animals) + "]";
    }
}

class Data {

    @JsonProperty(value = "Zoo")
    private Zoo zoo;

    public Zoo getZoo() {
        return zoo;
    }

    public void setZoo(Zoo zoo) {
        this.zoo = zoo;
    }

    @Override
    public String toString() {
        return "Data [zoo=" + zoo + "]";
    }
}

This program prints:

{"Zoo":{"Animals":[{"type":"DogDTO","code":"001","name":"chihuahua"},{"type":"CatDTO","code":"002","name":"felix"}]}}
like image 86
Michał Ziober Avatar answered Jan 13 '23 18:01

Michał Ziober