I have a list of objects serialized into a file:
class MyClass implements Serializable {
public String location;
public SomeClass lastUpdatedAt;
}
Unfortunately, I now have a new version of this class, and we don't have SomeClass anymore; The reason is complex but I need to serialize this back into this new form (this is an example class and not the actual class obviously):
class MyClass implements Serializable {
public String location;
}
In short, I'd like to ignore a property at deserialization.
Please not that I cant change the original class source file. I only have the new source file
However ObjectInputStream fails when it tries to find the SomeClass on the classpath:
try {
FileInputStream fis;
ObjectInputStream is;
File f = getSavedFile();
if (f.exists()) {
fis = new FileInputStream(f);
is = new ObjectInputStream(fis);
ArrayList<MyClass> result = (ArrayList<MyClass>) is.readObject(); //fails
is.close();
fis.close();
}
}
catch (Exception e) {
//ClassNotFound exception because SomeClass is not on the classpath.
e.printStackTrace();
}
I know you can extend ObjectInputStream and implement something there, but I'm not familiar with serialization that much and hope that someone solved the same problem.