Deserialize JSON collection to collection of Objects using Jackson

suggest change

Suppose you have a POJO class Person. Jackson does not use an unmarked constructor when binding properties, so the property name is given explicitly:

public class Person {
    public String name;

    @JsonCreator
    public Person(@JsonProperty("name") String name) {
        this.name = name;
    }
}

To deserialize a JSON array or a map of Person objects, the collection type has to be supplied. Type erasure means a List<Person> or Map<String, Person> class object does not exist at runtime. Jackson accepts that information through TypeFactory or TypeReference. mapper and factory are created under Details below. readValue throws checked IOException.

TypeFactory

The approach taken here is to use a factory (and its static utility function) to build your type for you. The parameters it takes are the collection you want to use (list, set, etc.) and the class you want to store in that collection.

TypeReference

The type reference approach seems simpler because it saves you a bit of typing and looks cleaner. TypeReference accepts a type parameter, where you pass the desired type List<Person>. You simply instantiate this TypeReference object and use it as your type container.

Now let’s look at how to actually deserialize your JSON into a Java object. If your JSON is formatted as an array, you can deserialize it as a List. If there is a more complex nested structure, you will want to deserialize to a Map. We will look at examples of both.

Deserializing JSON array

String jsonString = "[{\"name\": \"Alice\"}, {\"name\": \"Bob\"}]";

TypeFactory approach

CollectionType listType = 
    factory.constructCollectionType(List.class, Person.class);
List<Person> list = mapper.readValue(jsonString, listType);

TypeReference approach

TypeReference<List<Person>> listType = new TypeReference<List<Person>>() {};
List<Person> list = mapper.readValue(jsonString, listType);

Deserializing JSON map

String jsonString = "{\"0\": {\"name\": \"Alice\"}, \"1\": {\"name\": \"Bob\"}}";

TypeFactory approach

MapType mapType =
    factory.constructMapType(Map.class, String.class, Person.class);
Map<String, Person> people = mapper.readValue(jsonString, mapType);

TypeReference approach

TypeReference<Map<String, Person>> mapType = new TypeReference<Map<String, Person>>() {};
Map<String, Person> people = mapper.readValue(jsonString, mapType);

Details

Import statement used:

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.type.CollectionType;
import com.fasterxml.jackson.databind.type.MapType;

Instances used:

ObjectMapper mapper = new ObjectMapper();
TypeFactory factory = mapper.getTypeFactory();

Note

While TypeReference approach may look better it has several drawbacks:

  1. Instantiate TypeReference as an anonymous subclass so the type arguments survive erasure.
  2. Write those arguments on that subclass: new TypeReference<List<Person>>() {}. A raw new TypeReference() {} drops them.

Dropping the arguments makes Jackson bind the elements as Object or fail.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents