Close

Jackson JSON - Using @JsonView Annotation to include/exclude group of properties to be serialized/deserialized

[Updated: Aug 24, 2018, Created: Aug 23, 2018]

@JsonView annotation is used for specifying one or more views for properties to be serialized or deserialized. Views are used to make groups of desired properties. Views are nothing but developer defined empty classes/interfaces; it's a way to specify a strongly type group.

Quick Example:

public interface ContactInfoView {}
public class Employee{
 private String name;
 @JsonView(ContactInfoView.class)
 private String phone;
 private String dept;
}
 Employee emp = ....
 ObjectMapper om = new ObjectMapper();
 om.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
 String jsonString = om.writerWithView(ContactInfoView.class).writeValueAsString(emp);
 System.out.println(jsonString);
{"phone":"123-123-123"}

Example

Views

public class Views {
  public interface QuickContact {}
  public interface SummaryView {}
}

POJO

public class Customer {
  @JsonView({Views.SummaryView.class, Views.QuickContact.class})
  private String name;
  @JsonView(Views.SummaryView.class)
  private String address;
  private String phone;
  @JsonView(Views.QuickContact.class)
  private String cellPhone;
  private String emailAddress;
  @JsonView(Views.SummaryView.class)
  @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy/MM/dd", timezone = "America/Chicago" )
  private Date customerSince;
    .............
}

Serializing QuickContactView

public class MainQuickContactViewSerialization {
  public static void main(String[] args) throws IOException {
      Customer customer = new Customer();
      customer.setName("Emily");
      customer.setAddress("642 Buckhannan Avenue Stratford");
      customer.setPhone("111-111-111");
      customer.setCellPhone("222-222-222");
      customer.setCustomerSince(Date.from(ZonedDateTime.now().minusYears(8).toInstant()));
      customer.setEmailAddress("emily@example.com");

      System.out.println("-- before serialization --");
      System.out.println(customer);

      ObjectMapper om = new ObjectMapper();
      om.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
      String jsonString = om.writerWithView(Views.QuickContact.class)
                   .writeValueAsString(customer);
      System.out.println("-- after serialization --");
      System.out.println(jsonString);
  }
}
-- before serialization --
Customer{name='Emily', address='642 Buckhannan Avenue Stratford', phone='111-111-111', cellPhone='222-222-222', emailAddress='emily@example.com', customerSince=Mon Aug 23 17:57:06 CDT 2010}
-- after serialization --
{"name":"Emily","cellPhone":"222-222-222"}

Serializing SummaryView

public class MainSummaryViewSerialization {
  public static void main(String[] args) throws IOException {
      Customer customer = new Customer();
      customer.setName("Emily");
      customer.setAddress("642 Buckhannan Avenue Stratford");
      customer.setPhone("111-111-111");
      customer.setCellPhone("222-222-222");
      customer.setCustomerSince(Date.from(ZonedDateTime.now().minusYears(8).toInstant()));
      customer.setEmailAddress("emily@example.com");

      System.out.println("-- before serialization --");
      System.out.println(customer);

      ObjectMapper om = new ObjectMapper();
      om.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
      String jsonString = om.writerWithView(Views.SummaryView.class)
                   .writeValueAsString(customer);
      System.out.println("-- after serialization --");
      System.out.println(jsonString);
  }
}
-- before serialization --
Customer{name='Emily', address='642 Buckhannan Avenue Stratford', phone='111-111-111', cellPhone='222-222-222', emailAddress='emily@example.com', customerSince=Mon Aug 23 18:00:57 CDT 2010}
-- after serialization --
{"name":"Emily","address":"642 Buckhannan Avenue Stratford","customerSince":"2010/08/23"}

Deserializing SummaryView

Views can be applied while deserializing as well:

public class MainSummaryViewDeserialization {
  public static void main(String[] args) throws IOException {
      String jsonString = "{\"name\":\"Emily\",\"address\":\"642 Buckhannan Avenue Stratford\","
              + "\"phone\":\"111-111-111\",\"cellPhone\":\"222-222-222\","
              + "\"emailAddress\":\"emily@example.com\",\"customerSince\":\"2010/08/23\"}";
      System.out.println("-- before deserialization --");
      System.out.println(jsonString);
      ObjectMapper om = new ObjectMapper();
      om.disable(MapperFeature.DEFAULT_VIEW_INCLUSION);
      Customer customer = om.readerWithView(Views.SummaryView.class)
                   .forType(Customer.class)
                   .readValue(jsonString);
      System.out.println("-- after deserialization --");
      System.out.println(customer);
  }
}
-- before deserialization --
{"name":"Emily","address":"642 Buckhannan Avenue Stratford","phone":"111-111-111","cellPhone":"222-222-222","emailAddress":"emily@example.com","customerSince":"2010/08/23"}
-- after deserialization --
Customer{name='Emily', address='642 Buckhannan Avenue Stratford', phone='null', cellPhone='null', emailAddress='null', customerSince=Mon Aug 23 00:00:00 CDT 2010}

Without any view

public class MainWithoutViewSerialization {
  public static void main(String[] args) throws IOException {
      Customer customer = new Customer();
      customer.setName("Emily");
      customer.setAddress("642 Buckhannan Avenue Stratford");
      customer.setPhone("111-111-111");
      customer.setCellPhone("222-222-222");
      customer.setCustomerSince(Date.from(ZonedDateTime.now().minusYears(8).toInstant()));
      customer.setEmailAddress("emily@example.com");

      System.out.println("-- before serialization --");
      System.out.println(customer);

      ObjectMapper om = new ObjectMapper();
      String jsonString = om.writeValueAsString(customer);
      System.out.println("-- after serialization --");
      System.out.println(jsonString);
  }
}
-- before serialization --
Customer{name='Emily', address='642 Buckhannan Avenue Stratford', phone='111-111-111', cellPhone='222-222-222', emailAddress='emily@example.com', customerSince=Mon Aug 23 18:00:58 CDT 2010}
-- after serialization --
{"name":"Emily","address":"642 Buckhannan Avenue Stratford","phone":"111-111-111","cellPhone":"222-222-222","emailAddress":"emily@example.com","customerSince":"2010/08/23"}

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

@JsonView Example Select All Download
  • jackson-json-view-annotation-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • Customer.java

    See Also