Java Gson exclude fields using ExclusionStrategy interface

Tags: JSON gson Gson toJson GsonBuilder setExclusionStrategies Gson exclude fields ExclusionStrategy

In this Java Gson tutorial we learn how to exclude fields during serialization or deserialize JSON by using the ExclusionStrategy interface. Via step by step tutorial we will show you how to use the GsonBuilder.setExclusionStrategies() method and com.google.gson.ExclusionStrategy interface to configure Gson to exclude fields based on custom implementation.

How to add Gson to the Java project

To use the Gson library in the Gradle build project, add the following dependency into the build.gradle file.

implementation group: 'com.google.code.gson', name: 'gson', version: '2.8.7'

To use the Gson library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.8.7</version>
</dependency>

Or you can download the Gson jar file from Maven Central at gson-2.8.7.jar

To have more information about the Gson library you can visit the project repository at github.com/google/gson

How to exclude fields using ExclusionStrategy interface

For example, we have a Contact class as below.

Contact.java

public class Contact {
    private String firstName;
    private String lastName;
    private String email;
    private int age;
    private String address;

    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 String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

The following code we show you how to implement a custom class that implements the ExclusionStrategy interface to customize the exclusion. For example we want to exclude the address field.

CustomExclusionStrategy.java

import com.google.gson.ExclusionStrategy;
import com.google.gson.FieldAttributes;

public class CustomExclusionStrategy implements ExclusionStrategy {

    @Override
    public boolean shouldSkipField(FieldAttributes f) {
        if("address".equals(f.getName())) {
            return true;
        }

        return false;
    }

    @Override
    public boolean shouldSkipClass(Class<?> clazz) {
        return false;
    }
}

The following Java example program we use the CustomExclusionStrategy class above to exclude address field during convert Contact object to JSON String.

ExclusionStrategyExample.java

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

public class ExclusionStrategyExample {
    public static void main(String... args) {
        Contact contact = new Contact();
        contact.setFirstName("Simple");
        contact.setLastName("Solution");
        contact.setEmail("contact@simplesolution.dev");
        contact.setAddress("123, test street");
        contact.setAge(25);

        GsonBuilder gsonBuilder = new GsonBuilder();
        Gson gson = gsonBuilder
                .setExclusionStrategies(new CustomExclusionStrategy())
                .create();
        String jsonString = gson.toJson(contact);

        System.out.println("Output JSON: ");
        System.out.println(jsonString);
    }
}
The output is:
Output JSON: 
{"firstName":"Simple","lastName":"Solution","email":"contact@simplesolution.dev","age":25}

Happy Coding 😊