ExclusionStrategy example during serialization of object

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

Below is the Employee class structure that we want to serialize

Employee


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();
    }
}

In the above class, we want to serialize 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


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 type 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 gson = gsonBuilder.addSerializationExclusionStrategy(fieldExclusionStrategy).create();

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

Below is the complete main code


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

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

The output is as shown below

Output

Preventing ssn field from serialization
Preventing boolean type field from serialization
{“id”:1,”name”:”name1″}

As you can see from the output ssn and boolean field is excluded from serialization.

Leave a Reply