2013-01-22 5 views
9

Kryo를 사용하여 List (사용자 정의 된 클래스의 List : List) 목록을 직렬화하려고합니다.kryo list serialization

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

지금까지 아무런 문제없이 목록을 오류없이 작성합니다. 하지만 그것을 읽으려고 할 때 :

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

내가이 오류 :

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

는이 문제를 어떻게 해결할 수 있습니까?

+0

이 문제를 해결 했습니까? – expert

답변

3

당신의 오류에 따르면, 당신은 당신의 클래스에 인수 없음의 생성자를 추가 할 수 있습니다 : List는 인터페이스이기 때문에, 다시 객체를 읽을 때

public class MyClass { 

    public MyClass() { // no-arg constructor 

    } 

    //Rest of your class.. 

} 
+0

답변 해 주셔서 감사합니다. 그러나 MyClass에는 이미 하나의 String 인수를 취하는 생성자가 있습니다. 어떤 아이디어? – MAZDAK

+0

zero arg 생성자를 추가하거나 객체를 생성하기 위해 자신의 시리얼 라이저를 작성하십시오. FieldSerializer를 확장하고 create를 재정의 할 수 있습니다. – NateS

5

당신은 List.class을 사용할 수 없습니다.

k2.readObject(listRead, ArrayList.class); 
+0

네 말이 맞아. 그리고 우리는 던질 필요가 없습니다. 즉 :'List data = kryo.readObject (input, ArrayList.class);' –