Gson Builder — How to Ignore Fields with @Expose

In our previous tutorials, we've always assumed you want to (de)serialize all properties of a model. This might not be the case. If you want to customize (de)serialization keep reading!

Of course, this will not be the only post in our Gson series. If you're interested in the other topics, check out our series outline:

Gson Series Overview

Model Annotation @Expose

After reading the previous tutorial on null handling, you could think that you would just set a Java object field to null, if you don't want it to show up in the JSON. This can be necessary when you don't want to transfer private or sensitive data across the network. No worries, Gson offers a simpler solution with the annotation @Expose.

@Expose is optional and offers two configuration parameters: serialize and deserialize. By default everything is set to true. Thus, if you don't enhance any fields with @Expose, like we did in the first tutorial, everything will be included. If you set the @Expose, but don't set either value specifically to false, it still will be included.

Let's revisit the UserSimple class and add some @Expose annotations:

public class UserSimple {  
    @Expose()
    String name; // equals serialize & deserialize

    @Expose(serialize = false, deserialize = false)
    String email; // equals neither serialize nor deserialize

    @Expose(serialize = false)
    int age; // equals only deserialize

    @Expose(deserialize = false)
    boolean isDeveloper; // equals only serialize
}

The example above would result during the serialization that only the name and the isDeveloper flag will show up in the JSON. The other two fields, even if they're set, will not be converted.

In the other direction of deserialization, the Java object would only have the JSON values of the name and the age field, email and isDeveloper would be ignored.

@Expose will not be regarded by the default Gson instance. In order to utilize it, you'll need to use a custom Gson instance:

GsonBuilder builder = new GsonBuilder();  
builder.excludeFieldsWithoutExposeAnnotation();  
Gson gson = builder.create();  

All following uses of gson will respect the @Expose annotation.

@Expose is an easy way to control which values get (de)serialized. We recommend to not use @Expose if you want everything to be transformed. It only blows up your model classes.

Transient

An alternative to using @Expose is to declare a field as transient. A transient field will not be (de)serialized either. However, you don't have full control like you do with @Expose. You cannot deactivate one direction, transient will always completely turn off the conversion for that property.

public class UserSimple {  
    String name;
    String email;
    int age;
    boolean transient isDeveloper; // will not be serialized or deserialized
}

Outlook

In this tutorial, you've how you can configure @Expose to exclude and include model properties during (de)serialization. You might need it in your daily work, so make sure to understand it properly.

If you've feedback or a question, let us know in the comments or on twitter @futurestud_io.

Make it rock & enjoy coding!

Explore the Library

Find interesting tutorials and solutions for your problems.