users@jersey.java.net

Re: [Jersey] JSON Arrays

From: Jakub Podlesak <Jakub.Podlesak_at_Sun.COM>
Date: Mon, 26 Jan 2009 11:08:15 +0100

Hi Sarat,

On Thu, Jan 22, 2009 at 12:52:50PM -0800, sarat.pediredla wrote:
>
> Hi Jakub,
>
> Sorry for the lack of response on this.

I am sorry for this time. I was experimenting with
another e-mail client and some messages somehow ended in a wrong folder :o[

>
> I have checked the names. The add("user") bit was just an example from me.
> The code is correct but I might be slightly confused as to what to use here?
> Should it be add("user") or add("users") ?

It depends on your JAXB beans. To be more specific, if you have:

@XmlElement List<User> user;

then you will end with <user/> tag in XML and
you should add("user").
It really depends on what XML tag name you are dealing with
as a wrapping tag for your array/list.

I am going to announce another JSON notation this week, where
you will get rid of this kind of this a bit complex configuration.

More details will come, but you can try out now with JSONJAXBContext.JSONNotation.NATURAL
and you do not need to bother with configuring anything.
More details to come.

~Jakub

>
>
>
> Jakub Podlesak wrote:
> >
> >
> > Hi Sarat,
> >
> > please see inline...
> >
> > On Sun, Dec 21, 2008 at 02:17:50PM -0800, sarat.pediredla wrote:
> >>
> >> Hi,
> >>
> >> I know that in Jersey 1.0, to enable JSON elements to be forced to be
> >> displayed as arrays, I can use the following,
> >>
> >> props.put(JSONJAXBContext.JSON_ARRAYS, new
> >> HashSet<String>(1){{add("user");}});
> >>
> >> This might seem a newbie question but what do I do when I want multiple
> >> beans added to the JSON_ARRAYS list? Is the following code right? It
> >> definitely does not work as the JSON returned does not treat single
> >> elements
> >> as arrays.
> >
> > Could you please send out a simple test case.
> > Your code for setting the JSON_ARRAYS property looks fine and the thing
> > should work.
> > There is even a junit test
> > [JsonXmlStreamReaderWriterTest.testTwoListsWrapper]
> > for your case.
> >
> > Could you maybe double-check you added the right names?
> > I can see add("user") in your first example, and then add("users")
> > in the second. Are the names correct?
> >
> > ~Jakub
> >
> >>
> >> public class JsonContextResolver implements ContextResolver<JAXBContext>
> >> {
> >>
> >> private JAXBContext context;
> >> private Class[] types = {
> >> User.class,
> >> Role.class,
> >> Group.class
> >> };
> >>
> >> public JsonContextResolver() throws Exception {
> >> Map props = new HashMap<String, Object>();
> >> props.put(JSONJAXBContext.JSON_NOTATION,
> >> JSONJAXBContext.JSONNotation.MAPPED);
> >> props.put(JSONJAXBContext.JSON_ROOT_UNWRAPPING, Boolean.TRUE);
> >> props.put(JSONJAXBContext.JSON_ARRAYS, new
> >> HashSet<String>(types.length){{
> >> add("users");
> >> add("roles");
> >> add("groups");
> >> }});
> >> this.context = new JSONJAXBContext(types, props);
> >> }
> >>
> >> public JAXBContext getContext(Class<?> objectType) {
> >> for(Class<?> x : types){
> >> if(x.equals(objectType)){
> >> return context;
> >> }
> >> }
> >> return null;
> >> }
> >> --
> >> View this message in context:
> >> http://n2.nabble.com/JSON-Arrays-tp1687952p1687952.html
> >> Sent from the Jersey mailing list archive at Nabble.com.
> >>
> >>
> >> ---------------------------------------------------------------------
> >> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> >> For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >>
> >
> > ---------------------------------------------------------------------
> > To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> > For additional commands, e-mail: users-help_at_jersey.dev.java.net
> >
> >
> >
>
> --
> View this message in context: http://n2.nabble.com/JSON-Arrays-tp1687952p2200121.html
> Sent from the Jersey mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jersey.dev.java.net
> For additional commands, e-mail: users-help_at_jersey.dev.java.net
>