Java 如何使用 Kryo 序列化对象并再次反序列化?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23082079/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 20:34:09  来源:igfitidea点击:

How can I use Kryo to serialize an object and deserialize it again?

javaserializationkryo

提问by mcfly soft

How can I use Kryoto serialize an object and deserialize it again? I am working in Kryo 2.23.0

如何使用Kryo序列化对象并再次反序列化它?我在工作Kryo 2.23.0

采纳答案by Richard Tingle

Kryo's syntax is relatively similar to java serialisation. A kryo object is created as well as an output/input and one of kryos methods is used to perform the serialisation/deserialisation

Kryo 的语法与 java 序列化比较相似。创建 kryo 对象以及输出/输入,并且使用 kryos 方法之一来执行序列化/反序列化

  • kryo.writeClassAndObject(output, object); //for if the concrete class isn't known (can be null)
  • kryo.writeObjectOrNull(output, someObject); //if the object could be null
  • kryo.writeObject(output, someObject); //can't be null and concrete class is known
  • kryo.writeClassAndObject(output, object); //for if the concrete class isn't known (can be null)
  • kryo.writeObjectOrNull(output, someObject); //if the object could be null
  • kryo.writeObject(output, someObject); //can't be null and concrete class is known

Each of the writes is paired with a read

每个写入都与一个读取配对

  • SomeClass object = (SomeClass)kryo.readClassAndObject(input);
  • SomeClass someObject = kryo.readObjectOrNull(input, SomeClass.class);
  • SomeClass someObject = kryo.readObject(input, SomeClass.class);
  • SomeClass object = (SomeClass)kryo.readClassAndObject(input);
  • SomeClass someObject = kryo.readObjectOrNull(input, SomeClass.class);
  • SomeClass someObject = kryo.readObject(input, SomeClass.class);

The following is an example using writeClassAndObject that serialises a Vector3d to a file and back again.

以下是使用 writeClassAndObject 将 Vector3d 序列化到文件并再次返回的示例。

public class KryoTest {
    public static void main(String[] args){

        Vector3d someObject=new Vector3d(1,2,3);

        //serialise object

        //try-with-resources used to autoclose resources
        try (Output output = new Output(new FileOutputStream("KryoTest.ser"))) {
            Kryo kryo=new Kryo();
            kryo.writeClassAndObject(output, someObject);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(KryTest.class.getName()).log(Level.SEVERE, null, ex);
        }

        //deserialise object

        Vector3d retrievedObject=null;

        try (Input input = new Input( new FileInputStream("KryoTest.ser"))){
            Kryo kryo=new Kryo();
            retrievedObject=(Vector3d)kryo.readClassAndObject(input);
        } catch (FileNotFoundException ex) {
            Logger.getLogger(KryTest.class.getName()).log(Level.SEVERE, null, ex);
        }

        System.out.println("Retrieved from file: " + retrievedObject.toString());
    }
}

All the up to date documentation has moved to github now; https://github.com/EsotericSoftware/kryo#quickstart

所有最新的文档现在都移到了 github;https://github.com/EsotericSoftware/kryo#quickstart

回答by Radim Burget

A simple version:

一个简单的版本:

Kryo kryo = new Kryo();
// #### Store to disk...
Output output = new Output(new FileOutputStream("file.bin"));
SomeClass someObject = ...
kryo.writeObject(output, someObject);
output.close();
// ### Restore from disk...
Input input = new Input(new FileInputStream("file.bin"));
SomeClass someObject = kryo.readObject(input, SomeClass.class);
input.close();