Java Serialization Versioning with Externalizable
Uncategorized - 1 Comment » - Posted on October, 22 at 1:39 am
Java Serialization is a easy way to persist the state of Java Objects. However if the structure of the class changes the serialized bytes is invalidated by the changes. This article suggest how one could implement serialization with Externalizable with support for versions.
Consider a simple java class
-
private int field1;
-
private int field2;
-
}
If the structure of the class changes, then the serialized bytes becomes incompatible and you would get the dreaded ClassCastException
-
//private int field1; removed
-
private int field2;
-
private int field3; //added
-
}
A good way to get complete control of the serialization and support version is by implementing Externalizable instead of Serializable.
Externalizable gives the programmer full control over how the class is serialized and de-serialized. It is common practice to add a version variable to support representations of the same class over time.
Consider first version of the same class
-
private int version=1;
-
private int field1;
-
private int field2;
-
@Override
-
version=in.readInt();
-
if(version==1) {
-
field1=in.readInt();
-
field2=in.readInt();
-
} else {
-
}
-
}
-
@Override
-
out.writeInt(version);
-
out.writeInt(field1);
-
out.writeInt(field2);
-
}
-
}
Now if the next version of the class needs to add a String and remove field2 the class would look like
-
private int version=2;
-
private int field1;
-
//private int field2; removed
-
private String version2String;
-
@Override
-
version=in.readInt();
-
if(version==1) {
-
field1=in.readInt();
-
in.readInt();// ignore
-
version2String=“”;// initialize to default
-
} else if (version==2) {
-
field1=in.readInt();
-
} else {
-
}
-
}
-
@Override
-
out.writeInt(version);
-
out.writeInt(field1);
-
out.writeObject(version2String);
-
}
-
}
Posted in Uncategorized | 1 Comment »
In the second version, the version number is still 1 which may be confusing.