views:

24

answers:

1

I'm experimenting with the java serialization API to create small persistent objects. In my research I have found a lot on the importance of versioning, so you can detect and handle incompatible changes between the stored object model and the current object model.

What I can't seem to find is what changes to the object model can actually break compatibility. Does removing a primitive member variable break it? Does adding one?

What changes to a Serializable class will cause the readObject/writeObject functions to break down without proper version difference handling?

+1  A: 

If you define serialVersionUID, removing or adding a primitive member doesn't break it (missing primitives get their default values). More info: Java Serialization Specification.

Joonas Pulakka
I actually didn't know this, but it makes sense given that the serialisation bytes generated contains the names of the fields and their values, rather than just the values.
Chris Dennett
Yep; it also means that rearranging fields is ok, and renaming fields is equivalent to remove+add. This works as long as serialVersionUID is kept unchanged.
Joonas Pulakka
So what would break it?
CodeFusionMobile
For example: changing the declared type of a primitive field while keeping the name. See the spec for detials.
Joonas Pulakka