Home > Java > javaTutorial > body text

How to serialize and deserialize JSON using ExclusionStrategy interface in Java?

PHPz
Release: 2023-08-20 21:53:24
forward
1213 people have browsed it

How to serialize and deserialize JSON using 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 register it with GsonBuilder using the setExclusionStrategies() method. It configures Gson to apply a set of exclusion policies during serialization and deserialization.

Syntax

public GsonBuilder setExclusionStrategies(ExclusionStrategy... strategies)
Copy after login

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 {
      <strong>Gson </strong>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 + "]";
   }
}
Copy after login

Output

Serialize a JSON:
{"lastName":"Sai","address":"Hyderabad"}
Deserialize a JSON:
Person [null Ramesh Hyderabad]
Copy after login

The above is the detailed content of How to serialize and deserialize JSON using ExclusionStrategy interface in Java?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template