I should add: another surprising behavior for me is the serialization of
{"map": {"key": []}} is '{}' when Include.NON_EMPTY is set, presumably
because after '"key": []' is omitted from the Map, then 'map' itself is
considered empty and omitted.
private static class Foo {
public Map<String, String[]> map = new HashMap<>();
}
@Test
public void testMapFieldWithEmptyValue() throws JsonProcessingException {
Foo foo = new Foo();
foo.map.put("key", new String[0]);
ObjectMapper mapper = new
ObjectMapper().setSerializationInclusion(Include.NON_EMPTY);
assertThat(mapper.writeValueAsString(foo), containsString("map"));
}
On Tuesday, August 1, 2017 at 12:02:38 PM UTC-7, Daniel Halperin wrote:
>
> I have an object like this:
>
> class Foo {
> Map<String, String[]> map;
> }
>
> using JsonInclude.NON_EMPTY in the default serializer.
>
> Suppose that map = {"key": []}. What is the expected serialization? Should
> "key": [] be present in the output JSON?
>
> I can see two arguments:
> - Option A: map is a property of Foo, but "key" is just part of its value.
> It's not a property, so "map: {"foo": []}" should appear in the result JSON.
> - Option B: JSON doesn't really distinguish Objects and Maps, so we treat
> both foo.map and map.get("key") as JSON properties. According to NON_EMPTY,
> "key" should be omitted.
>
> The current behavior is Option B, which I found surprising at first when
> reading the NON_EMPTY documentation. Wondering if this is a conscious
> choice and the expected behavior, or a bug, or something else?
>
> Thanks!
> Dan
>
--
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.