Skip to content

Instantly share code, notes, and snippets.

@elucash
Last active June 20, 2016 00:11
Show Gist options
  • Save elucash/e79dd75dfeaa7f896ee2 to your computer and use it in GitHub Desktop.
Save elucash/e79dd75dfeaa7f896ee2 to your computer and use it in GitHub Desktop.
Using Immutables with cryo
package org.immutables.samples.json.immutables;
import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;
import com.esotericsoftware.kryo.serializers.JavaSerializer;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.Serializable;
import org.immutables.value.Value;
@Value.Immutable(singleton = true)
public interface Ser extends Serializable {}
class UseKryo {
public static void main(String... args) throws IOException {
ImmutableSer ser = ImmutableSer.of();
Kryo kryo = new Kryo();
// this is optional if you want to preserve singleton
kryo.register(ImmutableSer.class, new JavaSerializer());
Output output = new Output(new FileOutputStream("file.bin"));
kryo.writeObject(output, ser);
output.close();
Input input = new Input(new FileInputStream("file.bin"));
ImmutableSer sss = kryo.readObject(input, ImmutableSer.class);
input.close();
System.out.println(sss);
System.out.println(sss == ser); // should be true if JavaSerializer is used (readResolve magic generated by Immutables)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment