Close

Jackson JSON - Using @JsonAutoDetect to define fields/methods access visibility rules

[Updated: Sep 15, 2018, Created: Jun 28, 2018]

By default Jackson can access public fields for serialization and deserialization. If there's no public fields available then public getters/setters are used. We can customize this default behavior by the use of @JsonAutoDetect annotation.

@JsonAutoDetect annotation can be used to specify access visibility rules for fields and/or methods. Following is its snippet:

package com.fasterxml.jackson.annotation;
  .......
@Target({ElementType.ANNOTATION_TYPE, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@JacksonAnnotation
public @interface JsonAutoDetect{
    public enum Visibility {
        ANY, //all kinds of access modifiers 
        NON_PRIVATE, //other than 'private' 
        PROTECTED_AND_PUBLIC, //only 'protected' and 'public'
        PUBLIC_ONLY,//public only
        NONE, //no access modifiers;  to explicitly disable auto-detection for fields or methods
        DEFAULT;//default rules
   }
       ....
    Visibility getterVisibility() default Visibility.DEFAULT;  //for getXyz()
    Visibility isGetterVisibility() default Visibility.DEFAULT;  // for isXyz()
    Visibility setterVisibility() default Visibility.DEFAULT;// for setXyz()
    Visibility creatorVisibility() default Visibility.DEFAULT;// for constructor or factory methods
    Visibility fieldVisibility() default Visibility.DEFAULT; //for fields
   .....
}

Example

Following example specifies fieldVisibility=JsonAutoDetect.Visibility.ANY so that all access modifiers (including private) of the 'fields' can be detectable.

Java Object

@JsonAutoDetect(fieldVisibility = JsonAutoDetect.Visibility.ANY)
public class Employee {
  private String name;
  private String dept;
  private String address;

  public static Employee of(String name, String dept, String address) {
      Employee e = new Employee();
      e.name = name;
      e.dept = dept;
      e.address = address;
      return e;
  }

  @Override
  public String toString() {
      return "Employee{" +
              "name='" + name + '\'' +
              ", dept='" + dept + '\'' +
              ", address='" + address + '\'' +
              '}';
  }
}

Main class

package com.logicbig.example;

import com.fasterxml.jackson.databind.ObjectMapper;
import java.io.IOException;

public class ExampleMain {
  public static void main(String[] args) throws IOException {
      Employee employee = Employee.of("Trish", "Admin", "421 Moon Hill");
      ObjectMapper om = new ObjectMapper();
      String jsonString = om.writeValueAsString(employee);
      System.out.println(jsonString);
      Employee e = om.readValue(jsonString, Employee.class);
      System.out.println(e);
  }
}
{"name":"Trish","dept":"Admin","address":"421 Moon Hill"}
Employee{name='Trish', dept='Admin', address='421 Moon Hill'}

Without @JsonAutoDetect

If we remove @JsonAutoDetect from the Employee class, then following exception will be thrown (Employee class does not have getter/setters for default access to work):

Example Project

Dependencies and Technologies Used:

  • jackson-databind 2.9.6: General data-binding functionality for Jackson: works on core streaming API.
  • JDK 10
  • Maven 3.5.4

@JsonAutoDetect Example Select All Download
  • jackson-auto-detect-annotation
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • Employee.java

    See Also