2

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?

casperOne
  • 73,706
  • 19
  • 184
  • 253

3 Answers3

1
  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

Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327
1

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

user207421
  • 305,947
  • 44
  • 307
  • 483
0

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.

Community
  • 1
  • 1
Kevin Day
  • 16,067
  • 8
  • 44
  • 68