I'm trying to register a global serializer using jackson-smile for hazelcast.
public class GlobalJacksonSmileSerializer implements ByteArraySerializer<Object> {
private final ObjectMapper mapper = new ObjectMapper(new SmileFactory());
public int getTypeId() { return 1; }
public void destroy() { }
@Override
public byte[] write(final Object object) throws IOException {
return mapper.writeValueAsBytes(object);
}
@Override
public Object read(final byte[] bytes) throws IOException {
return mapper.readValue(bytes, Object.class);
}
}
When deserializing an object containing a generic collection, e.g.
public class Foo {
private List<Bar> bars;
}
it fails:
Caused by: java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to Bar
According to other answers here on stackoverflow (e.g. java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to com.testing.models.Account) this is a jackson limitation.
Since this is a global byte-array serializer, I can not be more specific about collection-types. Is there a way to handle this other than implement a serializer for each class?