You can use SerializationFeature.WRITE_NULL_MAP_VALUES
feature but before you have to convert your ObjectNode
object to Map
.
See below example:
ObjectMapper mapper = new ObjectMapper();
mapper.configure(SerializationFeature.WRITE_NULL_MAP_VALUES, false);
String nullValue = null;
ObjectNode subNode = new ObjectNode(mapper.getNodeFactory());
subNode.put("test", 1);
subNode.put("nullValue", nullValue);
ObjectNode node = new ObjectNode(mapper.getNodeFactory());
node.put("notNull", "Not null.");
node.put("nullValue", nullValue);
node.set("subNode", subNode);
MapType mapType = mapper.getTypeFactory().constructMapType(Map.class, String.class, Object.class);
Object mapValue = mapper.convertValue(node, mapType);
System.out.println(mapper.writerWithDefaultPrettyPrinter().writeValueAsString(mapValue));
Above program prints:
{
"notNull" : "Not null.",
"subNode" : {
"test" : 1
}
}