tomee-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Johnny Macnum <johnny_mac...@hotmail.com>
Subject Dealing with Jettison array serialization issues
Date Wed, 05 Feb 2014 10:54:24 GMT
Hello,

As described in the CXF document (http://cxf.apache.org/docs/jax-rs-data-bindings.html#JAX-RSDataBindings-ConfiguringJSONprovider),
Jettison wrongly serializes List objects by default. The default approach of Jettison is for
me a bad practice.

JSONProvider has two properties 'serializeAsArray'  and 'arrayKeys' to handle this issue.


I think TomEE initializes wrongly the JSONProvider. You should by default set serializeAsArray
to 'true' and allow the developer to configure the list of keys to put in the arrayKeys so
as to produce a well-defined JSON.

 private static List<Object> defaultProviders() {
        final JAXBElementProvider jaxb = new JAXBElementProvider();
        final Map<String, Object> jaxbProperties = new HashMap<String, Object>
();
        jaxbProperties.put(Marshaller.JAXB_FRAGMENT, true);
        jaxb.setMarshallerProperties(jaxbProperties);

        final JSONProvider json = new JSONProvider();
        // TOMEE-514
        // json.setSerializeAsArray(true);

        return Arrays.asList((Object) jaxb, json);
    }

In order to solve my issue, I had to define my customer provider which is quite cumbersome
for a lambda developer.

@Produces("application/json")
@Consumes("application/json")
@Provider
public class CustomJSONProvider<T> extends JSONProvider<T> {

    public CustomJSONProvider() {
        super();
        setSerializeAsArray(true);
        setArrayKeys(Arrays.asList("element1", "element2"));
    }

}


Regards,

Jonathan

 		 	   		  
Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message