I'm getting the following exception when trying to deserialize with ObjectMapper onto a parameterized class (works fine for non-parametrized classes):
java.lang.ClassCastException: class java.util.LinkedHashMap cannot be cast to class com.xyz.A (java.util.LinkedHashMap is in module java.base of loader 'bootstrap'; com.xyz.A is in unnamed module of loader 'app')
Here's the original code:
Foo<A> request = OBJECT_MAPPER.readValue(payload, Foo.class);
I tried:
Foo<A> request = OBJECT_MAPPER.readValue(payload, new TypeReference<Foo<A>>() {});
As well as:
JavaType myType = OBJECT_MAPPER.getTypeFactory()
.constructParametricType(Foo.class, A.class);
Foo<A> request = OBJECT_MAPPER.readValue(payload, myType);
But I still get the same exception.
Could there be something special about my scenario that's not covered in these questions?
- Jackson is not deserialising a generic list that it has serialised
- Jackson and generic type reference
- Jackson - Deserialize using generic class
One thing I can think of is that my Foo
is actually an @AutoMatter
-annotated interface that generates the class:
@AutoMatter
public interface Foo<T> {
Optional<T> parent;
Optional<List<T>> children;
}
Normally we have no issues mapping onto AutoMatter-generated classes though. It's just adding the parametrization <T>
that seems to be causing issues.
Does anyone have an idea?
Edit to answer @MichalZiober's questions:
In my test code I'm actually just serializing what I know is a valid object, i.e. then deserializing that to get back the object I started with:
Foo<A> myExampleObject;
ByteString.encodeUtf8(OBJECT_MAPPER.writeValueAsString(myExampleObject));
Edit 2
Okay, so it looks like we are already importing that module:
@VisibleForTesting
public static final ObjectMapper OBJECT_MAPPER = new ObjectMapper()
.registerModule(new JodaModule())
.registerModule(new GuavaModule())
.registerModule(new AutoMatterModule())
.registerModule(new Jdk8Module())
.registerModule(new ProtobufModule())
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false)
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false);