dev@jaxb.java.net

Re: accessing jaxb model info (related to json (un)marshalling) question

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Mon, 17 Mar 2008 14:48:11 -0700

Jakub Podlesak wrote:
> Hi Kohsuke,
>
> i am working on further improving JAXB bean->JSON marshalling
> for Jersey. Now i am kind of stuck on an issue i am not sure
> how to cleanly resolve. I tried to look at JAXB docs but still
> have not find a resolution. I hope you could help somehow.
> Even confirmation something would not work would be helpful.

OK. I hope you don't mind my CC-ing this to dev_at_jaxb.dev.java.net to
keep this useful dialog in the archive.


> Based on our last conversation i have ended up with
> the XML stream reader/writer approach. And it works
> pretty well.
>
> Now i would love to get access to information on java types
> of elements/attributes being marshalled. The thing is that i would
> like to marshal e.g.
> { "a": 12 }
> instead of
> { "a" : "12" }
> if i knew that "a" was a number.
> i.e. in XML you would always have
> <a>12</a>
> while in JSON it could be one of the above mentioned expressions.

I see.


> Another case is arrays/collections, when for e.g.
> @XmlElement List<String> list
> i could have
> <list>1st element</list>
> but in JSON i would like to get:
> [ {"list" : "1st"} ]
> and not just:
> {"list" : "1st"}

You probably mean { "list" : ["1st"] }


> Notice: the former is not an issue for collections with more then
> one item, when the writer can easily figure out...
>
> So my question is: is there any way to obtain such type information
> from e.g. [JAXBContext] ?
> I can probably analyze the JAXB beans of my own based on reflection,
> get the JAXB annotations, etc...., but i believe there must be another
> way? Is there?

JAXB internally does have this information, but it's probably difficult
to use it if you are at XML infoset level. You can access our
RuntimeTypeInfoSet object, which is like the root of all the binding
information, but you won't be able to correlate the infoset you are
seeing with this model.

For you to effectively use the model, you'd have to write your
POJO->JSON engine by using information from RuntimeTypeInfoSet.

I think it is quite doable, because marshalling is easier than
unmarshalling to begin with, plus you won't have to worry about
namespace handling, which is like 50% of the complexity.

But at the same time, it does require a lot of interaction with this
JAXB binding model, and it might be difficult for people who are not
familiar with this.

I suppose the best is if I could give you some skeleton so that you can
figure out the rest by extrapolating it. Are you coming to JavaOne?
Maybe we can sit down one day together and do it.


> Thank you a lot in advance,
>
> ~Jakub
>
> P.S. Currently i give users a possibility to manually configure
> what is a string/array and what is not. Now i would like to get rid
> of the unneccessary configuration step.

-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com