Jackson ObjectNode: how to not include NULL on serialization
<p>I'm having trouble excluding Null/Empty properties when I serialize my ObjectNode back to a string.  I googled around and all the references about <em>JsonInclude.Include.NON_NULL</em> are regarding serializing and deserializing from a Bean, not an ObjectNode - It seams that the serialization behaves differently from an "arbitrary" ObjectNode instead of a pre-defined Bean.</p>

<p>My goal is to have the output = <strong>{"name":"myName"}</strong>, Not <strong>{"name":"myName","nu":null,"empt":""}</strong></p>

<pre><code>import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.node.ObjectNode
import com.fasterxml.jackson.databind.ObjectMapper

val jsonInputStr = """{"name": "myName"}"""
val newObjMapper = new ObjectMapper().setSerializationInclusion(JsonInclude.Include.NON_EMPTY)

val json = newObjMapper.readTree(jsonInputStr).asInstanceOf[ObjectNode]
json.put("nu", null.asInstanceOf[String])
json.put("empt", "")
val str = newObjMapper.writeValueAsBytes(json)
println(s"OUTPUT = ${new String(str)}")
</code></pre>

<p>prints:
<strong>OUTPUT = {"name":"myName","nu":null,"empt":""}</strong></p>