Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Java (http://www.velocityreviews.com/forums/f30-java.html)
-   -   serializing (http://www.velocityreviews.com/forums/t953172-serializing.html)

bob smith 10-08-2012 07:08 PM

serializing
 
Let's say I have this class:

public class Going_to_be_serialized implements Serializable {

public ArrayList<My_Rectangle> rectangles;

public My_Rectangle cur_rect;



So, there are four rectangles in "rectangles".

The "catch" is that cur_rect points to the first rectangle in "rectangles".

So, will there be five rectangles stored when I serialize this? Or will
cur_rect magically point to the first rectangle?

Arne Vajhøj 10-08-2012 07:15 PM

Re: serializing
 
On 10/8/2012 3:08 PM, bob smith wrote:
> Let's say I have this class:
>
> public class Going_to_be_serialized implements Serializable {
>
> public ArrayList<My_Rectangle> rectangles;
>
> public My_Rectangle cur_rect;
>
> So, there are four rectangles in "rectangles".
>
> The "catch" is that cur_rect points to the first rectangle in "rectangles".
>
> So, will there be five rectangles stored when I serialize this? Or will
> cur_rect magically point to the first rectangle?


ObjectOutputStream and ObjectInputStream will get it right.

I would have made cur_rect an index.

And obviously private fields and public getters/settters.

Arne




Lew 10-08-2012 09:58 PM

Re: serializing
 
bob smith wrote:
> Let's say I have this class:
>
> public class Going_to_be_serialized implements Serializable {
> public ArrayList<My_Rectangle> rectangles;
> public My_Rectangle cur_rect;
>
> So, there are four rectangles in "rectangles".
>
> The "catch" is that cur_rect points to the first rectangle in "rectangles".
>
> So, will there be five rectangles stored when I serialize this? Or will
> cur_rect magically point to the first rectangle?


Java's inherent serialization handles cycles in the object graph correctly. It's not very
space-efficient at doing so.

GIYF.

--
Lew

markspace 10-08-2012 11:16 PM

Re: serializing
 
On 10/8/2012 12:15 PM, Arne Vajhøj wrote:

>
> ObjectOutputStream and ObjectInputStream will get it right.



Thanks for pointing that out. I would have guessed that serialization
would not get this right.

Does anyone know what XmlEncoder/Decoder do off hand? I would think the
problem is even harder here, but maybe there's a trick they use to
prevent errors there too.



Daniele Futtorovic 10-08-2012 11:59 PM

Re: serializing
 
On 08/10/2012 23:58, Lew allegedly wrote:
> (Java's inherent serialization handles cycles in the object graph correctly.)


> It's not very space-efficient at doing so.


[citation needed]

--
DF.

Lew 10-09-2012 12:06 AM

Re: serializing
 
markspace wrote:
> Arne Vajh�j wrote:
>> ObjectOutputStream and ObjectInputStream will get it right.

>
> Thanks for pointing that out. I would have guessed that serialization
> would not get this right.


http://docs.oracle.com/javase/7/docs...serialTOC.html

Linked from the java.io API docs.

"The writeObject method (see Section 2.3, "The writeObject Method") serializes the specified object and
traverses its references to other objects in the object graph recursively to create a complete serialized
representation of the graph. Within a stream, the first reference to any object results in the object being
serialized or externalized and the assignment of a handle for that object. Subsequent references to that
object are encoded as the handle. Using object handles preserves sharing and circular references that
occur naturally in object graphs. Subsequent references to an object use only the handle allowing a very
compact representation."

No need for guesswork.

> Does anyone know what XmlEncoder/Decoder do off hand? I would think the
> problem is even harder here, but maybe there's a trick they use to
> prevent errors there too.


The API docs do.

http://docs.oracle.com/javase/7/docs...MLEncoder.html
“XML's standard "id" and "idref" attributes are used to make references to previous expressions -
so as to deal with circularities in the object graph.”

--
Lew

Lew 10-09-2012 12:07 AM

Re: serializing
 
Daniele Futtorovic wrote:
> Lew allegedly wrote:
>> (Java's inherent serialization handles cycles in the object graph correctly.)
>> It's not very space-efficient at doing so.

>
> [citation needed]


Further research indicates that I'm wrong here.

--
Lew

markspace 10-09-2012 02:29 AM

Re: serializing
 
On 10/8/2012 5:06 PM, Lew wrote:

>> Does anyone know what XmlEncoder/Decoder do off hand? I would think the
>> problem is even harder here, but maybe there's a trick they use to
>> prevent errors there too.

>
> The API docs do.
>
> http://docs.oracle.com/javase/7/docs...MLEncoder.html
> “XML's standard "id" and "idref" attributes are used to make references to previous expressions -
> so as to deal with circularities in the object graph.”
>


Nice! Thanks for reading the docs for me (I was being very lazy, I
admit). Good to know too that they found a way deal with circular
references there too.




Roedy Green 10-09-2012 09:09 AM

Re: serializing
 
On Mon, 8 Oct 2012 12:08:08 -0700 (PDT), bob smith
<bob@coolfone.comze.com> wrote, quoted or indirectly quoted someone
who said :

>Let's say I have this class:


It all works no matter what a tangled mess of interconnections you
have. The worst that can happen is you end up serialising a lot of
dependent objects you forgot about.

The serialisation process keeps track of which objects it has already
written to the stream. If it encounters a link to an already written
object, it just outputs a reference to it (I have not looked recently,
but it is probably just a serial number). If it finds a new object,
it serialises it recursively, then outputs the link to it.

I have forgotten if it keeps each object contiguous, by delaying write
or if it embeds.
--
Roedy Green Canadian Mind Products http://mindprod.com
The iPhone 5 is a low end Rolex.



Roedy Green 10-10-2012 12:27 AM

Re: serializing
 
On Mon, 08 Oct 2012 16:16:53 -0700, markspace <-@.> wrote, quoted or
indirectly quoted someone who said :

>
>Does anyone know what XmlEncoder/Decoder do off hand? I would think the
>problem is even harder here, but maybe there's a trick they use to
>prevent errors there too.


It is basically the same thing, except the stream is chars XML instead
of binary. I would expect it to be considerably less compact.
--
Roedy Green Canadian Mind Products http://mindprod.com
The iPhone 5 is a low end Rolex.




All times are GMT. The time now is 09:44 AM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57