Why doesn't @JsonUnwrapped work for lists?

I am using Jackson 2.1.0. Given:

public static final class GetCompanies { private final List<URI> companies; /** * Creates a new GetCompanies. * <p/> * @param companies the list of available companies * @throws NullPointerException if companies is null */ @JsonCreator public GetCompanies(@JsonUnwrapped @NotNull List<URI> companies) { Preconditions.checkNotNull(companies, "companies"); this.companies = ImmutableList.copyOf(companies); } /** * @return the list of available companies */ @JsonUnwrapped @SuppressWarnings("ReturnOfCollectionOrArrayField") public List<URI> getCompanies() { return companies; } } 

When the input list contains http://test.com/ , Jackson generates:

 {"companies":["http://test.com/"]} 

instead:

 ["http://test.com/"] 

Any ideas?

UPDATE . See https://github.com/FasterXML/jackson-core/issues/41 for a discussion on this.

+8
jackson
source share
1 answer

In this case, if this works, you will eventually try to create the following:

 { "http://test.com" } 

which is not legal JSON. @JsonUnwrapped really just removes one layer of wrapping. And although theoretically this can be done to work with arrays in arrays, this is not so. And actually, I am wondering if adding this function is a mistake: mainly because it encourages use, which is often associated with advanced data binding methods (simplicity, one-to-one mapping).

But instead, there will be @JsonValue :

 @JsonValue private final List<URI> companies; 

which means "use the value of this property instead of serializing the object that contains it."

And the creator method will work as is, it is not necessary for either @JsonUnwrapped or for @JsonProperty .

Here is the corrected code:

 public static final class GetCompanies { private final List<URI> companies; /** * Creates a new GetCompanies. * <p/> * @param companies the list of available companies * @throws NullPointerException if companies is null */ @JsonCreator public GetCompanies(@NotNull List<URI> companies) { Preconditions.checkNotNull(companies, "companies"); this.companies = ImmutableList.copyOf(companies); } /** * @return the list of available companies */ @JsonValue @SuppressWarnings("ReturnOfCollectionOrArrayField") public List<URI> getCompanies() { return companies; } } 
+16
source share

All Articles