Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Duplicate Boolean fields in JSON

Tags:

java

json

jackson

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

like image 893
SuhasD Avatar asked Oct 13 '16 13:10

SuhasD


3 Answers

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

@JsonProperty("isCustom")
like image 154
OneCricketeer Avatar answered Sep 24 '22 11:09

OneCricketeer


You can set custom name in json property like this

@JsonProperty(name="isCustom")
private boolean isCustom;

Please refer docs for more info

like image 43
Mohammad Adil Avatar answered Sep 25 '22 11:09

Mohammad Adil


You can try this

@JsonProperty("isCustom")
like image 36
anna Avatar answered Sep 23 '22 11:09

anna