📘 Premium Read: Access my best content on Medium member-only articles — deep dives into Java, Spring Boot, Microservices, backend architecture, interview preparation, career advice, and industry-standard best practices.
✅ Some premium posts are free to read — no account needed. Follow me on Medium to stay updated and support my writing.
🎓 Top 10 Udemy Courses (Huge Discount): Explore My Udemy Courses — Learn through real-time, project-based development.
▶️ Subscribe to My YouTube Channel (172K+ subscribers): Java Guides on YouTube
Java Modifier Exclusion
import java.lang.reflect.Modifier;
Gson gson = new GsonBuilder()
.excludeFieldsWithModifiers(Modifier.STATIC)
.create();
Gson gson = new GsonBuilder()
.excludeFieldsWithModifiers(Modifier.STATIC, Modifier.TRANSIENT, Modifier.VOLATILE)
.create();
Gson's @Expose
Gson Maven Dependency
<dependencies>
<!-- Gson: Java to Json conversion -->
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.5</version>
<scope>compile</scope>
</dependency>
</dependencies>
User Class - Object to be Serialized/Deserialized
package net.javaguides.gson;
import com.google.gson.annotations.Expose;
/**
*
* @author Ramesh Fadatare
*
*/
public class User {
private int id;
private String firstName;
private String lastName;
@Expose
private int age;
@Expose
private String gender;
@Expose
private String password;
public int getId() {
return id;
}
public void setId(int i) {
this.id = i;
}
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", age=" + age + ", gender=" +
gender + "]";
}
}
Excluding field from JSON using @Expose GSON
package net.javaguides.gson;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
/**
*
* @author Ramesh Fadatare
*
*/
public class GSONExcludingFieldsExample {
public static void main(String[] args) {
Gson gson = new GsonBuilder().setPrettyPrinting().create();
User user = new User();
user.setFirstName("Ramesh");
user.setLastName("Fadatare");
user.setGender("Male");
user.setAge(28);
user.setId(100);
user.setPassword("secret");
// Serialization without excludeFieldsWithoutExposeAnnotation() method
String userJson = gson.toJson(user);
System.out.println(userJson);
// Serialization with excludeFieldsWithoutExposeAnnotation() method
gson = new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setPrettyPrinting().create();
String result = gson.toJson(user);
System.out.println("-------- Serialization using @Expose annotation -----");
System.out.println(result);
}
}
{
"id": 100,
"firstName": "Ramesh",
"lastName": "Fadatare",
"age": 28,
"gender": "Male",
"password": "secret"
}
-------- Serialization using @Expose annotation -----
{
"age": 28,
"gender": "Male",
"password": "secret"
}
Comments
Post a Comment
Leave Comment