Skip to content

Commit

Permalink
Add compatible option for VersionFieldSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
tess3ract committed Jan 17, 2015
1 parent 10daf5a commit 907c58b
Showing 1 changed file with 11 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import java.lang.reflect.Field;

import com.esotericsoftware.kryo.Kryo;
import com.esotericsoftware.kryo.KryoException;
import com.esotericsoftware.kryo.io.Input;
import com.esotericsoftware.kryo.io.Output;

Expand All @@ -41,13 +42,19 @@
public class VersionFieldSerializer<T> extends FieldSerializer<T> {
private int[] fieldVersion; // version of each field
private int typeVersion = 0; // version of current type
private boolean compatible = true; // whether current type is compatible with serialized objects in different version

public VersionFieldSerializer (Kryo kryo, Class type) {
super(kryo, type);
// Make sure this is done before any read / write operations.
initializeCachedFields();
}

public VersionFieldSerializer (Kryo kryo, Class type, boolean compatible) {
this(kryo, type);
this.compatible = compatible;
}

@Override
protected void initializeCachedFields () {
CachedField[] fields = getFields();
Expand Down Expand Up @@ -96,6 +103,10 @@ public T read (Kryo kryo, Input input, Class<T> type) {

// Read input version.
int version = input.readVarInt(true);
if (!compatible && version != typeVersion) {
// Reject to read
throw new KryoException("Version not compatible: " + version + " <-> " + typeVersion);
}
CachedField[] fields = getFields();
for (int i = 0, n = fields.length; i < n; i++) {
// Field is not present in input, skip it.
Expand Down

0 comments on commit 907c58b

Please sign in to comment.