users@jersey.java.net

Re: [Jersey] Polymorphic deserialization of JSON?

From: Tatu Saloranta <tsaloranta_at_gmail.com>
Date: Sat, 9 May 2009 21:43:45 -0700

On Sat, May 9, 2009 at 3:40 PM, Chad McHenry <mchenryc_at_gmail.com> wrote:
> 2009/5/9 Felipe Gaúcho <fgaucho_at_gmail.com>
...
> Thanks for sharing your code Filipe. As I feared, I'm looking for a solution
> to a different problem.
> Using shapes as an example, I would like to be able to send POST and PUT
> messages to a Jersey Resource which accepts a "Shape" as an argument, but
> instead of deserializing a Shape, I would like the unmarshaller to recognize
> the descriminator in the incoming data, and deserialize a Circle or
> Rectangle.

I may be wrong, but I thought some earlier comments suggested that
required type information was being lost between JSON<->XML/Stax event
conversions.
Type information would usually be done using xsi:type though, not a
custom discriminator (AFAIK), and requires w3c schema to have proper
type inheritance (extension) definitions.

Now: usually you could do something like this using custom converters
(deserializers whatever they are called). I'm not a JAXB expert, but
it looks like this area is not very well covered -- there's
DatatypeConverter, but that's for simple (textual value) types; and
then javax.xml.bind.annotation.DomHandler, which sounds like it can be
used for custom deserialization. But only if input comes in as DOM
Element, which might not work with JAXB/json variant.

-+ Tatu +-