Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kryo list serialization

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?

like image 314
MAZDAK Avatar asked Jan 22 '13 12:01

MAZDAK


People also ask

What is Kryo serialization?

Kryo is a fast and efficient binary object graph serialization framework for Java. The goals of the project are high speed, low size, and an easy to use API. The project is useful any time objects need to be persisted, whether to a file, database, or over the network.

Why is serialization faster than KRYO serialization?

Kryo is significantly faster and more compact than Java serialization (often as much as 10x), but does not support all Serializable types and requires you to register the classes you'll use in the program in advance for best performance. So it is not used by default because: Not every java. io.

Is Kryo thread safe?

The Kryo instance is not thread safe, and quite expensive to build, so storing it on a ThreadLocal is a recommended way to make sure that the ProductKryoSerializer is thread safe.


1 Answers

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

k2.readObject(listRead,  ArrayList.class);
like image 96
xiaowl Avatar answered Sep 29 '22 14:09

xiaowl