question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add new JsonInclude.Include feature to exclude maps after exclusion removes all elements.

See original GitHub issue

Current exclusion features NON_NULL and NON_EMPTY help greatly in removing unwanted text in the json payloads, but maps still get serialized if they originally contained an element even when that child element is removed because it is null or empty. For example, when making a json tree from this json:

{ "foo" : {"bar" : null } }

then serializing with NON_EMPTY produces this:

{"foo" : {} }

but ideally, I want the payload to be:

{}

Seems trivial in this case, but when dealing with a realistic payload like:

{"familyCount":{},"zipCode":{},"areaCode":["555"],"streetAddress":{},"profileInfo":{},"marketInfo":{},"longitude":{},"latitude":{}}

I would rather have to deal with just this:

{"areaCode":["512"]}

These payloads are transported to mobile devices, logged and written to databases, so having them compact and readable is highly desirable. I ended up having to write a “JSON cleaner” that recursively prunes empty nodes to get a compact and readable payload.

It would be great if the ObjectMapper had a built-in serialization feature like NON_EMPTY that does this.

Issue Analytics

  • State:closed
  • Created 9 years ago
  • Reactions:1
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
cherlocommented, Nov 9, 2015

Thank you. Greatly appreciated.

On Thu, Nov 5, 2015 at 5:14 PM, Tatu Saloranta notifications@github.com wrote:

Missing piece was to add handling in MapSerializer, so that now following property:

class POJO { @JsonInclude(content=JsonInclude.Include.NON_EMPTY, value=JsonInclude.Include.NON_EMPTY) public Map<String,String> values;

would produce

{}

for content where values only had “empty” values, such as nulls and empty Strings (“”), or Lists with no elements.

— Reply to this email directly or view it on GitHub https://github.com/FasterXML/jackson-databind/issues/497#issuecomment-154226140 .

0reactions
cowtowncodercommented, Aug 15, 2018

@kellerassel007 As per my other comment, while I do not doubt you have an issue, it is not necessarily the same underlying problem. And since this issue is closed, a new issue with reproduction is needed.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Add new JsonInclude.Include feature to exclude maps after ...
Current exclusion features NON_NULL and NON_EMPTY help greatly in removing unwanted text in the json payloads, but maps still get serialized ...
Read more >
Expected behavior of JsonInclude.NON_EMPTY with Maps?
@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...
Read more >
How to tell Jackson to ignore a field during serialization if its ...
To omit all null values in the class as a default, annotate the class. Per-field/getter annotations can still be used to override this...
Read more >
Jackson Tips: filtering with @JsonInclude | by @cowtowncoder
When serializing content as JSON (or another format Jackson supports), Jackson by default just writes out all property values as they are.
Read more >
Jackson JSON - @JsonInclude NON_EMPTY Example
@JsonInclude(NON_EMPTY) can be used to exclude values that are empty. Following values are considered to be empty:.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found