2

We have a very old class which doesn't have serialVersionUID.

4 years back someone added serialVersionUID to this class with a random value. He should have used serialver tool to find the value and used it while adding serialVersionUID, but that was not the case.

There are customers using both the versions of this class.

Now if I want to deserialize the old customer's objects is there any way out. I can't change the current serialVersionUID since the new customers will be affected.

Varun
  • 1,014
  • 8
  • 23
  • 1
    Make another class with the same shape and the correct original ID? Refer to the object as Object, and then look to see what you have? – bmargulies Feb 06 '13 at 12:36

1 Answers1

3

You will have to write your own readObject() method that can handle the different UIDs.

"Versioning of Serializable Objects" explains what the UID means and what the rules are.

This answer goes into more details.

I tried to find a good example how to write readObject() for some object that has several UIDs but couldn't find any. There isn't even an example in "Effective Java" by Joshua Bloch. It just says "If you change the UID, you will get an error" but not how to handle this error gracefully.

This question at least comes close: How to deserialize an object persisted in a db now when the object has different serialVersionUID

Community
  • 1
  • 1
Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • Strongly doubt writing your own readObject will work. The 'serialVersionUID' is checked *before* that member function (readObject) is called. The likely reason why you can not find an example is because it does not work this way. Just went through an ordeal were setting the serialVersionUID for 1 class was skipped. When the class implementation is left untouched except for adding readObject/writeObject the members will be called. As soon as the static declaration for the serialVersionUID is added the member functions are not called anymore. – digitaldaemon Jun 01 '19 at 21:03
  • @digitaldaemon Please prove that "'serialVersionUID' is checked before[...]readObject is called", for example with a link to some source code. – Aaron Digulla Jul 09 '19 at 14:27