I'm creating a small app using JavaFX and I have this kind of class organization.
public class Context implements Serializable {
private static final long serialVersionUID = 12355322553L;
private A objA;
private List<A> aList;
...
}
public class A implements Serializable {
private static final long serialVersionUID = 467788765432L;
private B objB,
private List<B> bList;
...
}
for backup purposes, I use serialization.
My question is: how to handle deserialization when you have modified the classes for example adding a new field.
When adding a new field like so :
public class Context implements Serializable{
private static final long serialVersionUID = 12355322553L;
private A objA;
private List<A> aList;
private String name;
...
}
public class A implements Serializable{
private static final long serialVersionUID = 467788765432L;
private B objB,
private List<B> bList;
...
}
when trying to deserialize my old data with my new model I got the following error
java.io.EOFException
at java.io.ObjectInputStream$BlockDataInputStream.peekByte(ObjectInputStream.java:3016)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1575)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2343)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2267)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2125)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1624)
at java.io.ObjectInputStream.defaultReadFields(ObjectInputStream.java:2343)
at java.io.ObjectInputStream.readSerialData(ObjectInputStream.java:2267)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:2125)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1624)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:464)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:422)
the 1st thing you have to remember is the fact that the
is not a just a random decorator for the class, but instead is used by the VM to serialize and deserialize objects, with this ID, the VM has a reference of the class involved in the process including fields and even methods, i.e. if you change the class in any way by adding,removing fields and or methods then the VM is not going to be able to deserialize objects anymore because is considered as a totally different class