
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to exclude a field from JSON using @Expose annotation in Java?
The Gson @Expose annotation can be used to mark a field to be exposed or not (included or not) for serialized or deserialized. The @Expose annotation can take two parameters and each parameter is a boolean which can take either the value true or false. 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, it configures Gson to exclude all fields from consideration for serialization or deserialization that do not have the Expose annotation.
Syntax
public GsonBuilder excludeFieldsWithoutExposeAnnotation()
Example
import com.google.gson.*; import com.google.gson.annotations.*; public class JsonExcludeAnnotationTest { public static void main(String args[]) { Employee emp = new Employee("Raja", 28, 40000.00); Gson gson = new GsonBuilder().setPrettyPrinting().create(); String jsonStr = gson.toJson(emp); System.out.println(jsonStr); gson = new GsonBuilder().setPrettyPrinting().excludeFieldsWithoutExposeAnnotation().create(); jsonStr = gson.toJson(emp); System.out.println(jsonStr); } } // Employee class class Employee { @Expose(serialize = true, deserialize = true) public String name; @Expose(serialize = true, deserialize = true) public int age; @Expose(serialize = false, deserialize = false) public double salary; public Employee(String name, int age, double salary) { this.name = name; this.age = age; this.salary = salary; } }
Output
{ "name": "Raja", "age": 28, "salary": 40000.0 } { "name": "Raja", "age": 28 }
- Related Articles
- How to deserialize a JSON string using @JsonCreator annotation in Java?
- How to convert a bean to JSON object using Exclude Filter in Java?
- How to control serialization through @JSON annotation using flexjson in Java?\n
- How to exclude a field in Gson during serialization in Java?
- How to get a JSON field in a nested JSON using Rest Assured?
- How to get a JSON array field in a nested JSON using Rest Assured?
- How can we change a field name in JSON using Jackson in Java?
- How to ignore a field of JSON object using the Jackson library in Java?\n
- How to exclude array type field value in MongoDB?
- How to implement custom serializer using @JsonSerialize annotation in Java?
- How to wrap a JSON using flexjson in Java?
- How to convert a Collection to JSON Array using JSON-lib API in Java?
- How to convert a Map to JSON object using JSON-lib API in Java?
- How to convert a JSON string to a bean using JSON-lib API in Java?
- How to exclude a test class from a testing suite using testng.xml?

Advertisements