I'm driving myself up a wall just trying to serialize a class that contains a simple java.nio.file.Path field in Kryo 4.0.1. I've written a PathKryoSerializer:
public class PathKryoSerializer extends Serializer<Path> {
public void write(Kryo kryo, Output output, Path path) {
System.out.println("Serializing Path: " + path.toString());
kryo.writeObject(output, path.toString());
}
public Path read(Kryo kryo, Input input, Class<Path> type) {
return Paths.get(kryo.readObject(input, String.class));
}
}
I've tried @Binding the field in my class:
public class MyClass {
@Bind(PathKryoSerializer.class)
private final Path path
...
}
I've registered the serializer with Kryo AND added it as a default serializer
Kryo kryo = new Kryo();
kryo.setRegistrationRequired(true);
kryo.register(Path.class, new PathKryoSerializer());
kryo.addDefaultSerializer(Path.class, new PathKryoSerializer());
I even check that the kryo instance is supposed to use my serializer!
System.out.println("MyClass serializer field 'path' serializer class: "
+ ((FieldSerializer) kryo.getSerializer(MyClass.class))
.getField("path")
.getSerializer()
.getClass()
.getName());
Output:
MyClass serializer field 'path' serializer class: mypackage.serializer.PathKryoSerializer
And yet. Every single time I not only don't get an output from the println in my write method (indicating that it's not being called), I get this error:
com.esotericsoftware.kryo.KryoException: java.lang.IllegalArgumentException: Class is not registered: sun.nio.fs.UnixPath
Note: To register this class use: kryo.register(sun.nio.fs.UnixPath.class);
Serialization trace:
path (mypackage.MyClass)
at com.esotericsoftware.kryo.serializers.ObjectField.write(ObjectField.java:101
at com.esotericsoftware.kryo.serializers.FieldSerializer.write(FieldSerializer.java:508)
at com.esotericsoftware.kryo.Kryo.writeObject(Kryo.java:557)
I'm at my wit's end trying to figure out how to do something as simple as serializing a standard Java 7 Path field. What am I missing?