Question

How do you receive serialization events? You can define

void writeObject(ObjectOutputStream out) {
  // handle event
  out.defaultWriteObject(this);
}

in java serialization and this method will be called when your object is serialized. How do you do the same thing in Kryo? Both KryoSerializable and Externalizable have the problem of default serialization: once you invoked your event handler, you want the default read/write object. But there is no such thing! ? You may invoke FieldSerializer in read(Kryo, Input) to read the fields of the object, but it will produce you a new object instead of populating the current one. For this reason, I tried to introduce a custom serializer:

Serializer def = kryo.getDefaultSerializer(A.class)
kryo.addDefaultSerializer(A.class, new Serializer() {
    public void write(Kryo kryo, Output output, Object object) {
        ((A)object).serializationEvent();
        def.write(kryo, output, object);

But, I mentioned that through subclasses of A receive the serializationEvent() event, only A.class fields are serialized. So, this does not work for class B extends A. I also tried the solution proposed by Natan: register(A.class, new FieldSerializer(A.class, myhandler. This serializes all fields, including subclasses, but, the custom serializer is not invoked for subclasses at all. So, I decided that Kryo customization works only for final classes. Nathan says that this conclusion is "invalid" and KryoSerializable solution "application-specific" and thinking otherwise "rude". Despite of such resolution, I decided to publish the general method I discovered.

Was it helpful?

Solution

I have discovered two solutions. At first, overriding writeReferenceOrNull can work

Kryo kryo = new Kryo() {
    public boolean writeReferenceOrNull (Output output, Object object, boolean mayBeNull) {
        if (object instanceof A) {
            ((A) object).serializationEvent();
        }

        return super.writeReferenceOrNull(output, object, mayBeNull);
    }

But, it needs source code visibility change and Natan says that this works only when references are enabled (in the default case) and recommends a more reliable approach instead: overriding the newDefaultSerializer:

public class EventFiringKryo extends Kryo {
    protected Serializer newDefaultSerializer(Class type) {
        final Serializer def = super.newDefaultSerializer(type);
        Serializer custom = new Serializer() {

            public void write(Kryo kryo, Output output, Object object) {
                System.err.println("writing " + object + ":" + object.getClass().getSimpleName());
                if (object instanceof A)
                    ((A)object).serializationEvent();
                def.write(kryo, output, object);
            }

            public Object read(Kryo kryo, Input input, Class type) {
                Object result = def.read(kryo, input, type);
                if (result instanceof SomeAnotherType)
                    result.canInitializeSomethingElse();
                return result;
            }
        };
        return custom;
    }

}

In addition to being effective, this method does not suffer from the need to carefully register all classes that implement the interface that you are going to invoke.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top