Is it possible to serialise/de-serialise an object with Kryo by registering an interface instead of a concreate class?
In concreate I need to serialise a Java 7 Path
object which is defined as in interface.
I tried writing a serialiser that saves the path URI as a string and recreates it during the read deserialisation. But it turns out the my serialise writer method is never invoked by Kryo.
This is my (Groovy) code:
class PathSerializer extends FieldSerializer<Path> {
PathSerializer(Kryo kryo) {
super(kryo, Path)
}
public void write (Kryo kryo, Output output, Path path) {
def uri = path.toUri().toString()
kryo.writeObject(output, uri)
}
public Path read (Kryo kryo, Input input, Class<Path> type) {
def uri = kryo.readObject(input,String)
Paths.get(new URI(uri))
}
}
def kryo = new Kryo()
kryo.register(Path, new PathSerializer(kryo))
def path = Paths.get('hola')
Output output = new Output(new FileOutputStream("file.bin"));
kryo.writeObject(output, path);
output.close();
Any idea how to register an interface for serialization with Kryo?
Thanks
Use the addDefaultSerializer
method:
kryo.addDefaultSerializer(Path, new PathSerializer(kryo))
来源:https://stackoverflow.com/questions/21076136/how-serialise-de-serialise-an-object-with-kryo-by-using-an-interface