Jackson: TypeReference

Yichun ZhaoYichun Zhao
2 min read

TypeReference is a class provided by the Jackson library in Java, used to convey generic type information. Java's generic type information is lost during runtime due to type erasure, but sometimes you need this information for operations like JSON serialization and deserialization.

When you work with generics in Java, type erasure means that the generic type parameters are not available at runtime. For instance, when you have a List<String>, at runtime, it's just seen as a List, with no information about the String type parameter. This becomes a problem with libraries like Jackson, which need to know the exact type of elements in a collection for proper serialization and deserialization.

Purpose of TypeReference

Jackson's TypeReference is used to overcome the limitations of type erasure. It captures and passes generic type information so that Jackson knows the specific types to serialize or deserialize, especially for complex, nested, or generic types.

How TypeReference Works

TypeReference is an abstract class that you subclass to provide type information. By creating an anonymous subclass, you allow Jackson to infer the actual type parameters for generic types.

Example Usage of TypeReference

Consider a case where you have a JSON string that represents a list of Action objects (assuming Action is a custom class), and you want to deserialize this JSON into a List<Action>.

Without TypeReference, Jackson would not know the list's element type and might deserialize the JSON into a List of LinkedHashMap objects or something similar.

Here's how you would use TypeReference:

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import java.util.List;

public class JsonExample {
    public static void main(String[] args) throws Exception {
        ObjectMapper mapper = new ObjectMapper();
        String json = "[{\"name\": \"Action1\", \"description\": \"Description1\"}, {\"name\": \"Action2\", \"description\": \"Description2\"}]";

        List<Action> actions = mapper.readValue(json, new TypeReference<List<Action>>() {});
        System.out.println(actions);
    }
}

class Action {
    private String name;
    private String description;
    // Getters, setters, toString()
}

In this example, new TypeReference<List<Action>>() {} creates an anonymous subclass of TypeReference with the type List<Action>. This provides Jackson with the necessary type information to deserialize the JSON string into a List of Action objects.

Key Points

  • TypeReference is useful for conveying type information for generic types.
  • It's especially helpful for deserialization, where Jackson needs to know the specific type of elements in a collection or map.
  • You use it by creating an anonymous subclass, thereby preserving the type information that would otherwise be lost due to type erasure.
0
Subscribe to my newsletter

Read articles from Yichun Zhao directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Yichun Zhao
Yichun Zhao