I want to know in which order the attributes of the following example class would be serialized:
public class Example implements Serializable {
private static final long serialVersionUID = 8845294179690379902L;
public int score;
public String name;
public Date eventDate;
}
EDIT:
Why i wanna know this:
I got a serialized string in a file for a class of mine which has no implementation for readObject() or writeObject(). now that implementation changed ( some properties are gone ) and i want to write a readObject() method that handles the old serialized class.
There i would just read this property but wouldnt save it to the created object.
This is basically just for legacy i use a database now but need to support the old serialized files.
to write this readObject() i need the order of properties that in the stream.
Based on a brief reading of the spec.
The fields are written in the order of the field descriptors the class descriptor
The field descriptors are in "canonical order" which is defined as follows:
"The descriptors for primitive typed fields are written first sorted by field name followed by descriptors for the object typed fields sorted by field name. The names are sorted using String.compareTo."
(I suspect that the bit about the canonical order should not matter. The actual order of the fields in the serialization should be recoverable from the actual order of the field descriptors in the class descriptor in the same serialization. I suspect that the reason a canonical order is specified is that it affects the computed serialization id. But I could easily be wrong about this :-) )
Reference: