Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to avoid null values serialization in HashMap?

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);
like image 831
eugenn Avatar asked Jun 29 '10 12:06

eugenn


People also ask

How do you ignore null values in an object?

To ignore the null fields, put @JsonInclude on class level or field level.

Can Objectmapper readValue return null?

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.

How do I ignore null values in post request body in spring boot?

Just use this @JsonSerialize(include = Inclusion. NON_NULL) instead of @JsonInclude(Include. NON_NULL) and it works..!!


1 Answers

With latest Jackson version, on the ObjectMapper, you can do:

mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);

like image 168
Rémy Giard Avatar answered Sep 19 '22 20:09

Rémy Giard