XStream does not preserve static information

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

XStream does not preserve static information

Joseph Campolongo
I've noticed that XStream doesn't preserve static information.

For example, the class

public class MyClass {
  private static int counter = 0;
 
  public int getCount() {
    return counter++;
  }
}

is serialized as

<MyClass/>

This is fine, except when I unmarshell the XML inside a different
classloader.  At this point, my counter gets reset to zero, instead of
wherever it was when I serialized my class out.

Joseph Campolongo
Reply | Threaded
Open this post in threaded view
|

Re: XStream does not preserve static information

Jörg Schaible-2
Joseph Campolongo wrote:

> I've noticed that XStream doesn't preserve static information.
>
> For example, the class
>
> public class MyClass {
>   private static int counter = 0;
>  
>   public int getCount() {
>     return counter++;
>   }
> }
>
> is serialized as
>
> <MyClass/>
>
> This is fine, except when I unmarshell the XML inside a different
> classloader.  At this point, my counter gets reset to zero, instead of
> wherever it was when I serialized my class out.

This is how serialization works. No serialization library will set the
static member, since it is not serialized.

- Jörg

Reply | Threaded
Open this post in threaded view
|

RE: Re: XStream does not preserve static information

Akshay Shrivastava
I guess the reason why that makes sense is because static is not really a
part of the object instance you just serialized... its part of the object's
Class.

Is there a way to add a feature to serialize the static content of a Class
using XStream?  Just tag the data as static (in the xml) and serialize it
only once for all object-instances of a given class... and obviously while
deserializing it, load it only once and set the values back in the Class
once, instead of setting for each object instance.

That could be a cool extra.


>From: Jörg Schaible <[hidden email]>
>Reply-To: [hidden email]
>To: [hidden email]
>Subject: [xstream-user]  Re: XStream does not preserve static information
>Date: Thu, 05 Jan 2006 17:43:37 +0100
>
>Joseph Campolongo wrote:
>
> > I've noticed that XStream doesn't preserve static information.
> >
> > For example, the class
> >
> > public class MyClass {
> >   private static int counter = 0;
> >
> >   public int getCount() {
> >     return counter++;
> >   }
> > }
> >
> > is serialized as
> >
> > <MyClass/>
> >
> > This is fine, except when I unmarshell the XML inside a different
> > classloader.  At this point, my counter gets reset to zero, instead of
> > wherever it was when I serialized my class out.
>
>This is how serialization works. No serialization library will set the
>static member, since it is not serialized.
>
>- Jörg
>

_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar – get it now!
http://toolbar.msn.click-url.com/go/onm00200415ave/direct/01/

Reply | Threaded
Open this post in threaded view
|

RE: Re: XStream does not preserve static information

Jörg Schaible-2
Akshay Shrivastava wrote:

> I guess the reason why that makes sense is because static is not really a
> part of the object instance you just serialized... its part of the
> object's Class.

And you do not marshall the class object and this object might just exist if
you unmarshal.
 
> Is there a way to add a feature to serialize the static content of a Class
> using XStream?  Just tag the data as static (in the xml) and serialize it
> only once for all object-instances of a given class... and obviously while
> deserializing it, load it only once and set the values back in the Class
> once, instead of setting for each object instance.

Since it is always the same object, it would be serialized only once anyway.
But you have no valid place to write it into the XML stream.

> That could be a cool extra.

I guess it would be more of a hassle than a good feature. You cannot just
turn on a mode where XStream writes all statics of a class, you would have
to register explicitly, which static of which class you want to marshall.

- Jörg


Reply | Threaded
Open this post in threaded view
|

Re: RE: Re: XStream does not preserve static information

Grégory Joseph
> I guess it would be more of a hassle than a good feature. You cannot just
> turn on a mode where XStream writes all statics of a class, you would have
> to register explicitly, which static of which class you want to marshall.

A custom converter for the specific class might be a good option
though, I suppose.

Just my 2c.

g