renaming fields in serialised objects

R

Roedy Green

If I rename a field in a serialised object, e.g fileName to filename,
is there some sample code somewhere so that I can still read the old
version objects?

I don't know when I read a file whether they have been converted the
new name.


--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
S

Stefan Schulz

is there some sample code somewhere so that I can still read the old
version objects?

I don't know when I read a file whether they have been converted the
new name.

AFAIK this is one of the reasons why serialization should not be used as
persistent storage. There is no easy way to do this as far as i know,
except for almost implementing the serialization protocol again in your
readObject, and changing this one name.
 
J

Joan

Roedy Green said:
If I rename a field in a serialised object, e.g fileName to filename,
is there some sample code somewhere so that I can still read the old
version objects?

I read about this somewhere, but forget the details. Short answer
is that you can. It has to do with the serialver command and the
long serialVersionUID
that it produces.
 
T

Tom Dyess

Joan said:
I read about this somewhere, but forget the details. Short answer
is that you can. It has to do with the serialver command and the
long serialVersionUID
that it produces.

Yes, I was reading up on this as well. AFAIK, the long serialVersionUID is a
unique identifier for the class structure. This is similar to a GUID for a
com object and is used for versioning. If the serialVersionUID is different
from when the obejct was persisted, then the object is no longer compatable
with the data. Conversely, if your serialVersionUID is the same, Java will
attempt to load the persisted object. I'm not sure if the case difference
would render the object unreadable. If you don't explicitely create a
serialVersionUID, Java will create one on the fly based on classname and
structure using reflection.

Here is some more info:

http://www.codecompile.com/CodeCompile/webportal?aid=sr&id=11318
http://www.codecompile.com/CodeCompile/webportal?aid=sr&id=11818
 
R

Roedy Green

I read about this somewhere, but forget the details. Short answer
is that you can. It has to do with the serialver command and the
long serialVersionUID
that it produces.

I discovered to complicate this that not only is each serialisable
base class supposed to have a id version number, but every individual
subclass as well. I guess the idea is you can rescue the base class
info even if you can't rescue the modified subclass.


I guess the way you do it is the way you would in the old cobol days.

You have the old record layout and the new and you write a utility
that does a move corresponding (faked in java with a utility)
to copy the fields across. You write one of these to convert from v1
to v2, v2 to v3 etc. When you open a file you read the first record
that tells the versionlayout of the file, then apply the appropriate
updater before continuing processing. I worked with a scheme like
this in C for educational software so end users could restore old
datafile backups safely.

Some how though you have to have the same class name used for both old
and new code. I guess you implement an interface on both and load
them with separate class loaders??

This is something than needs automation.


--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 
R

Roedy Green

If you don't explicitely create a
serialVersionUID, Java will create one on the fly based on classname and
structure using reflection.

That one is very fragile IIRC. It seems that even adding methods
nothing to do with the data fields changes the generated number.
Every time you compile your serialised objects stop working.

--
Bush crime family lost/embezzled $3 trillion from Pentagon.
Complicit Bush-friendly media keeps mum. Rumsfeld confesses on video.
http://www.infowars.com/articles/us/mckinney_grills_rumsfeld.htm

Canadian Mind Products, Roedy Green.
See http://mindprod.com/iraq.html photos of Bush's war crimes
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top