7

What is the difference between JsonIgnore on Field vs JsonIgnore on a getter of a field in Jackson?

Sagar
  • 5,315
  • 6
  • 37
  • 66
  • Putting it on the field is the equivalent of annotating both the getter and the setter individually. – Roddy of the Frozen Peas Jul 23 '18 at 21:07
  • What if I don't have getter and setter for the field? – Sagar Jul 23 '18 at 21:09
  • 3
    well then you don't need to worry about annotating the getter, do you? – Roddy of the Frozen Peas Jul 23 '18 at 21:09
  • I thought Jackson looks for getter and Setter for a field to deserialize/serialize , if we don't have them, still, it does do deserialize/serialize operation ? – Sagar Jul 23 '18 at 21:10
  • Its diff. \ I just had a bug where `@JsonIgnore` didnt work when I put it on Field, only works when I put it on Getter. (In Spring) \ see https://stackoverflow.com/a/19895272/9549068 \ I have no idea why (cuz Lombok?), so I came here for an explanation, didnt find one. – Nor.Z Jul 14 '23 at 14:36

2 Answers2

7

@JsonIgnore annotation is used to ignore fields from de-serialization and serialization, it can be put directly on the instance member or on its getter or its setter. The application of the annotation in any of these 3 points, leads to the total exclusion of the property from both the serialization and de-serialization processes (and this applies starting from Jackson 1.9; the version used in these examples is Jackson 2.4.3).

Note: Before version 1.9, this annotation worked purely on method-by-method (or field-by-field) basis; annotation on one method or field did not imply ignoring other methods or fields

Example

 import java.io.IOException;

 import com.fasterxml.jackson.annotation.JsonIgnore;
 import com.fasterxml.jackson.core.JsonParseException;
 import com.fasterxml.jackson.core.JsonProcessingException;
 import com.fasterxml.jackson.databind.JsonMappingException;
 import com.fasterxml.jackson.databind.ObjectMapper;

 class MyTestClass {

 private long id;
 private String name;
 private String notInterstingMember;
 private int anotherMember;
 private int forgetThisField;

 public long getId() {
    return this.id;
 }

 public void setId(long id) {
     this.id = id;
 }

 public String getName() {
     return this.name;
 }

 public void setName(String name) {
    this.name = name;
 }

 @JsonIgnore
 public String getNotInterstingMember() {
    return this.notInterstingMember;
 }

 public void setNotInterstingMember(String notInterstingMember) {
    this.notInterstingMember = notInterstingMember;
 }

 public int getAnotherMember() {
    return this.anotherMember;
 }

 public void setAnotherMember(int anotherMember) {
    this.anotherMember = anotherMember;
 }

 public int getForgetThisField() {
    return this.forgetThisField;
 }

 @JsonIgnore
 public void setForgetThisField(int forgetThisField) {
    this.forgetThisField = forgetThisField;
 }

 @Override
 public String toString() {
    return "MyTestClass [" + this.id + " , " +  this.name + ", " + this.notInterstingMember + ", " + this.anotherMember + ", " + this.forgetThisField + "]";
    }

  }

Output:

 {"id":1,"name":"Test program","anotherMember":100}
 MyTestClass [1 , Test program, null, 100, 0]

But still it is possible to change this behavior and make it asymmetric, for example to exclude a property only from the deserialization using the @JsonIgnore annotation together with another annotation called @JsonProperty.

Ryuzaki L
  • 37,302
  • 12
  • 68
  • 98
1

As far as I am aware, there is no difference between the two. The @JsonIgnore JavaDocs seem to use the various places where you may place it interchangeably as well.

If you have getters that produce no side effects, however, and wish to eventually incorporate something like lombok into your project for whatever reason, it would be much easier to make the switch if you put the @JsonIgnore on the fields. In addition, IMO, it is much clearer to put this kind of de/serialization information in the same place where the parameter is defined, i.e. the field itself.

filpa
  • 3,651
  • 8
  • 52
  • 91