jackson-databind
jackson-databind copied to clipboard
setSerializationInclusion(JsonInclude.Include.NON_EMPTY doesn't filter empty values from collections
Search before asking
- [X] I searched in the issues and found nothing similar.
Describe the bug
Empty Map values in collections don't get filtered out.
Version Information
2.17.2
Reproduction
Here is code for the corresponding unit test:
final var objectMapper = new ObjectMapper()
.setSerializationInclusion(JsonInclude.Include.NON_EMPTY);
final var payload = Map.of(
"key", "value",
"empty", Map.of(),
"list", List.of(
Map.of("key", "value"),
Map.of(),
Map.of(),
Map.of("key", "value")));
final var json = objectMapper.writeValueAsString(payload);
final var deserialized = (Map<?,?>) objectMapper.readValue(json, Map.class);
final var expected = Map.of(
"key", "value",
"list", List.of(
Map.of("key", "value"),
Map.of("key", "value")));
assertThat(deserialized)
.isEqualTo(expected);
Expected behavior
It's expected that the test would pass.
Additional context
No response