compromise thread-safety. So, if we were to address this in Gson, we
should be using ThreadLocal. However, that doesn't really seem all
> 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:
> > 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:
> > > 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:
> > > > 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