。私はKryo v2を使用することをお勧めします。
マップのシリアライザを設定しておき、各キーと値をシリアル化できます。各オブジェクトを直列化するには、Outputクラスを使用してデータを書き込み、Inputクラスを使用してデータを読み込むか、Kryoインスタンスのメソッドを呼び出してオブジェクトを処理させます。
組み込みのMapSerializerを使用する方が簡単です。 Tileオブジェクトのシリアライズをカスタマイズするだけで済みます。 KryoSerializableを拡張したり、シリアライザを登録したりすることができます。ここでは以下の例で...
public class Tile implements KryoSerializable {
int x, y;
Object something;
public void write (Kryo kryo, Output output) {
output.writeInt(x);
output.writeInt(y);
kryo.writeClassAndObject(output, something);
}
public void read (Kryo kryo, Input input) {
x = input.readInt();
y = input.readInt();
something = kryo.readClassAndObject(input);
}
}
ではなくKryoSerializableのシリアライザを使用して、別の例である:
public class Tile {
int x, y;
Object something;
}
kryo.register(Tile.class, new Serializer<Tile>() {
public void write (Kryo kryo, Output output, Tile object) {
output.writeInt(object.x);
output.writeInt(object.y);
kryo.writeClassAndObject(output, object);
}
public Tile read (Kryo kryo, Input input, Class<Tile> type) {
Tile tile = new Tile();
kryo.reference(tile); // Only necessary if Kryo#setReferences is true AND Tile#something could reference this tile.
tile.x = input.readInt();
tile.y = input.readInt();
tile.something = kryo.readClassAndObject(input);
return tile;
}
});
これはKryo#リファレンスへの呼び出しのreadメソッドではやや複雑ですKryoインスタンスを使用して子オブジェクトを逆シリアル化する前にこれは、参照をまったく使用しない場合、または「何か」のオブジェクトが、今作成したばかりのタイルを参照できないことが分かっている場合は省略できます。入力を使ってデータを読み込むだけの場合は、Kryo#参照を呼び出す必要はありません。