Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 751
mcfly soft Avatar asked Apr 15 '14 11:04

mcfly soft


People also ask

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.

Why is KRYO serialized?

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.

Which method is used to serialize an object to open?

For serializing the object, we call the writeObject() method of ObjectOutputStream class, and for deserialization we call the readObject() method of ObjectInputStream class. We must have to implement the Serializable interface for serializing the object.


2 Answers

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

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

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

like image 53
Richard Tingle Avatar answered Sep 23 '22 04:09

Richard Tingle


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();
like image 28
Radim Burget Avatar answered Sep 26 '22 04:09

Radim Burget