Charles A
Charles A

Reputation: 404

Serializing and Deserializing Object Arrays in Kryo

I've been testing Kryo for serialization and deserialization recently and have generally been happy with it, however it is not clear to be how to handle the (de)serialization of a class which contains an object array. The class contains final fields, so I don't seem to be able to use the default FieldSerializer (The error being "Class cannot be created (missing no-arg constructor)", but a no-arg constructor being inappropriate for a 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 would one efficiently write a serializer/deserializer to handle this? My assumption is that I'm missing something in com.esotericsoftware.kryo.io.Input which will let me do this in a custom serializer, but that might be the wrong track..

Upvotes: 4

Views: 3710

Answers (1)

Mike Herasimov
Mike Herasimov

Reputation: 1359

question was asked 3 years ago, so this is kinda silly to write answer for it, but I found a solution, described directly in kryo's readme file

here is a link

Basically when you serializing object by simple call of writeObject(Output, Object) without providing your own serializer, kryo uses default FieldSerializer that requires non argument constructor.

You can provide private non argument constructor which kryo will invoke using reflection mechanism. I think it's suitable way to do, what you wanted because private zero argument constructor doesn't violates architecture principles

Upvotes: 2

Related Questions