Adapter for Parent Type

5 views
Skip to first unread message

Jacob

unread,
Nov 4, 2008, 12:27:31 PM11/4/08
to google-gson
We have a set of classes that all extends a class ReferenceData. In
JSON we represent these as simple strings and then I have written an
adapter to to look up the correct instance based on the Type and
string.

Right now we must register the same adapter for each sub tye of
ReferenceData that is in our model. If there a way to register the
adapter once for all sub types of a type?

Jacob

inde...@gmail.com

unread,
Nov 4, 2008, 8:07:35 PM11/4/08
to google-gson
There is no such way currently. Although, internally we use a single
type adapter for all the subclasses of Maps.

In general, the preferred approach to solve your problem will be to
use the base type in the JSON interface. In that case, only the base
type serializer/deserializer will be invoked even if the actual type
is a subtype.

But coming back to your request, I have thought about supporting this.
However, one problem is that all Subclasses will get that type adapter
whether you wanted it or not. So, you may have a sub-class which is
not a proper sub-type, but gets serialized in the standard way
nonetheless. Deserialization presents an even bigger problem since the
deserializer may return an object that can not be assigned to the
actual target field.

Am I imagining these concerns? What do you think?
Inder

Jacob Tomaw

unread,
Nov 4, 2008, 9:15:17 PM11/4/08
to googl...@googlegroups.com
Inder,

I think these might be imagined concerns (although engineers are always thinking up use cases to break any system).  I think it is reasonable to expect all the subclasses of a type to use that adapter.  To me that is what is explicitly intended by registering an adapter like this.

It address deserialization, the type information for the field being assigned to needs to be sent to the adapter.

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