users@jersey.java.net

Re: Problem with JSON serialization of wrapped lists

From: Frank Martínez <mnesarco_at_gmail.com>
Date: Mon, 7 Apr 2008 05:27:22 -0500

Thanks Jakub,

Finally i wrote my own MessageBodyWriter using a free library named
json-lib and it works like a charm.

> JSON writer works at XML Infoset level, (not in the JAXB model
> level), so there is no way to recognize the former case was an array (list).
>
> However, you can provide a simple hint to the JSON writer
> via creating a custom JSONJAXBContext for your bean:
>
> @Provider
> public static class JAXBContextResolver implements ContextResolver<JAXBContext> {
>
> private JAXBContext context;
> private Class[] types = {BuildingList.class};
>
> public JAXBContextResolver() throws Exception {
> Map<String, Object> props = new HashMap<String, Object>();
> props.put(JSONJAXBContext.JSON_NOTATION, "MAPPED");
> props.put(JSONJAXBContext.JSON_ROOT_UNWRAPPING, Boolean.TRUE);
> props.put(JSONJAXBContext.JSON_ARRAYS, "[\"buildings\"]");
> this.context = new JSONJAXBContext(types, props);
> }
>
> public JAXBContext getContext(Class<?> objectType) {
> return (types[0].equals(objectType)) ? context : null;
> }
> }
>

I decided not to use Jaxb2Json support because is too hard to maintain
this kind of configurations in a big application with a lot of
collections and generic classes.

Thanks and Cheers,
Frank.

-- 
Frank D. Martínez M.
Asimov Technologies Ltda.
Blog: http://www.ibstaff.net/fmartinez/