Serialization and refactoring

  • Thread starter Kenneth P. Turvey
  • Start date
K

Kenneth P. Turvey

I've got some code that stores its data to a file using object
serialization. I've refactored the code by extracting a superclass and
creating a new subclass. The details of all this are really unimportant,
but the point is that the new class hierarchy is different from the old
one. Some fields have been moved to a super class that didn't previously
exist.

Now I've got a data file produced using the old code and I want to read it
in to the new program.

Is there a way to handle backward-compatibility using serialization? I
don't mind parsing the file by hand, but I can't seem to find any details
on how to do that.

I should note, this is not a question about serialVersionID. My new class
structure _is not_ compatible with the old one. I just need to be able to
get to the data.

Should I look for another way to handle persistence?

Thanks.
 
M

Manish Pandit

Not sure if you've already considered this solution - keep your old
hierarchy intact for the sake of reading these files. Create a utility
method that still reads the objects in the old format, and reconstructs
object(s) based on the new hierarchy. You can then store the new
object(s) in a new file. Once you've transformed all old ones, you can
get rid of the old class from your source tree.

If your application is prone to such major changes in future, consider
using externalization instead of serialization, or better yet,
something like hibernate.

-cheers,
Manish
 
E

EJP

There are several options within the versioning provisions of
Serialization. You can declare your own serializable fields, you can use
writeReplace() & readResolve(), you can read and write optional data
inside readObject() and writeObject(). See the Serialization Specification.
 
C

Chris Uppal

Kenneth said:
Is there a way to handle backward-compatibility using serialization? I
don't mind parsing the file by hand, but I can't seem to find any details
on how to do that.

The serialisation spec will tell you that,

http://java.sun.com/j2se/1.5.0/docs/guide/serialization/index.html

But I think Manish's suggestion makes a lot more sense unless you can find an
off-the-shelf parser for the serialisation format (I don't know of one myself).

-- chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top