State information during serialization/deserialization?

3,118 views
Skip to first unread message

JoelPM

unread,
Jan 8, 2009, 2:58:41 PM1/8/09
to google-gson
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

inde...@gmail.com

unread,
Jan 9, 2009, 11:23:56 PM1/9/09
to google-gson
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

inde...@gmail.com

unread,
Jan 9, 2009, 11:25:28 PM1/9/09
to google-gson

JoelPM

unread,
Jan 11, 2009, 7:05:31 PM1/11/09
to google-gson
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

inder

unread,
Mar 12, 2009, 5:00:18 PM3/12/09
to google-gson
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

Zuned Ahmed

unread,
Aug 8, 2016, 12:34:48 PM8/8/16
to google-gson
class Employee{
private String name;
private String ssn;
}


Now i want to encrypt only ssn that is on serialization ssn get encrypted and on deserialization ssn get decrypted.


The only way i can see this can be achieved is instead of using String type specify custom type then register type adapter for that. Whereas there should be some option that i should be able to annotate the field and then do some custom parsing base of those field.

Please let me know what could be other possible way to achieve this.

Jake Wharton

unread,
Aug 9, 2016, 9:21:45 AM8/9/16
to googl...@googlegroups.com
Annotate the encrypted fields with @JsonAdapter(Encrypted.class) and have Encrypted be a TypeAdapterFactory that does the encryption/decryption.
--
You received this message because you are subscribed to the Google Groups "google-gson" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-gson...@googlegroups.com.
To post to this group, send email to googl...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-gson.
For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages