[jdom-interest] TODO serialization [eg]

Dennis Sosnoski dms at sosnoski.com
Tue Apr 17 13:20:09 PDT 2001


Joseph Bowbeer wrote:

> Concerning convenience, writeExternal is given an ObjectOutput interface -
> not an ObjectOutputStream.  (Or can one assume?)  ObjectOutput doesn't
> implement defaultWriteObject.  Nor does it implement writeInt or any of the
> many other convenience methods.

ObjectOutput includes writeObject and extends DataOutput, which defines writeInt
and the rest. defaultWriteObject is a special method that may only be called
from within the writeObject method of the class being serialized, so it doesn't
apply in this case. The whole point of Externalizable is that you control the
form of the serialized output, nothing is written by default.

> > Extending it is no problem - the subclass needs to do it's
> > own work in writeExternal, then call the superclass method,
> > and reverse the process in readExternal.
>
> Isn't implementing writeObject easier for subclasses?
>
> For example, using writeObject, subclasses only interested in short-term
> persistence "will just work" because their non-transient fields will be
> serialized automatically by the defaultWriteObject in our superclass
> writeObject implementation (right?).
>
> If we go the Externalizable route, on the other hand, we're not giving
> subclasses much of an option.
>
> Nor does writeExternal provide the compatibility mechanism that
> defaultWriteObject provides (i.e., the ability to add new non-transient
> fields in the future and have it "just work").

These are the tradeoffs involved; Externalizable allows much more efficient
serialization, but works best for transport purposes, not for long-term
persistence. I agree with the earlier point that long-term persistence is best
done as an XML document; why define a new format when what you've got is just an
internal representation of an external form already?

Actually, though, I think subclasses could be lazy and just implement
Serializable to have everything work automatically; I believe serialization
still uses writeExternal/readExternal when it's handling the superclass.

  - Dennis




More information about the jdom-interest mailing list