Re: Serialization trouble shooting

Posted by dscho on
URL: http://imagej.273.s1.nabble.com/Serialization-trouble-shooting-tp5007683p5007717.html

Hi steve,

On Tue, 13 May 2014, Steven Görlich wrote:

> i really thought, I'd studied serialized objects well, but it seems that I
> was wrong. I spend attention on the fields being serializable as well....
> Curiously, it used to work until a certain point, with almost all the
> fields that are implemented by now.

Sorry, my comment was misleading: I am sure that you studied the details
of the Java serialization well. However, I think that there might be a
slight misconception what Java serialization is intended for.

It is distinctly *not* intended for storage. Java serialization is
substantially too fragile for that: the binary protocol depends on the
implementation details. For example, you simply cannot deserialize Java
objects with Android that were serialized with OpenJDK, because the specs
allow explicitly to override the binary protocol by implementing the
writeObject/readObject methods. When using a class path library that
implements those methods differently (or one of both implementations does
*not* implement them), you're out of luck. A good example is the TreeMap
class if you want to learn more.

What serialization *is* intended for is to be able to page out data e.g.
when RAM becomes scarce, and to be able to page things back in later, when
needed.

To that end, Java serialization defaults to serialize all fields
recursively, no matter whether that is proper or not (it is very easy to
see that an open file handle simply cannot serialized properly).

Your case hits one of these limitations: the Roi class has a reference to
the ImageCanvas which is a *GUI* element. As such, you probably never want
to serialize it because it is bound to an *actual window*. You simply
cannot deserialize such things robustly because the window might not even
exist!

BTW please do not be mislead by the fact that ij.gui.Roi is marked
Serializable, it is not well tested (the ImageCanvas field I mentioned
above clearly would need to be marked as "transient" for example) and it
distinctly does not work, unless you drive it in a very narrow manner.
Even apart from that, it is not intended for storage.

> You mentioned that Rois are saved in a custom way?

Such a question is really asked much better on the mailing list than in a
private mail (you really do not want to treat me as a commodity private
help desk, right?). It is your lucky day that I have enough time to
answer. In any case, to maximize the effect of my answer (you are
certainly not the only one who can benefit from my expertise), I Cc:ed the
mailing list.

When you call File>Save As>Selection..., the real work is done by the
ij.io.RoiEncoder class. You might want to read up on the documentation of
that class:

        http://jenkins.imagej.net/job/ImageJ1-javadoc/javadoc/ij/io/RoiEncoder.html

Ciao,
Johannes

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html