I have an application that previously stored some data by serializing the model object (QuickNote
). This was then loaded by deserialization.
However, I am updating the application to store the data by XML instead and I need to be able to import the old, serialized object for conversion. The new object, which does not implement Serializable
, also has the same name though.(QuickNote
).
I am trying to deserialize the old QuickNote
and store it into the new one using the follow code:
FileInputStream fileIn = new FileInputStream("data/quicknotes_user.dat");
ObjectInputStream in = new ObjectInputStream(fileIn);
List<OldQuickNote> newList = (List<OldQuickNote>) in.readObject();
Since the class name for the old serialized file is QuickNote
, I get the error java.io.InvalidClassException: model.QuickNote; class invalid for deserialization
due to the new class having the same name and not implementing Serializable
.
Is there a way to deserialize this QuickNote
object into an OldQuickNote
object? Both QuickNote
and OldQuickNote
are identical except that QuickNote
no longer implements Serializable
but OldQuickNote
does.
I did try to search for answers, but to be honest, I am not even sure how to search for this as I obviously made a poor design decision when electing to serialize the QuickNote
object in the first place.
Caveats:
QuickNote
object cannot be changed to implement Serializable
because it is annotated and used for JAXB marshalling.This was answered in the comments but the one with the answer didn't post it as such.
I ended up moving the old class to a different package and converted the class when the user ran the program again.
All I had to do then was wait for my users to all update the the new version and deleted the old class.
Thank you!
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments