Reputation: 31
I am using the following code to add hooks to Kryo deserialization.
private static class KryoWithHooks extends Kryo {
private static final class SerializerWithHook extends com.esotericsoftware.kryo.Serializer {
private final com.esotericsoftware.kryo.Serializer old;
private final Hook hook;
private SerializerWithHook(com.esotericsoftware.kryo.Serializer old, Hook hook) {
this.old = old;
this.hook = hook;
}
@Override
public Object read(Kryo kryo, Input input, Class type) {
hook.preRead(type);
return hook.postRead(old.read(kryo, input, type));
}
@Override
public void write(Kryo kryo, Output output, Object object) {
// Add write hooks if needed.
old.write(kryo, output, object);
}
}
public void addHook(Class<?> k, Hook h) {
register(k, new SerializerWithHook(newDefaultSerializer(k), h));
}
}
}
Then I add hooks after registering all the explicit classes. So far so good. The question is, how do I do the same if I don't know the class in advance, and only know the base class? I.e. I have
class Container {
private Contained c;
}
Contained can have any number of subclasses, and I want to intercept its deserialization /before it is instantiated/. If I add the hook for Contained.class above, that doesn't get called (understandably), even though the hook for e.g. Container works fine.
Upvotes: 3
Views: 387