1

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?

saimonsez
  • 344
  • 3
  • 16
  • Hazelcast allows to use non-generic objects in its global serialization, but you need to find a third-party serializer to perform this job. I am not an expert on it, but it looks `jackson-smile` is not the right candidate for it. – Alparslan Avci Nov 20 '18 at 10:38

1 Answers1

0

Please check this as an example: https://github.com/gokhanoner/generic-serializer/blob/master/member/src/main/java/test/BBSerializer.java

Use StremSerializer & write class name before serializing the object. Then while deserializing, get class name & pass it to objectnapper.readValue method

Gokhan Oner
  • 3,237
  • 19
  • 25