Skip to content

Java Bean naming conventions not respected for boolean properties -> Incompatibility with standard Jackson ObjectMapper #291

@prismec

Description

@prismec

If the field name of a boolean Java Bean Property differs from the name of the getters and setters and starts with "is" it is serialized wrong. This does not follow bean naming conventions and is incompatible with an ObjectMapper used without the Scala module. Here's a Spock specification to show the problem (the second test may occasionally fail due to ordering of properties):

package smarter.ecommerce.whoop

import com.fasterxml.jackson.databind.ObjectMapper
import com.fasterxml.jackson.databind.SerializationFeature
import com.fasterxml.jackson.datatype.joda.JodaModule
import com.fasterxml.jackson.module.scala.DefaultScalaModule
import spock.lang.Specification

public class JacksonSerializationIssue extends Specification {

static class Bean {
    private boolean isBooleanProperty = true;
    private boolean nameDoesNotMatter = false;
    private String isString = "value";

    public boolean isBooleanProperty() {
        return isBooleanProperty;
    }

    public void setBooleanProperty(boolean booleanProperty) {
        this.isBooleanProperty = booleanProperty;
    }

    public boolean getAnotherBooleanProperty() {
        return nameDoesNotMatter;
    }

    public void setAnotherBooleanProperty(boolean anotherBooleanProperty) {
        this.nameDoesNotMatter = anotherBooleanProperty;
    }

    public String getString() {
        return isString;
    }

    public void setString(String isString) {
        this.isString = isString;
    }
}

def "serialize to JSON with scala module"() {
    def objectMapper = new ObjectMapper().registerModule(new DefaultScalaModule())

    expect:
    objectMapper.writeValueAsString(new Bean()) == """{"booleanProperty":true,"anotherBooleanProperty":false,"string":"value"}"""
}

def "serialize to JSON without scala module"() {
    def objectMapper = new ObjectMapper()

    expect:
    objectMapper.writeValueAsString(new Bean()) == """{"string":"value","booleanProperty":true,"anotherBooleanProperty":false}"""
}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions