Bored. Let's try this (not compiled)...
import java.io.*;
class A implements Serializable {
private static final long serialVersionUID = 0x...L;
private static final ObjectStreamField[] serialPersistentFields = {
new ObjectStreamField("first", int.class),
new ObjectStreamField("second", int.class),
new ObjectStreamField("secondString", String.class),
};
private int first;
private String second;
private void writeObject(ObjectOutputStream out) throws IOException {
ObjectOutputStream.PutField fields = out.putFields();
fields.put("first", first);
int secondInt;
try {
secondInt = Integer.parseInt(second);
} catch (NumberFormatException exc) {
secondInt = -1;
}
fields.put("second", secondInt);
fields.put("secondString", second);
}
private void readObject(
ObjectInputStream in
) throws IOException, ClassNotFoundException {
ObjectInputStream.GetField fields = out.readFields();
fields = fields.get("first", 0);
int secondInt = fields.get("second", 0);
second = (String)field.get("secondString", Integer.toString(secondInt));
}
}
If either of the fields were final
, then you'd need readResolve
weirdness or worse.
(Almost left the last secondInt
as second
there. Boo hiss to String.valueOf
.)