views:

27

answers:

1

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);
+1  A: 

For what it's worth, Jackson 1.6 will have this:

objectMapper.configure(SerializationConfig.WRITE_NULL_MAP_VALUES, false);

which does do what you want. Existing method only works for beans, and is not being changed to ensure maximum backwards compatibility.

StaxMan
And 1.6 was just released couple of days ago as well.
StaxMan