kryo list serialization

10,209

Solution 1

You can't use List.class when read objects back, since List is an interface.

k2.readObject(listRead,  ArrayList.class);

Solution 2

According to your error, you might want to add a no-arg constructor to your class:

 public class MyClass {

    public MyClass() {  // no-arg constructor

    }

    //Rest of your class..

 }
Share:
10,209
MAZDAK
Author by

MAZDAK

Updated on August 05, 2022

Comments

  • MAZDAK
    MAZDAK over 1 year

    I am trying to serialize a List of List of some objects (of a customized class: List> ), using Kryo.

    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();
    

    So far no problem, it writes out the list with no errors. But when I try to read it:

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

    I get this error:

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

    How can I solve this problem?

  • MAZDAK
    MAZDAK over 11 years
    Thanks for your answer. But MyClass already has a constructor which takes one String argument. Any ideas?
  • NateS
    NateS over 10 years
    Add zero arg constructor or write your own serializer to create the object. You can extend FieldSerializer and override create.
  • Rod Lima
    Rod Lima over 8 years
    You're right. And we don't need to cast. Ie: List<Fabri> data = kryo.readObject(input, ArrayList.class);