-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Add new JsonInclude.Include feature to exclude maps after exclusion removes all elements. #497
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Value of |
NON_DEFAULT produces the exact same result as NON_NULL. Here is an actual payload that correlates to a pojo and the json each feature produces. The payload includes all the different combinations of nulls and empty strings: {"peopleKeyword":"","country":[],"zipCodeRadius":{"zipCode":"","distanceType":"","radius":""},"salesInfo":{"low":null,"high":null},"industryInfo":{"usSics":[]},"ipo":{}} NON_DEFAULT: NON_NULL: NON_EMPTY: NON_NULL and NON_EMPTY only ignored the nulls. I expected for NON_EMPTY to at least not write the empty map, but it didn't even do that. Just the empty arrays and strings. |
Actually what I need to know is not so much JSON, but POJO definitions. Could you add class definition for the root value you are using here? All determinations are done at object level, without considering JSON. |
Actually, ok, I think I see what you mean here. So: determination of emptiness is done on source Changing behavior here is quite difficult, due to sequencing of events that exclude empty values. |
#588 was implemented, and for 2.6 may allow solving the "recursive emptiness" problem, at least for some cases. |
Missing piece was to add handling in class POJO
{
@JsonInclude(content=JsonInclude.Include.NON_EMPTY,
value=JsonInclude.Include.NON_EMPTY)
public Map<String,String> values; would produce
for content where |
Thank you. Greatly appreciated. On Thu, Nov 5, 2015 at 5:14 PM, Tatu Saloranta [email protected]
|
This doesn't solve the nested POJO problem. |
solved. |
@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. |
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:
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:
I would rather have to deal with just this:
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.
The text was updated successfully, but these errors were encountered: