Given the following POJO, I would like to apply SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED on the links field ONLY.
public class HalRepresentation {
@JsonProperty("_links")
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private final Map<String, List<Link>> links = new HashMap<String, List<Link>>();
@JsonProperty("_embedded")
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
private final Map<String, Object> embedded = new HashMap<String, Object>();
protected HalRepresentation() {
}
public Map<String, List<Link>> getLinks() {
return links;
}
public Map<String, Object> getEmbedded() {
return embedded;
}
}
I tried to serialize it as follows:
ObjectMapper objectMapper = new ObjectMapper()
.enable(SerializationFeature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED);
try {
outputStream.write(objectMapper.writeValueAsBytes(halRepresentation));
outputStream.flush();
} catch (JsonProcessingException e) {
throw new IllegalStateException(e);
}
But when I do this the unwrap feature is also applied on the embedded field. I tried to find an equivalent annotation for WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED, but I can't find one. Do you have an idea for this using Jackson ?
It appears additional functionality was added to Jackson 2.6 to allow you to apply this feature to a single field using the @JsonFormat annotation:
@JsonProperty("_links")
@JsonInclude(JsonInclude.Include.NON_DEFAULT)
@JsonFormat(with = {Feature.ACCEPT_SINGLE_VALUE_AS_ARRAY,Feature.WRITE_SINGLE_ELEM_ARRAYS_UNWRAPPED})
private final Map<String, List<Link>> links = new HashMap<String, List<Link>>();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With