When is the @JsonProperty property used and what is it used for?

blue-sky picture blue-sky · Sep 25, 2012 · Viewed 413.8k times · Source

This bean 'State' :

public class State {

    private boolean isSet;

    @JsonProperty("isSet")
    public boolean isSet() {
        return isSet;
    }

    @JsonProperty("isSet")
    public void setSet(boolean isSet) {
        this.isSet = isSet;
    }

}

is sent over the wire using the ajax ' success' callback :

        success : function(response) {  
            if(response.State.isSet){   
                alert('success called successfully)
            }

Is the annotation @JsonProperty required here ? What is the advantage of using it ? I think I can remove this annotation without causing any side effects.

Reading about this annotion on https://github.com/FasterXML/jackson-annotations/wiki/Jackson-Annotations I don't know when this is required to be used ?

Answer

OldCurmudgeon picture OldCurmudgeon · Sep 25, 2012

Here's a good example. I use it to rename the variable because the JSON is coming from a .Net environment where properties start with an upper-case letter.

public class Parameter {
  @JsonProperty("Name")
  public String name;
  @JsonProperty("Value")
  public String value; 
}

This correctly parses to/from the JSON:

"Parameter":{
  "Name":"Parameter-Name",
  "Value":"Parameter-Value"
}