users@jersey.java.net

JSON Notation

From: Marcel Thibault <mthibault_at_novell.com>
Date: Thu, 19 Jun 2008 08:31:00 -0600

Hello,

Using the following XML doc:

<list>
   <item>
        <id1>val1<id1>
        <id2>val2<id2>
   <item>
   <item>
        <id1>val1<id1>
        <id2>val2<id2>
   <item>
....
</list>

I am trying to get the following JSON notation:

{[{"id1":"val1", "id2":"Val2"},{"id1":"val1","id2","val2"}]}

Jersey comes close to rendering this format but I can't seem to get rid of the "item" text.

I get the following:
{"item":[{"id1":"val1", "id2":"Val2"},{"id1":"val1","id2","val2"}]}

I am using the following code for my ContextResolver:

@Provider
public class JAXBContextResolver implements ContextResolver<JAXBContext> {

    private JAXBContext context;
    private Class[] types = {List.class, Item.class};

    public JAXBContextResolver() throws Exception {
        Map<String, Object> props = new HashMap<String, Object>();
        props.put(JSONJAXBContext.JSON_NOTATION, JSONJAXBContext.JSONNotation.MAPPED.toString());
        props.put(JSONJAXBContext.JSON_ROOT_UNWRAPPING, Boolean.TRUE);
        props.put(JSONJAXBContext.JSON_ARRAYS, "[\"Item\"]");
        this.context = new JSONJAXBContext(types, props);
    }

    public JAXBContext getContext(Class<?> objectType) {
        for (Class type : types) {
            if (type == objectType) {
                return context;
            }
        }
        return null;
         
    }
}

Is it possible to strip the extra "item" text from the JSON structure using a ContextResolver

Thanks,

Marcel Thibault