kryo列表序列化

MAZ*_*DAK 10 java serialization constructor list kryo

我正在尝试使用Kryo序列化一些对象列表(自定义类的列表:List>).

list2D; // List<List<MyClass>> which is already produced.

Kryo k1 = new Kryo();
Output output = new Output(new FileOutputStream("filename.ser"));
k1.writeObject(output, (List<List<Myclass>>) list2D);
output.close();
Run Code Online (Sandbox Code Playgroud)

到目前为止没问题,它写出了没有错误的列表.但是当我尝试阅读它时:

Kryo k2 = new Kryo();
Input listRead = new Input(new FileInputStream("filename.ser"));
List<List<Myclass>> my2DList = (List<List<Myclass>>) k2.readObject(listRead,  List.class);
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Exception in thread "main" com.esotericsoftware.kryo.KryoException: Class cannot be created (missing no-arg constructor): java.util.List
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?

xia*_*owl 6

List.class当读取对象时,您无法使用,因为它List是一个接口.

k2.readObject(listRead,  ArrayList.class);
Run Code Online (Sandbox Code Playgroud)


Maj*_*ssi 2

根据您的错误,您可能需要向您的类添加一个无参数构造函数:

 public class MyClass {

    public MyClass() {  // no-arg constructor

    }

    //Rest of your class..

 }
Run Code Online (Sandbox Code Playgroud)