Home > Software design >  Deserialize a list of objects with Jackson
Deserialize a list of objects with Jackson

Time:12-22

I have a List of objects each of them has specified fields(type of variable) so i thought that i will create a main class(super class) with the shared elements(variables) and two subclasses for the specific type of variables. I want to deserialize all of Subclasses as the Type of the superclass so that i can put all of this Jsons in List of the same objects.

Here is an example of my Json

[ { "query": "age", "type": "numeric", "min": 5, "max": 99 }, { "query": "diagnosis", "type": "string", "in": ["husten", "schnupfen"] } ]

the code i wrote to deserialize this is:

public class Query{
    private String query;
    private String type;
    // Getters and Setters and constructor
}

public class QueryString extends Query implements Serializable {
    private List<String> in;
    private String like;
    // Getters and Setters and constructor
}

public class QueryNum extends Field implements Serializable {
    private Number min;
    private Number max;
    // Getters and Setters and constructor
}

The serialization using ObjectMapper is working as expected but the by deserialization the compiler gives me that there is an unrecognized values ( which are the fields of my sublasses).

I want to get a List of objects(Query) List which contains QueryString and QueryNum.

is that possible with Jackson Json in Java ?

for the desrialization i used :

    ObjectMapper mapper = new ObjectMapper();

    List<Query> queries= Arrays.asList(mapper.readValue(JsonString, Query[].class));

Thanks in advance

CodePudding user response:

If you have a field in each object that can be used to recognize the object to build you can use the annotations @JsonTypeInfo:

Annotation used for configuring details of if and how type information is used with JSON serialization and deserialization, to preserve information about actual class of Object instances. This is necessarily for polymorphic types, and may also be needed to link abstract declared types and matching concrete implementation.

and @JsonSubTypes:

Annotation used with JsonTypeInfo to indicate sub types of serializable polymorphic types, and to associate logical names used within JSON content (which is more portable than using physical Java class names).

or also JsonTypeName:

Annotation used for binding logical name that the annotated class has. Used with JsonTypeInfo (and specifically its JsonTypeInfo.use() property) to establish relationship between type names and types.

Basically you need to define a property holding the informations related to the class to be instantiated in the @JsonTypeInfo and define the possible values in @JsonSubTypes

Here is an example

@JsonTypeInfo(
  use = JsonTypeInfo.Id.NAME, 
  include = As.PROPERTY, 
  property = "type")
@JsonSubTypes({
    @JsonSubTypes.Type(value = Dog.class, name = "dog"),
    @JsonSubTypes.Type(value = Cat.class, name = "cat")
})
public class Animal {
    // The property type is used to know what object instantiate
    private String type;
    ...
}

@JsonTypeName("dog")
public class Dog extends Animal {
    ...
}

@JsonTypeName("cat")
public class Cat extends Animal {
    ...
}

If you don't have such field you need to create a custom deserializer.

An alternative is to convert your records to a list of Map because a Map can hold any valid object.

  • Related