If I use Java serializer, I can write the following scala code:
trait BeforeAndAfterSerialize extends Serializable {
def beforeWrite(): Unit = {}
def afterRead(): Unit = {}
private def writeObject(out: ObjectOutputStream): Unit = {
beforeWrite()
out.defaultWriteObject()
}
private def readObject(in: java.io.ObjectInputStream): Unit = {
in.defaultReadObject()
afterRead()
}
}
Such that after a new instance is created through deserialization, the afterRead part of the code can be run to initialize the new instance.
How to do the same in Kryo?
P.S. In this situation I'm not allowed to get or modify the Kryo instance being used, because it is created and managed by a third party software (e.g. Apache Spark)
UPDATE 1, this is a more general case of a more specific problem:
For any Kryo instance, the behaviour of BeforeAndAfterSerialize can always be customised by overriding the class KryoSerializable, which has 2 abstract functions:
trait BeforeAndAfterSerialize extends Serializable with KryoSerializable {
....
override def write(kryo: Kryo, output: Output): Unit = ???
override def read(kryo: Kryo, input: Input): Unit = ???
}
If I only need to customise "write", I need to implement "read" with a default implementation. Now the problem is: what is this default implementation?