As an example there is a method with the generic type:
public static <T extends Serializable> ArrayDeque<Pair<String, T>> deserialize(String input)
throws IOException {
// Pair is java.io.Serializable: Pair<F extends Serializable, S extends Serializable>
ArrayDeque<Pair<String, T>> transformedResult = new ArrayDeque<>();
// The issue is here with the constructing TypeReference
List<Map<String, T>> result = objectMapper.readValue(input,
new TypeReference<List<Map<String, T>>>() {
});
// Here goes the transformation of the list in the final result; it does not matter in this context
return transformedResult;
}
I am calling this method as is:
// CustomObject implements Serializable
Deque<Pair<String, CustomObject>> result = deserialize("input");
I expect that it takes T
as CustomObject
and creates TypeReference
as TypeReference<List<Map<String, CustomObject>>>
In fact, when I run the unit test it gets an error with Serializable
:
com.fasterxml.jackson.databind.exc.InvalidDefinitionException:
Cannot construct instance of `java.io.Serializable` (no Creators, like default construct, exist):
abstract types either need to be mapped to concrete types, have custom deserializer, or contain additional type information
at [Source: (String)"string-to-parse-goes-here"; line: 1, column: 13] (through reference chain: java.util.ArrayList[0]->java.util.LinkedHashMap["part-of-the-string-to-parse"])
So I assume that the route is because of the "type erasure" the T is promoted into the highest constrained type
in the step with the new TypeReference<List<Map<String, T>>>
.
The questions: is it possible to transform current method to keep the generic approach for the constructing different types of the TypeReferences?