users@jersey.java.net

[Jersey] Re: How do I marshal nested lists as JSON? I get an array of nulls or an array of one-element dictionaries containing an array.

From: totolearning <totolearning_at_gmail.com>
Date: Thu, 5 Apr 2012 07:07:13 -0700 (PDT)

Thanks you all for giving the clue - This is the solution for a Resolver to
marshal nested lists as JSON - and make the List properly will be an Array
in JSON as well

import javax.ws.rs.core.MediaType;
import javax.ws.rs.ext.Provider;

import org.codehaus.jackson.jaxrs.JacksonJsonProvider;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.map.SerializationConfig;

@Provider
public class NewContextResolver extends JacksonJsonProvider {
                public NewContextResolver(){
                }

            @Override
                public ObjectMapper locateMapper(Class<?> type, MediaType mediaType)
            {
                    ObjectMapper mapper = super.locateMapper(type, mediaType);
                    //DateTime in ISO format "2012-04-07T17:00:00.000+0000"
               
mapper.configure(SerializationConfig.Feature.WRITE_DATES_AS_TIMESTAMPS,
false);
                //No 'null' value returned
                mapper.configure(SerializationConfig.Feature.WRITE_NULL_MAP_VALUES,
false);
                mapper.configure(SerializationConfig.Feature.WRITE_NULL_PROPERTIES,
false);
                return mapper;
            }
}

--
View this message in context: http://jersey.576304.n2.nabble.com/How-do-I-marshal-nested-lists-as-JSON-I-get-an-array-of-nulls-or-an-array-of-one-element-dictionarie-tp3277157p7440057.html
Sent from the Jersey mailing list archive at Nabble.com.