Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Serialization and refactoring

Thread view: 
Kenneth P. Turvey - 09 Oct 2006 04:52 GMT
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.

Signature

Kenneth P. Turvey <kt-usenet@squeakydolphin.com>

XMPP: kpturvey@jabber.org

Manish Pandit - 09 Oct 2006 08:59 GMT
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
EJP - 09 Oct 2006 09:52 GMT
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.
Chris Uppal - 09 Oct 2006 11:40 GMT
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.