Java – deserialization version conflict
•
Java
My question is, what is a good way to upgrade obsolete objects?
For example, suppose you have a class like this:
public class Foo() implements Serializable { int a; String b; }
Then you serialize the object You can then add more features to your course by adding the following:
... int c; ...
You still want to keep the information in the serialized object, but I know there will be a conflict if you try to deserialize it Is there any way (perhaps by checking serialVersionUID) to set a value for int c that knows it does not exist?
thank you
Solution
You need to add a private method:
private void readObject(ObjectInputStream ois) { // compare serialVersionUID and determine which fields to expect // read the expected fields for that version // initialize the new fields. }
For more information, read serialization specs
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码