How to deserialize object with different serialVersionUID

970 Views Asked by At

I'm aware that serialVersionUID is used for versioning a serializable class, so, how can I load a old serialized object with the new class?

The only related question I found is: how to discover serialVersionUID of serialized objects

But still, how I would be able to deserialize the old object as an instance of the new updated class?

1

There are 1 best solutions below

0
On

You can set the serialVersionUID to any value needed (the expected vs actual values are normally seen in the error messages). Put the field like

   private static final long serialVersionUID = 1L;

(replace 1 by the old value) and you will be able to read your old data.

If you need to read both new and old format at the same time and they are actually somewhat different, best seems to give the new version of the class a new name use resolveObject to convert the old format into new if the leagacy class appears in the stream.