JSON single element arrays

177 views
Skip to first unread message

Jason Rowland

unread,
Jul 15, 2013, 7:48:24 PM7/15/13
to grails-jax...@googlegroups.com
When I annotate a list and it only has one element, with JSON, it renders as a map and not an array.

{
    "@size": "1",
    "list": {
        "name": "Jason"
    }
}

I expect it to be this:
{
    "@size": "1",
    "list": [
        {
            "name": "Jason"
        }
    ]
}

How do I accomplish this?

Noam Tenne

unread,
Jul 21, 2013, 4:00:37 AM7/21/13
to grails-jax...@googlegroups.com
Can you provide a gist of some sort that reproduces this? 


--
You received this message because you are subscribed to the Google Groups "grails-jaxrs-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to grails-jaxrs-dis...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Jason Rowland

unread,
Jul 23, 2013, 2:49:00 PM7/23/13
to grails-jax...@googlegroups.com
Here is a gist with simple strings as the element in the array.
https://gist.github.com/jsonxr/6065040

Noam Tenne

unread,
Aug 7, 2013, 6:46:10 AM8/7/13
to grails-jax...@googlegroups.com
Thanks. 
The Grails plugin doesn't have any special effect in this case so the question should probably be posted to the Jersey user list.


--

Jason Rowland

unread,
Aug 7, 2013, 2:18:21 PM8/7/13
to grails-jax...@googlegroups.com
It is really a result of using an old jersey implementation.  The jersey in the plugin is 1.14, but the latest is now >2.0 which fixes this issue. I attempted to convert the plugin locally to use 2.0 but was blocked because SpringServlet isn't in 2.1 yet.

Noam Tenne

unread,
Oct 6, 2013, 11:21:02 AM10/6/13
to grails-jax...@googlegroups.com
Has this by any chance fixed on the latest revision of Jersey 1.x?
We plan to move the plugin to JAX-RS 2.0, but it'll probably be on a different branch because it breaks backwards compatibility 


Christian Junk

unread,
Nov 5, 2013, 1:45:42 PM11/5/13
to grails-jax...@googlegroups.com
Hi!

The following workaround is working for me for all non-domain objects. I am using Jackson for serializing the objects to JSON. This fixes the 'single-element-array' problem:

1. Add Jackson as dependency to your BuilddConfig.groovy

runtime 'com.fasterxml.jackson.jaxrs:jackson-jaxrs-json-provider:2.0.5'

2. Create a class JsonMessageBodyWriter.groovy in grails-app/providers

import com.fasterxml.jackson.databind.ObjectMapper

import javax.ws.rs.Produces
import javax.ws.rs.ext.Provider

import static org.grails.jaxrs.support.ConverterUtils.*
import static org.grails.jaxrs.support.ProviderUtils.*
import grails.converters.JSON
import grails.converters.XML

import java.lang.annotation.Annotation
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type

import javax.ws.rs.WebApplicationException
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.MultivaluedMap
import javax.ws.rs.ext.MessageBodyWriter

@Provider
@Produces("application/json")
class JsonMessageBodyWriter implements MessageBodyWriter {

    @Override
    boolean isWriteable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return true
    }

    @Override
    long getSize(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType) {
        return -1
    }

    @Override
    void writeTo(Object t, Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, Object> httpHeaders, OutputStream entityStream) throws IOException, WebApplicationException {
        new ObjectMapper().writeValue(entityStream, t);
    }
}

Regards,
Christian

To unsubscribe from this group and stop receiving emails from it, send an email to grails-jaxrs-discuss+unsub...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages