Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.programmer > #19188 > unrolled thread

serializing

Started bybob smith <bob@coolfone.comze.com>
First post2012-10-08 12:08 -0700
Last post2012-10-09 02:09 -0700
Articles 12 — 6 participants

Back to article view | Back to comp.lang.java.programmer


Contents

  serializing bob smith <bob@coolfone.comze.com> - 2012-10-08 12:08 -0700
    Re: serializing Arne Vajhøj <arne@vajhoej.dk> - 2012-10-08 15:15 -0400
      Re: serializing markspace <-@.> - 2012-10-08 16:16 -0700
        Re: serializing Lew <lewbloch@gmail.com> - 2012-10-08 17:06 -0700
          Re: serializing markspace <-@.> - 2012-10-08 19:29 -0700
        Re: serializing Roedy Green <see_website@mindprod.com.invalid> - 2012-10-09 17:27 -0700
          Re: serializing Arne Vajhøj <arne@vajhoej.dk> - 2012-10-09 21:01 -0400
        Re: serializing Arne Vajhøj <arne@vajhoej.dk> - 2012-10-09 21:00 -0400
    Re: serializing Lew <lewbloch@gmail.com> - 2012-10-08 14:58 -0700
      Re: serializing Daniele Futtorovic <da.futt.news@laposte-dot-net.invalid> - 2012-10-09 01:59 +0200
        Re: serializing Lew <lewbloch@gmail.com> - 2012-10-08 17:07 -0700
    Re: serializing Roedy Green <see_website@mindprod.com.invalid> - 2012-10-09 02:09 -0700

#19188 — serializing

Frombob smith <bob@coolfone.comze.com>
Date2012-10-08 12:08 -0700
Subjectserializing
Message-ID<fe978849-c763-4bfb-a8b1-58ffd7203a24@googlegroups.com>
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?

[toc] | [next] | [standalone]


#19189

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-10-08 15:15 -0400
Message-ID<50732644$0$293$14726298@news.sunsite.dk>
In reply to#19188
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


[toc] | [prev] | [next] | [standalone]


#19195

Frommarkspace <-@.>
Date2012-10-08 16:16 -0700
Message-ID<k4vmt5$7v2$1@dont-email.me>
In reply to#19189
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.

[toc] | [prev] | [next] | [standalone]


#19197

FromLew <lewbloch@gmail.com>
Date2012-10-08 17:06 -0700
Message-ID<fa6242a2-6ee8-4d49-9f58-a390beb35986@googlegroups.com>
In reply to#19195
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/platform/serialization/spec/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/api/java/beans/XMLEncoder.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

[toc] | [prev] | [next] | [standalone]


#19199

Frommarkspace <-@.>
Date2012-10-08 19:29 -0700
Message-ID<k5025t$sor$1@dont-email.me>
In reply to#19197
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/api/java/beans/XMLEncoder.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.


[toc] | [prev] | [next] | [standalone]


#19215

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-10-09 17:27 -0700
Message-ID<u6g978937pi6p4p0ditmcjp5jdn77485ef@4ax.com>
In reply to#19195
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. 

[toc] | [prev] | [next] | [standalone]


#19217

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-10-09 21:01 -0400
Message-ID<5074c8d2$0$282$14726298@news.sunsite.dk>
In reply to#19215
On 10/9/2012 8:27 PM, Roedy Green wrote:
> 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.

Real serialization and XmlEncode/XmlDecode is very different
in what they do and how they do it.

Arne

[toc] | [prev] | [next] | [standalone]


#19216

FromArne Vajhøj <arne@vajhoej.dk>
Date2012-10-09 21:00 -0400
Message-ID<5074c89c$0$282$14726298@news.sunsite.dk>
In reply to#19195
On 10/8/2012 7:16 PM, markspace wrote:
> 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?

It gets it right also.

For the same reason: when you serialize something and then
deserialize you expect to get the same as before.

Let us say that you have a class that contains two
HashMap and basically maps bidirectional between
two sets of data. If serialization + deserialization
resulted in having all data objects being duplicated, then
things could real messy.

Arne


[toc] | [prev] | [next] | [standalone]


#19194

FromLew <lewbloch@gmail.com>
Date2012-10-08 14:58 -0700
Message-ID<5023e882-8a0b-4429-8062-096d058081a7@googlegroups.com>
In reply to#19188
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

[toc] | [prev] | [next] | [standalone]


#19196

FromDaniele Futtorovic <da.futt.news@laposte-dot-net.invalid>
Date2012-10-09 01:59 +0200
Message-ID<k4vphs$kf1$1@dont-email.me>
In reply to#19194
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.

[toc] | [prev] | [next] | [standalone]


#19198

FromLew <lewbloch@gmail.com>
Date2012-10-08 17:07 -0700
Message-ID<3ab468c3-35d3-465c-a361-39c51762a9bc@googlegroups.com>
In reply to#19196
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

[toc] | [prev] | [next] | [standalone]


#19202

FromRoedy Green <see_website@mindprod.com.invalid>
Date2012-10-09 02:09 -0700
Message-ID<75q778582j376rtmvq5u3a77a2e4u942tn@4ax.com>
In reply to#19188
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. 

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.java.programmer


csiph-web