I am using jackson-databind 2.9.6:

compile("com.fasterxml.jackson.core:jackson-databind:2.9.6")


And it looks like I found a bug. When object mapper is told to not include 
nulls, it still includes if you read JSON to tree, then write to string 
from this tree.
I wrote small example using groovy and spock:

package jackson

import com.fasterxml.jackson.annotation.JsonInclude
import com.fasterxml.jackson.databind.ObjectMapper
import spock.lang.Specification

class ObjectMapperTest extends Specification {
    def "should not include nulls"() {
        given:
        def json = '''
                {
                    "a": {},
                    "b": [],
                    "c": "",
                    "d": 0,
                    "e": null
                }
        '''

        def obj = [
                "a": [:],
                "b": [],
                "c": "",
                "d": 0,
                "e": null
        ]

        def jsonMapper = new ObjectMapper()
                .setSerializationInclusion(JsonInclude.Include.NON_NULL)
        def tree = jsonMapper.readTree(json)


        when:
        def a1 = jsonMapper.writeValueAsString(tree)
        def a2 = jsonMapper.writeValueAsString(obj)

        then:
        a1 == a2
    }
}



Because the same mapper is used, both a1 and a2 should not include key "e" 
(its value is null).

But the actual output is the following:
Condition not satisfied:

a1 == a2
|  |  |
|  |  {"a":{},"b":[],"c":"","d":0}
|  false
|  9 differences (75% similarity)
|  {"a":{},"b":[],"c":"","d":0(,"e":null)}
|  {"a":{},"b":[],"c":"","d":0(---------)}
{"a":{},"b":[],"c":"","d":0,"e":null}

Expected :{"a":{},"b":[],"c":"","d":0}

Actual   :{"a":{},"b":[],"c":"","d":0,"e":null}


Is that a bug?

-- 
You received this message because you are subscribed to the Google Groups 
"jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to jackson-user+unsubscr...@googlegroups.com.
To post to this group, send email to jackson-user@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to