Saturday 1 February 2020

Jackson: JsonView Annotation example


In this post, I am going to explain how to use Jackson JsonView to serialize and deserialize the objects.

Step 1: Create a class to represent views.
package com.sample.app.model;

public class Views {
 public static class Private {
 }

 public static class Public extends Private {
 }

}

Step 2: Define model class and annotate the fields with corresponding views.


Employee.java
package com.sample.app.model;

import com.fasterxml.jackson.annotation.JsonView;

public class Employee {
 @JsonView(Views.Private.class)
 private int id;

 @JsonView(Views.Public.class)
 private String firstName;

 @JsonView(Views.Public.class)
 private String lastName;

 @JsonView(Views.Private.class)
 private double salary;

 public Employee() {

 }

 public Employee(int id, String firstName, String lastName, double salary) {
  this.id = id;
  this.firstName = firstName;
  this.lastName = lastName;
  this.salary = salary;
 }

 public int getId() {
  return id;
 }

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

 public String getFirstName() {
  return firstName;
 }

 public void setFirstName(String firstName) {
  this.firstName = firstName;
 }

 public String getLastName() {
  return lastName;
 }

 public void setLastName(String lastName) {
  this.lastName = lastName;
 }

 public double getSalary() {
  return salary;
 }

 public void setSalary(double salary) {
  this.salary = salary;
 }

}

As you see Employee class, I annotated firstName, lastName with Public view and id, salary with private view.

@JsonView(Views.Private.class)
private int id;

@JsonView(Views.Public.class)
private String firstName;

@JsonView(Views.Public.class)
private String lastName;

@JsonView(Views.Private.class)
private double salary;

Since Public view extends Private view, when you serialize with Public view, it takes all the properties that are annotated with either Public or Private.

Step 3: Specify the view while serializing the data.
String json = objectMapper.writerWithView(Views.Private.class).writeValueAsString(emp1);
json = objectMapper.writerWithView(Views.Public.class).writeValueAsString(emp1);

App.java
package com.sample.app;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.sample.app.model.Employee;
import com.sample.app.model.Views;

public class App {
 public static void main(String args[]) throws JsonProcessingException {
  Employee emp1 = new Employee(1, "Gopi", "Gurram", 5000000);

  ObjectMapper publicObjMapper = new ObjectMapper();
  publicObjMapper.writerWithView(Views.Public.class);

  ObjectMapper objectMapper = new ObjectMapper();

  String json = objectMapper.writerWithView(Views.Private.class).writeValueAsString(emp1);
  System.out.println("Data with private view");
  System.out.println(json);

  json = objectMapper.writerWithView(Views.Public.class).writeValueAsString(emp1);
  System.out.println("\nData with public view");
  System.out.println(json);
 }
}

Run App.java, you will see below messages in console.

Data with private view
{"id":1,"salary":5000000.0}

Data with public view
{"id":1,"firstName":"Gopi","lastName":"Gurram","salary":5000000.0}







Previous                                                    Next                                                    Home

No comments:

Post a Comment