On Sun, Dec 17, 2017 at 4:01 PM, Simon Joseph Aquilina
<[email protected]> wrote:
> 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?

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 +-

-- 
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 [email protected].
To post to this group, send email to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to