I've been trying all kinds of solutions to get this working. But without success.
I have a few classes like this
class Level1<T> {
public Level2<T> l2;
}
class Level2<T> {
public Level3<T> l3;
}
class Level3<T> {
public List<T> objectsList;
}
T in this case can be any object, and should be possible to be resolved during runtime.
My JSON looks like this:
{
"Level1": {
"Level2": {
"Level3": {
"genericObject": [
{
"attribute1": ...,
"attribute2": ...
},
{
"attribute1": ...,
"attribute2": ...
}
]
}
}
}
}
The JSON property "genericObject" changes its name and attributes depending of which kind of object I'm receiving.
I've tried defining in my ObjectMapper like this, in which I pass a Class object to my function (genericObjectClass):
JavaType javaType = TypeFactory.defaultInstance().constructParametricType(ArrayList.class, List.class, genericObjectClass);
javaType = TypeFactory.defaultInstance().constructParametricType(Level3.class, javaType);
javaType = TypeFactory.defaultInstance().constructParametricType(Level2.class, javaType);
javaType = TypeFactory.defaultInstance().constructParametricType(Level1.class, javaType);
Level1<genericObject> l1 = objectMapper.readValue(jsonString, javaType);
Tried solutions:
(de)serialising Nested Generics in Jackson
How to use Jackson with Generics
Is Jackson really unable to deserialize json into a generic type?