
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Exclude Field from JSON Using Expose Annotation in Java
Sometimes we need to exclude certain fields from the JSON output. For example, if we have a field that contains sensitive information, we may not want to include it in the JSON representation of the object. In such cases, we can use the Expose annotation provided by Gson.
The Gson @Expose annotation
The Gson @Expose annotation can be used to specify whether a field isto be exposed or not (included or not). The @Expose annotation can take two parameters, and each parameter is a boolean which can take either the value true or false. The two parameters are:
- serialize: Specifies if the field should be part of the JSON output. Set to true to include it, or false to exclude it.
- deserialize: Determines if the field should be part of the JSON input. Set to true to include it, or false to exclude it.
- Defaults for both parameters are true.
In order to get GSON to react to the @Expose annotations, we must create a Gson instance using the GsonBuilder class and need to call the excludeFieldsWithoutExposeAnnotation() method, which configures Gson to exclude all fields from consideration for serialization or deserialization that do not have the Expose annotation.
Now, let's see what we need to use the Gson library. If you are using Maven, add this to your pom.xml file:
<dependency> <groupId>com.google.code.gson <artifactId>gson <version>2.8.9 </dependency>
If you do not use Maven, you can download the jar file from here.
Excluding a Field from JSON using @Expose
Following are the steps to exclude a field from JSON using @Expose annotation in Java:
- Import Gson, GsonBuilder, and the
@Expose
annotation. - Create a class, like Person, with some fields.
- Use
@Expose
only on the fields you want to include in the JSON. - Leave out
@Expose
for fields you want to skip. - In your main code, create a
GsonBuilder
and callexcludeFieldsWithoutExposeAnnotation()
. - Use
GsonBuilder.create()
to get theGson
object. - Use
toJson()
to convert your object to JSON. - The JSON will only contain fields marked with
@Expose
.
Example
Following example demonstrates how to exclude a field from JSON using the @Expose annotation in Java. In here, we will create a class Person with fields for name, age, and email. We will use the @Expose annotation to exclude the email field from the JSON output.
import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.annotations.Expose; public class ExposeExample { public static void main(String[] args) { Gson gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create(); Person p = new Person("Ansh", 23, "ansh@example.com"); String json = gson.toJson(p); System.out.println(json); } } class Person { @Expose private String name; @Expose private int age; private String email; public Person(String name, int age, String email) { this.name = name; this.age = age; this.email = email; } }
Following is the output of the above code:
{ "name": "Ansh", "age": 23 }