1 Model 2 JSON structures

4 views
Skip to first unread message

Jacob

unread,
Nov 4, 2008, 12:24:22 PM11/4/08
to google-gson
Gson Group,

There is a use case for you.

We have one model but in different circumstances we want to serialize
different fields.

Model:
public class Book{
public long id;
public String name;
public String author;
public String publisher;
public Date createDate;
public String createUser;
}

In our admin tool we want all the fields to be serialized. But in our
publicly fasing system we do not want createDate and createUser
exposed.

Is there a more lightweight way to do this other than creating an
adapter for the second case. This is a simple example, in reality we
have much larger models and it will be significant effort to manual
serialize them, just to remove a few fields.

If this is not already possible, has any thought ever been given to
being able to annotate the fields with a case or scenario name,
similar to Since. Or maybe there is another way this meta data could
be passed.

Jacob

unread,
Nov 4, 2008, 12:32:13 PM11/4/08
to google-gson
So I think this exact case could be accomplished with the Expose
annotation, if you use it in one instance and then not in the other.

So if you will imagine you want three difference json structures, or
your models are more complicated and you are already using Expose in
all cases.

Jacob

inde...@gmail.com

unread,
Nov 4, 2008, 8:17:50 PM11/4/08
to google-gson
You probably know this already, but you can use context.serialize() to
delegate serialization of a complex type to Gson.

I haven't thought through the implications, but you can create dummy
subtypes for each, and register different serializers for each. That
doesnt seem like a good OO practice either, since the subtype is
actually representing a subset of data.




Jacob Tomaw

unread,
Nov 4, 2008, 8:53:21 PM11/4/08
to googl...@googlegroups.com
Inder

Right on context.serialize().  Thankfully that can be done, but I was thinking if my type has a large number (n) of members, but I only want exclude a few (m), I would need to manually kick off context.serialize() and put the values in a JsonObject n-m times, right?

I thought about the dummy type, but I am not sure that would pass our code review standards and does not seem very maintainable.

One of the other solutions we have thought of is serializing the model and then removing the fields, either from the string directly or by parsing it again in another marshaller.

None of these options are attractive to me.

Jacob
--
Jacob Tomaw
tfl:The Flatiron Life (http://tomaw.com)
Follow me on Twitter! (http://twitter.com/JacobTomaw)
Reply all
Reply to author
Forward
0 new messages