ahodder
ahodder

Reputation: 11439

How should one maintain serialized objects?

I am creating a Serializable object descriptor that will store important state data for a non- Serailizable entity. How do I keep the old descriptors valid across application upgrades? I was thinking of opening all of the known previous state (on upgrade) and prompt the states to upgrade themselves. Maybe via a:

void onUpgrade(int oldVersion, int newVersion, int updateFlags);

but I don't know if this is a good methodology for serializable maintenance.

Upvotes: 3

Views: 113

Answers (2)

user207421
user207421

Reputation: 311050

The difficulty of object versioning is generally wildly overstated. The great thing is to start out right knowing what you can and cannot do. Have a really good look at the rules in the Object Versioning section of the Object Serialization a specification, and follow them religiously:

  • don't change the inheritance;
  • don't change the datatype of any field;
  • restrict yourself to adding and subtracting fields;
  • and never change the serialVersionUID.

If that doesn't suffice, be prepared to use the writeReplace()/readResolve() mechanism and/or the serializableFields mechanism.

Upvotes: 1

BillRobertson42
BillRobertson42

Reputation: 12883

I would suggest another form for your external representation. Serialized objects are hard to keep compatible as you already noted. The easiest thing to do would be to annotate your object with jaxb annotations and then use Jaxb to read/write it.

Upvotes: 3

Related Questions