user886910
user886910

Reputation:

Serialization, evolution of a class

I need to serial an object and store it on disk. I used Java's built in and that works fine, as long as the class doesn't change to much. If I start mucking around with the class it can stop working.

What options are available here? Basically if we have an update I don't want to break all the user's data files.

So far I've tried serialization to XML (has same problems). Also tried to 'hand roll' a config/data file. Basically spit everything out into XML, load it and then create a NEW object based off the config file. This seems to work well, but would take forever to convert everything over to this since it's a lot of manual work.

Any other options?

Upvotes: 2

Views: 762

Answers (3)

Kevin Day
Kevin Day

Reputation: 16383

I am a big believer (having learned the hard way) of having persistent proxies for my serialized objects. Effective Java has a whole chapter that talks about ways of implementing them.

The way I do it is to use an inner class that takes my object in it's constructor and sets it's internal fields accordingly. As your real object evolves, you either add new fields to the proxy, or create a completely new proxy (leaving the old one in place for backwards compatibility).

See this article for more info.

Upvotes: 0

user207421
user207421

Reputation: 310916

First you need to read thoroughly the Versioning section of the Object Serialization Specification.

Upvotes: 1

Aravind Yarram
Aravind Yarram

Reputation: 80176

  1. protobuf (Very good support for forward and backward compatibility but you have to write your own mapping code)
  2. MessagePack
  3. Apache Avro
  4. Kryo

And many more with comparisons here

Upvotes: 1

Related Questions