jackson-databind icon indicating copy to clipboard operation
jackson-databind copied to clipboard

`JsonFormat(shape=Shape.ARRAY)` does not consider `Include.NON_NULL`

Open ayrtondumas opened this issue 2 years ago • 2 comments

@JsonPropertyOrder({"a", "b"})
@JsonFormat(shape = JsonFormat.Shape.ARRAY, with = Feature.ADJUST_DATES_TO_CONTEXT_TIME_ZONE)
public class MyDTO {

  private final String a;

 @JsonInclude(Include.NON_NULL)
  private final String b;
 }

When serialising the above class when b is null, b is still present inside the array

Expected a="hello" b = null. [a:"hello"] Actual [a:"hello", b:"null"]

ayrtondumas avatar Jan 28 '22 14:01 ayrtondumas

[Wrong repo, will move to jackson-databind]

cowtowncoder avatar Jan 29 '22 04:01 cowtowncoder

Since Array-shape notation must have values for all columns (placeholder if nothing else), there must always be a value, even if placeholder, to keep indexes (this would mostly matter for columns "in the middle", but dropping last columns could be problematic as well -- and would require additional logic to determine at any rate). I guess in theory it would be possible to then use null but the way things work is that inclusion filtering does not apply to this notation.

So I think I will considered this working as designed. Would be good to improve documentation to indicate that filtering does not apply for Array-based POJO serialization.

cowtowncoder avatar Jan 29 '22 04:01 cowtowncoder