Ah! This makes good sense: By using .setSerializationInclusion(Include.NON_EMPTY), I had set both content and value to NON_EMPTY and gotten option B.
Using value=NON_EMPTY, content=ALWAYS works as I expected. Thanks Tatu! On Tuesday, August 1, 2017 at 2:14:06 PM UTC-7, Tatu Saloranta wrote: > > Ok: this is where distinction between `value` and `content` differ, > for Maps (and referential types). I think your question is similar to > one I had when deciding earlier that 2 properties are needed. > > Basic `value`, like > > @JsonInclude(Include.NON_EMPTY) > > would only exclude "map" itself, and only if it has no elements on Java > side. > It's, I think, your Option A. > > But if adding `content`, like > > @JsonInclude(value = Include.NON_EMPTY, content = Include.NON_EMPTY) > > serializer should instruct `NON_EMPTY` check for Map values as well, > and in case of empty `List` or array, that element would be excluded; > and then `Map`, too, would be excluded if nothing was output. > And this would achieve Option B I think. > > Does this make sense? > > Handling gets rather tricky here, partly due to difference between > contents and vlaue itself; and partly since handling of emptiness is > very different between POJOs and Maps. > > Further, although ideally `content=Include.NON_EMPTY` SHOULD work for > Collections and Arrays, currently it does not; only `value` is > supported. This does not matter in your use case, but means that > functionality is not fully orthogonal. > > -+ Tatu +- > > > > On Tue, Aug 1, 2017 at 12:02 PM, Daniel Halperin <[email protected] > <javascript:>> 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] <javascript:>. > > To post to this group, send email to [email protected] > <javascript:>. > > For more options, visit https://groups.google.com/d/optout. > -- 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.
