Junfeic / kryo

Automatically exported from code.google.com/p/kryo
BSD 3-Clause "New" or "Revised" License
0 stars 0 forks source link

Serialization of File Object munges Path #136

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
File file=new File("/foo/bar")
Kryo kryo = new Kryo();
kryo.register(File.class);
kryo.setInstantiatorStrategy(new StdInstantiatorStrategy());

Round trip serialize/deserialize

What is the expected output? What do you see instead?

The deserialized instance File file2.getAbsolutePath()  is relative to the 
current directory. Apparently because the internal prefixLength property is 0.  
I was able to resolve it setting the prefixLengthField via reflection. 
Something like this. Not too robust, but you get the idea:

File file = kryo.readObject(input, File.class);
if (file.getPath().startsWith("/")) {
    DirectFieldAccessor dfa = new DirectFieldAccessor(file);
    dfa.setPropertyValue("prefixLength", 1);
}
return file; 

What version of the Kryo are you using?
2.21

Please provide any additional information below.

Original issue reported on code.google.com by dturan...@gmail.com on 2 Oct 2013 at 5:57

GoogleCodeExporter commented 9 years ago
Actually, upon further review, prefixLength is transient.  So not a bug, but if 
you have a better work around, I'd love to here it

Original comment by dturan...@gmail.com on 2 Oct 2013 at 6:00

GoogleCodeExporter commented 9 years ago
Kryo 2.22 was release just 2 days ago. Could you try with this version? It 
introduced some improvements when it comes to handling of transient fields 
inside classes, which seems to be the case here ("private transient int 
prefixLength").

Normally, transient fields are not serialized by default. But they are copied, 
if you use the Kryo.copy() method.

BTW, your way of solving the problem is not wrong. Basically, classes having 
transient fields usually require special handling. This is why java.io.File 
provides writeObject/readObject methods. For Kryo you can create a custom 
serializer for the File class and then register it.

Have you looked at this project?
https://github.com/magro/kryo-serializers

It provides a lot of useful serializers for many classes.

Original comment by romixlev on 2 Oct 2013 at 6:06

GoogleCodeExporter commented 9 years ago

Original comment by romixlev on 17 Oct 2013 at 7:16