
- 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 serialize and deserialize a JSON using the ExclusionStrategy interface in Java?
The ExclusionStrategy interface can be used to exclude any field during serialization and deserialization. We can provide a custom implementation of the ExclusionStrategy interface and need to register it with GsonBuilder using the setExclusionStrategies() method. It configures Gson to apply a set of exclusion strategies during serialization and deserialization.
Syntax
public GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies)
Example
import com.google.gson.*; import com.google.gson.ExclusionStrategy; import com.google.gson.FieldAttributes; public class ExclusionStrategyTest { public static void main(String args[]) throws Exception { Gson gson = new GsonBuilder().setExclusionStrategies(new CustomExclusionStrategy()).create(); Person person = new Person(); person.setFirstName("Adithya"); person.setLastName("Sai"); person.setAddress("Hyderabad"); String jsonString = gson.toJson(person); System.out.println("Serialize a JSON: \n "+jsonString); String inputJson = "{\"firstName\":\"Raja\", \"lastName\":\"Ramesh\", \"address\":\"Hyderabad\"}"; person = gson.fromJson(inputJson, Person.class); System.out.println("Deserialize a JSON:\n"+ person); } } // CustomExclusionStrategy class class CustomExclusionStrategy implements ExclusionStrategy { public boolean shouldSkipField(FieldAttributes f) { if(f.getName().equals("firstName")) { return true; } return false; } public boolean shouldSkipClass(Class aClass) { return false; } } // Person class class Person { private String firstName, lastName, address; public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String geLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } public String toString() { return "Person [" + firstName + " " + lastName + " " + address + "]"; } }
Output
Serialize a JSON: {"lastName":"Sai","address":"Hyderabad"} Deserialize a JSON: Person [null Ramesh Hyderabad]
- Related Articles
- How to serialize and deserialize an object in Java?
- How to deserialize a JSON to Java object using the flexjson in Java?
- How to deserialize a JSON string using @JsonCreator annotation in Java?
- ArduinoJSON: Serialize and Deserialize
- Serialize and Deserialize BST in Python
- Serialize and Deserialize Binary Tree in C++
- How to serialize a JSON object with JsonWriter using Object Model in Java?
- How to deserialize a JSON into an existing object in Java?
- Serialize and Deserialize N-ary Tree in C++
- How to deserialize a JSON array to list generic type in Java?\n
- How to serialize a JSON string to an Output Handler in Java?
- How to deserialize a JSON into Javascript object?
- How to serialize and de-serialize generic types using the Gson library in Java?\n
- How to serialize a Polyline object into JSON in FabricJS?
- How to de-serialize a Polyline object from JSON in FabricJS?

Advertisements