List with one element

640 views
Skip to first unread message

Simon Joseph Aquilina

unread,
Dec 17, 2017, 8:41:46 PM12/17/17
to jackson-user
Hello, I am using com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider to generate a JSON output of my objects. However when a List has only one element the JSON output I get is that of a single object and not a list of objects.

For example if I have a List<User> users = new ArrayList() with only one User object in this I get the following output:

"users" : { "username" : "hello" }

and not

"users" : [{"username" : "hello" }

I have been looking on the internet for a solution but could not find any (using fasterxml jackson library was listed as a solution initself which is why I am using it now). I initialise my JacksonJsonProvider as follows:


        ObjectMapper mapper = new ObjectMapper();
        return new JacksonJsonProvider(mapper);

And the list is annotated as follows:

@XmlElement(name="users")
List<User> users = new ArrayList<>();

What can I do to force JacksonJsonProvider to output a java.util.List always as a list in JSON even when such list has only one element?

Tatu Saloranta

unread,
Dec 18, 2017, 2:33:33 AM12/18/17
to jackson-user
By default, Jackson serializes Lists as Lists. It does not unwrap
single element.

It is possible to change this behavior by enabling

SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED

(or using `@JsonFormat` per-property annotation with similar setting)

so I would try to figure out who is configuring behavior either via
ObjectMapper,
or by declaring `users` property with formatting options.

I hope this helps,

-+ Tatu +-

Simon Joseph Aquilina

unread,
Dec 18, 2017, 3:01:39 PM12/18/17
to jackson-user
I have used the ObjectMapper on its own and you confirm that it works as you say.
It looks like my issue is with the combination with Cxf then.

Tatu Saloranta

unread,
Dec 18, 2017, 5:51:09 PM12/18/17
to jackson-user
On Mon, Dec 18, 2017 at 12:01 PM, Simon Joseph Aquilina
<simonja...@gmail.com> wrote:
> I have used the ObjectMapper on its own and you confirm that it works as you
> say.
> It looks like my issue is with the combination with Cxf then.

Ah. Yes, it may well be that Cxf for some reason chooses different defaults.
I am not sure why (I think this setting is somewhat confusing), but
traditionally one reason has been
that some XML frameworks have had this behavior due to difficulties in
distinguishing case of
single-element array and simple object value.

-+ Tatu +-
> --
> You received this message because you are subscribed to the Google Groups
> "jackson-user" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to jackson-user...@googlegroups.com.
> To post to this group, send email to jackso...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.

Simon Joseph Aquilina

unread,
Dec 19, 2017, 2:13:48 PM12/19/17
to jackson-user
Ok found what my problem was. Cxf was ignoring the jackson provider and I needed to set skip.default.json.provider.registration to true for it to use JacksonJasonProvider.
Reply all
Reply to author
Forward
0 new messages