ExclusionStrategy example during deserialization of object

In this post under Gson, I will explain how we can exclude certain fields of a json data from being deserialized with an example.

Below is the Employee class structure that will be used to create an instance and its field are set with the values obtained from Employee.json file.

Employee class



public class Employee {
    private int id;
    private String name;
    private int ssn;
    private Boolean terminated;

    public int getId() {
        return id;
    }

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

    public String getName() {
        return name;
    }

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

    public int getSsn() {
        return ssn;
    }

    public void setSsn(int ssn) {
        this.ssn = ssn;
    }
    
    public Boolean isTerminated() {
        return terminated;
    }

    public void setTerminated(Boolean terminated) {
        this.terminated = terminated;
    }

    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("Id:" + id).append("\n");
        sb.append("Name:" + name).append("\n");
        sb.append("Terminated:" + terminated).append("\n");
        sb.append("ssn: " + ssn);
        
        return sb.toString();
    }
}

Below is the Employee json structure that we want to deserialize

Employee.json


{"id":1,"name":"name1","ssn":100,"terminated":true}

In the above json, we want to deserialize all the fields except field “ssn” and any field of type boolean.

To do that, we take help of com.google.gson.ExclusionStrategy interface.

We create a class named “FieldExclusionStrategy” that implements the interface as shown below

FieldExclusionStrategy


1  import com.google.gson.ExclusionStrategy;
2  import com.google.gson.FieldAttributes;
3  
4  public class FieldExclusionStrategy implements ExclusionStrategy {
5   @Override
6   public boolean shouldSkipClass(Class clazz) {
7       if(clazz.equals(Boolean.class)) {
8           System.out.println("Preventing boolean type field from serialization");
9           return true;
10      }
11      else {
12          return false;
13      }
14  }
15 
16  @Override
17  public boolean shouldSkipField(FieldAttributes fieldAttributes) {
18      if(fieldAttributes.getName().equalsIgnoreCase("ssn")) {
19          System.out.println("Preventing ssn field from serialization");
20          return true;
21      } else {
22          return false;
23      }
24  }
25 }

In the class “FieldExclusionStrategy”, to prevent a field of Class boolean we are using “shouldSkipClass” method. If this method returns true the field will be excluded from serialization.

In the same class “FieldExclusionStrategy”, to prevent a field with name “ssn” we are using “shouldSkipField” method. If this method returns true the field will be excluded from serialization.

Now we need to register this strategy with our Gson which is done as shown in the below code snippet.


gson = gsonBuilder.addDeserializationExclusionStrategy(fieldExclusionStrategy).create();

The above code snippet will create a Gson instance with the given strategy configured.

Below is the complete main code

Main Class


import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public class GsonDemo14 {
    public static void main(String[] args) {
        Employee employee = new Employee();
        employee.setId(1);
        employee.setName("name1");
        employee.setSsn(100);
        employee.setTerminated(true);
        
        Gson gson = new Gson();
        String result = gson.toJson(employee);
        System.out.println(result);
        
        employee = null;
        
        FieldExclusionStrategy fieldExclusionStrategy = new FieldExclusionStrategy();
        GsonBuilder gsonBuilder = new GsonBuilder();
        gson = gsonBuilder.addDeserializationExclusionStrategy(fieldExclusionStrategy).create();
        employee = gson.fromJson(result, Employee.class);
        System.out.println(employee);
    }
}

The output is as shown below

Output

{“id”:1,”name”:”name1″,”ssn”:100,”terminated”:true}
Preventing ssn field from serialization
Preventing boolean type field from serialization
Preventing boolean type field from serialization
Id:1
Name:name1
Terminated:null
ssn: 0

If you see the output the value of ssn field which is 100 in the json file is not set to corresponding employee instance field and the value of terminated field which is true in the json file is also not set to corresponding employee instance field.


Check these products on Amazon

Leave a Reply