Java Gson exclude fields with modifiers

Tags: JSON gson Gson toJson GsonBuilder excludeFieldsWithModifiers Gson exclude fields

In this tutorial we learn how to configure Gson to exclude fields during serialization or deserialize JSON to exclude fields with specific access modifiers. Via step by step example we will show you how to use the GsonBuilder.excludeFieldsWithModifiers() method to override Gson configuration to exclude fields by given modifiers.

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 with modifiers

For example, we have a Java class named Contact as below.

Contact.java

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

    public String contactType = "TYPE_1";

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

You can notice that in the Contact class we have a public field contactType.

If we use the default configuration of Gson to serialize a Contact object to JSON String then we have the result as below.

ExcludeFieldsExample1.java

import com.google.gson.Gson;

public class ExcludeFieldsExample1 {
    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);

        Gson gson = new Gson();
        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,"address":"123, test street","contactType":"TYPE_1"}

By default, Gson will exclude all fields marked transient or static. In the following Java example we show you how to override default configure and exclude public fields to exclude contactType field of Contact class above.

ExcludeFieldsExample2.java

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

import java.lang.reflect.Modifier;

public class ExcludeFieldsExample2 {
    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
                .excludeFieldsWithModifiers(Modifier.PUBLIC)
                .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,"address":"123, test street"}

Happy Coding 😊