This is a well-known problem with Java type erasure: T is just a type variable, and you must indicate actual class, usually as Class argument. Without such information, best that can be done is to use bounds; and plain T is roughly same as 'T extends Object'. And Jackson will then bind JSON Objects as Maps.

In this case, tester method needs to have access to Class, and you can construct

JavaType type = mapper.getTypeFactory().
  constructCollectionType(List.class, Foo.class)

and then

List<Foo> list = mapper.readValue(new File("input.json"), type);
Answer from StaxMan on Stack Overflow
🌐
GitHub
github.com › FasterXML › jackson-databind › issues › 1490
TypeReference doesn't work with generic type parameter · Issue #1490 · FasterXML/jackson-databind
January 8, 2017 - I have a method that convert json string into a Map with generic value type: public <T> Map<String, T> convert(String str) throws IOException { ObjectMapper mapper = new ObjectMapper(); return mapper.readValue(str, new TypeReference<Map<String, T>>() {}); } The example usage: Map<String, Foo> map = obj.convert(jsonString); When run, I got error: Exception in thread "main" java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.example.Foo ·
Author: FasterXML
Top answer
1 of 3
267

This is a well-known problem with Java type erasure: T is just a type variable, and you must indicate actual class, usually as Class argument. Without such information, best that can be done is to use bounds; and plain T is roughly same as 'T extends Object'. And Jackson will then bind JSON Objects as Maps.

In this case, tester method needs to have access to Class, and you can construct

JavaType type = mapper.getTypeFactory().
  constructCollectionType(List.class, Foo.class)

and then

List<Foo> list = mapper.readValue(new File("input.json"), type);
2 of 3
12

'JavaType' works !! I was trying to unmarshall (deserialize) a List in json String to ArrayList java Objects and was struggling to find a solution since days.
Below is the code that finally gave me solution. Code:

JsonMarshallerUnmarshaller<T> {
    T targetClass;

    public ArrayList<T> unmarshal(String jsonString) {
        ObjectMapper mapper = new ObjectMapper();

        AnnotationIntrospector introspector = new JacksonAnnotationIntrospector();
        mapper.getDeserializationConfig()
            .withAnnotationIntrospector(introspector);

        mapper.getSerializationConfig()
            .withAnnotationIntrospector(introspector);
        JavaType type = mapper.getTypeFactory().
            constructCollectionType(
                ArrayList.class, 
                targetclass.getClass());

        try {
            Class c1 = this.targetclass.getClass();
            Class c2 = this.targetclass1.getClass();
            ArrayList<T> temp = (ArrayList<T>) 
                mapper.readValue(jsonString,  type);
            return temp ;
        } catch (JsonParseException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null ;
    }  
}
🌐
GitHub
github.com › FasterXML › jackson-databind › issues › 3129
TypeReference not working as expected in generic context · Issue #3129 · FasterXML/jackson-databind
April 22, 2021 - You cannot use TypeReference with type variables: this will not and cannot work due to Java Type Erasure: no type information is passed and
🌐
Fasterxml
fasterxml.github.io › jackson-core › javadoc › 2.2.0 › com › fasterxml › jackson › core › type › TypeReference.html
TypeReference (Jackson-core 2.2.0 API)
Usage is by sub-classing: here is one way to instantiate reference to generic type List<Integer>: TypeReference ref = new TypeReference<List<Integer>>() { }; which can be passed to methods that accept TypeReference, or resolved using TypeFactory to obtain ResolvedType.
🌐
Baeldung
baeldung.com › home › java › core java › super type tokens in java generics
Super Type Tokens in Java Generics | Baeldung
June 21, 2025 - Parsing a JSON object into a Map<String, String> can be accomplished by defining that type with a super type token: TypeReference<Map<String, String>> token = new TypeReference<Map<String, String>>() {}; Map<String, String> json = objectMapper.readValue(data, token); In this tutorial, we learned how we can use super type tokens to preserve the generic type information at runtime.
🌐
DEV Community
dev.to › emilossola › a-comprehensive-guide-for-java-typereference-249m
A Comprehensive Guide for Java TypeReference - DEV Community
July 10, 2023 - It enables developers to access and manipulate the type information of generic objects, even when the type is erased due to type erasure in Java. The primary purpose of the TypeReference is to overcome the limitations of type erasure and provide ...
🌐
Adobe Developer
developer.adobe.com › experience-manager › reference-materials › 6-5 › javadoc › com › fasterxml › jackson › core › type › TypeReference.html
TypeReference (The Adobe AEM Quickstart and Web Application.)
public abstract class TypeReference<T> extends java.lang.Object implements java.lang.Comparable<TypeReference<T>> This generic abstract class is used for obtaining full generics type information by sub-classing; it must be converted to ResolvedType implementation (implemented by JavaType from ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › java › api › com.azure.core.util.serializer.typereference
TypeReference<T> Class | Microsoft Learn
This class represents a generic Java type, retaining information about generics. ... // Construct a TypeReference<T> for a Java generic type. // This pattern should only be used for generic types, for classes use the createInstance factory method.
🌐
Baeldung
baeldung.com › home › json › jackson › deserialize generic type with jackson
Deserialize Generic Type with Jackson | Baeldung
September 17, 2025 - TypeReference<JsonResponse<User>> typeRef = new TypeReference<JsonResponse<User>>() {}; This approach for preserving the generic type information is known as a super type token.
Find elsewhere
🌐
Northcoder
northcoder.com › post › jackson-object-mapper-which-way-is
Jackson's ObjectMapper and TypeReference | northCoder
January 15, 2021 - The javadoc states that this is because doing so forces a compile-time check to ensure you have provided a type when instantiating a new TypeReference object: ... …bogus implementation of Comparable (any such generic interface would do, as long as it forces a method with generic type to be implemented), to ensure that a Type argument is indeed given.
🌐
CodingTechRoom
codingtechroom.com › question › jackson-typereference-generics
How to Utilize Jackson's TypeReference with Generics - CodingTechRoom
Use Jackson's TypeReference class to create anonymous subclasses that can carry type information throughout the serialization and deserialization processes. When parsing JSON data into Java collections with generics, instantiate TypeReference with your desired type, such as List<Person>.
🌐
TutorialsPoint
tutorialspoint.com › jackson › jackson_data_binding_generics.htm
Jackson - Data Binding with Generics
In simple data binding, we've used Map class which use String as key and Object as a value object. Instead we can have concrete java object and type cast it to be used in JSON binding.
🌐
Fasterxml
fasterxml.github.io › jackson-core › javadoc › 2.10 › com › fasterxml › jackson › core › type › TypeReference.html
TypeReference (Jackson-core 2.10.0 API)
Usage is by sub-classing: here is one way to instantiate reference to generic type List<Integer>: TypeReference ref = new TypeReference<List<Integer>>() { }; which can be passed to methods that accept TypeReference, or resolved using TypeFactory to obtain ResolvedType.
🌐
Adobe Developer
developer.adobe.com › experience-manager › reference-materials › cloud-service › javadoc › com › fasterxml › jackson › core › type › TypeReference.html
TypeReference (The Adobe Experience Manager SDK 2022.11.9850.20221116T162329Z-220900)
public abstract class TypeReference<T> extends java.lang.Object implements java.lang.Comparable<TypeReference<T>> This generic abstract class is used for obtaining full generics type information by sub-classing; it must be converted to ResolvedType implementation (implemented by JavaType from ...
🌐
GitHub
gist.github.com › sarxos › 396f9d0bd85fd9ef2e80
Pass type reference from generic argument · GitHub
Pass type reference from generic argument · Raw · TypeReference.java · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.