I would like to serialize a HashMap as a string through the Jackson JSON processor. For example:
String strMap = getMapper().writeValueAsString(myHashMap);
result output -> {"r_id":6,"a_am":null,"smb":"Submit","a_li":null,"l_id":878,"pos":[1345,1346,1347]}
I don't know how to disable null values serialization for Map. It works fine only for POJO if configure the Jackson like this:
mapper.getSerializationConfig().setSerializationInclusion(Inclusion.NON_NULL);
To ignore the null fields, put @JsonInclude on class level or field level.
readValue() returns null for JSON input consisting of JSON value null . It does not return null for any other case: missing input (for example) would be rewarded by an exception; and no deserializer produces null by default.
Just use this @JsonSerialize(include = Inclusion. NON_NULL) instead of @JsonInclude(Include. NON_NULL) and it works..!!
With latest Jackson version, on the ObjectMapper, you can do:
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With