Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
State information during serialization/deserialization?
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  5 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
JoelPM  
View profile  
 More options Jan 8 2009, 2:58 pm
From: JoelPM <joel.me...@gmail.com>
Date: Thu, 8 Jan 2009 11:58:41 -0800 (PST)
Local: Thurs, Jan 8 2009 2:58 pm
Subject: State information during serialization/deserialization?
Is there a way to pass state information along to a custom serializer?
For example, given the following hierarchy:

Artist -> Albums -> Tracks

Represented in classes like this:

class Artist {
  protected List<Albums> albums;
  //...

}

class Album {
  protected List<Track> tracks;
  //...
}

class Track {
  protected String name;
  //...

}

I'd like to be able to set a value in the SerializationContext (or
some other state object) that captures the current artist/album as the
nodes in the object graph are traversed so that when serializing the
Track I can get the name of the artist and album. I'd also like to be
able to explicitly set these values so that if I need to serialize
just a list of tracks I can do something like:

context.set("album", album);
context.set("artist", artist);
Gson.toJson(listOfTracks, context);

And then in my TrackSerializer I could do:

class TrackSerializer implements JsonSerializer<Track> {
  @Override
  public JsonElement serialize(Track track, Type typeOfSrc,
JsonSerializationContext context) {
    Artist artist = (Artist)context.get("artist");
    Album album  (Album)context.get("album");
    // ... do serialization ...
  }

}

Unfortunately the JsonSerializationContext doesn't support this but
maybe there's another way to accomplish the same thing? (And the
object structure I'm serializing is Thrift based so I can't add parent
pointers.)

Thanks,
JoelPM


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
inder123@gmail.com  
View profile  
 More options Jan 9 2009, 11:23 pm
From: "inder...@gmail.com" <inder...@gmail.com>
Date: Fri, 9 Jan 2009 20:23:56 -0800 (PST)
Local: Fri, Jan 9 2009 11:23 pm
Subject: Re: State information during serialization/deserialization?
This is a good idea, I will file a feature request in Gson on the
topic.

Meanwhile, you can store information in a ThreadLocal (thread-safe) or
a static (not so thread-safe).

Also, if your JsonSerializer is a inner class (not private nested
class) then you can use the fields of the parent type to store
information as well. However, that approach is fraught with thread-
safety issues. But sometimes that is not a concern.

Inder

On Jan 8, 11:58 am, JoelPM <joel.me...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
inder123@gmail.com  
View profile  
 More options Jan 9 2009, 11:25 pm
From: "inder...@gmail.com" <inder...@gmail.com>
Date: Fri, 9 Jan 2009 20:25:28 -0800 (PST)
Local: Fri, Jan 9 2009 11:25 pm
Subject: Re: State information during serialization/deserialization?
I filed a feature request at: http://code.google.com/p/google-gson/issues/detail?id=92

On Jan 9, 8:23 pm, "inder...@gmail.com" <inder...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
JoelPM  
View profile  
 More options Jan 11 2009, 7:05 pm
From: JoelPM <joel.me...@gmail.com>
Date: Sun, 11 Jan 2009 16:05:31 -0800 (PST)
Local: Sun, Jan 11 2009 7:05 pm
Subject: Re: State information during serialization/deserialization?
Thanks Inder, using a ThreadLocal is a good idea, I may switch to
that. Prior to seeing your reply I had gone ahead and created wrapper
objects that captured the state and registered custom serializers for
the wrapper types. However, it's not as elegant as using a ThreadLocal
(maybe with a static util to access it: SerializationContextUtil.set
("key", value) and SerializationContextUtil.get("key")).

Also, thanks for filing a feature request.

Joel

On Jan 9, 8:25 pm, "inder...@gmail.com" <inder...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
inder  
View profile  
 More options Mar 12 2009, 5:00 pm
From: inder <inder...@gmail.com>
Date: Thu, 12 Mar 2009 14:00:18 -0700 (PDT)
Local: Thurs, Mar 12 2009 5:00 pm
Subject: Re: State information during serialization/deserialization?
We resolved this issue by deciding not to add this feature in Gson.
The reason is that storing state in a shared map with-in Gson will
compromise thread-safety. So, if we were to address this in Gson, we
should be using ThreadLocal. However, that doesn't really seem all
that more convenient than the clients using ThreadLocal directly.

Inder

On Jan 11, 5:05 pm, JoelPM <joel.me...@gmail.com> wrote:


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »