Search code examples
javajsonjackson

Duplicate Boolean fields in JSON


I have boolean field as

private boolean isCustom;

having getter and setters as

public boolean isCustom() {
        return isCustom;
    }
public void setCustom(boolean isCustom) {
        this.isCustom = isCustom;
    }

And in this case my JSON will be {"custom":false}

but i want JSON to be {"isCustom":false}

so I added @JsonProperty :

@JsonProperty
    private boolean isCustom;

But now there is another problem as my JSON is {"isCustom":false,"custom":false}

Q : How can i eliminate unwanted/duplicate field in this case ?

Note: I am using jackson-all-1.9.11.jar


Solution

  • The annotation accepts a parameter. And it should be placed on the field, getter, and setter to prevent the duplicate

    @JsonProperty("isCustom")