
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.
Kryo can also perform automatic deep and shallow copying/cloning. This is direct copying from object to object, not object to bytes to object.
This documentation is for Kryo version 5.x. See the Wiki for version 4.x.
Contact / Mailing list
Please use the Kryo mailing list for questions, discussions, and support. Please limit use of the Kryo issue tracker to bugs and enhancements, not questions, discussions, or support.
Sponsors
Kryo maintenance and development is sponsored by the Gecko fund, which is powered by EXANTE.
Table of contents
Recent releases
- 5.7.1 - fixes the registration order of the serializers for unmodifiable and synchronized collections and adds serializers for the navigable and sequenced wrappers.
- 5.7.0 - brings safer deserialization of corrupt data, serializers for unmodifiable and synchronized collections, and bug fixes.
- 4.0.3 - brings bug fixes and performance improvements for chunked encoding.
- 5.6.2 - recompiles 5.6.1 to be compatible with Java 8 again
- 5.6.1 - brings a bug fix for the Maven coordinates of the versioned artifact
- 5.6.0 - brings bug fixes and performance improvements.
- 5.5.0 - brings bug fixes and performance improvements.
Installation
Kryo requires Java 8 or later.
Kryo publishes two kinds of artifacts/jars:
- the default jar (with the usual library dependencies) which is meant for direct usage in applications (not libraries)
- a dependency-free, "versioned" jar which should be used by other libraries. Different libraries shall be able to use different major versions of Kryo.
The two jars differ as follows:
|
Default jar |
Versioned jar |
| Maven coordinates |
com.esotericsoftware:kryo |
com.esotericsoftware.kryo:kryo5 |
| Package |
com.esotericsoftware.kryo |
com.esotericsoftware.kryo.kryo5 |
| Dependencies |
ReflectASM, Objenesis, MinLog |
None (bundled and relocated into com.esotericsoftware.kryo.kryo5) |
| Java module name |
com.esotericsoftware.kryo |
com.esotericsoftware.kryo.kryo5 |
| OSGi bundle symbolic name |
com.esotericsoftware.kryo |
com.esotericsoftware.kryo.5 |
Both jars are OSGi bundles and declare an automatic module name, so they can be used on the Java module path. When using the versioned jar, all Kryo classes must be imported from the relocated package, for example com.esotericsoftware.kryo.kryo5.Kryo.
Kryo JARs are available on the releases page and at Maven Central. The latest snapshots of Kryo, including snapshot builds of master, are in the Maven Central snapshot repository.
With Maven
To use the latest Kryo release in your application, use this dependency entry in your pom.xml:
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>5.7.1</version>
</dependency>
To use the latest Kryo release in a library you want to publish, use this dependency entry in your pom.xml:
<dependency>
<groupId>com.esotericsoftware.kryo</groupId>
<artifactId>kryo5</artifactId>
<version>5.7.1</version>
</dependency>
To use the latest Kryo snapshot, use:
<repository>
<id>central-snapshots</id>
<name>Maven Central snapshots repo</name>
<url>https://central.sonatype.com/repository/maven-snapshots/</url>
</repository>
<!-- for usage in an application: -->
<dependency>
<groupId>com.esotericsoftware</groupId>
<artifactId>kryo</artifactId>
<version>5.8.0-SNAPSHOT</version>
</dependency>
<!-- for usage in a library that should be published: -->
<dependency>
<groupId>com.esotericsoftware.kryo</groupId>
<artifactId>kryo5</artifactId>
<version>5.8.0-SNAPSHOT</version>
</dependency>
Without Maven
Not everyone is a Maven fan. Using Kryo without Maven requires placing the Kryo JAR on your classpath along with the dependency JARs found in lib.
Building from source
Building Kryo from source requires JDK11+ and Maven. To build all artifacts, run:
Quickstart
Jumping ahead to show how the library can be used:
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import java.io.*;
public class HelloKryo {
static public void main (String[] args) throws Exception {
Kryo kryo = new Kryo();
kryo.register(SomeClass.class);
SomeClass object = new SomeClass();
object.value = "Hello Kryo!";
Output output = new Output(new FileOutputStream("file.bin"));
kryo.writeObject(output, object);
output.close();
Input input = new Input(new FileInputStream("file.bin"));
SomeClass object2 = kryo.readObject(input, SomeClass.class);
input.close();
}
static public class SomeClass {
String value;
}
}
The Kryo class performs the serialization automatically. The Output and Input classes handle buffering bytes and optionally flushing to a stream.
The rest of this document details how this works and advanced usage of the library.
IO
Getting data in and out of Kryo is done using the Input and Output classes. These classes are not thread safe.
Output