Serialization and deserialization of arrays of objects in Cryo
I recently tested Kryo for serialization and deserialization and was generally happy with it, however it is not clear how to handle a series (de) of a class that contains an array of objects. The class contains final fields, so I can't seem to use the default FieldSerializer (error "Class could not be created (no no-arg constructor)", but no-arg constructor is inappropriate for the final primitive). So, given the class
@AllArgsConstructor
public class DataObject{
private final double field1;
private final double field2;
private SubObject[] children;
}
@AllArgsConstructor
public class SubObject{
private final double field1;
private final double field2;
}
How do I write a serializer / deserializer efficiently to handle this? My guess is that I am missing something in com.esotericsoftware.kryo.io.Input that will allow me to do this in a custom serializer, but that might not be the right way.
source to share
the question was asked 3 years ago so it is silly to write an answer for it, but I found the solution described directly in the kryo
readme file
here is the link
Basically, when you serialize an object with a simple call writeObject(Output, Object)
without providing your own serializer, it kryo
uses the default FieldSerializer
, which requires a no-argument constructor.
You can provide a private no-argument constructor that kryo
will be called using the mechanism reflection
. I think this is a suitable way to do what you need, because the private constructor of zero arguments does not violate architecture principles.
source to share