
- 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 implement custom JSON de-serialization with Gson in Java?n
A Gson library provides a way to specify custom de-serializers by registering a custom de-serializer with the GsonBuilder if we need a way to convert a java object to JSON. We can create a custom de-serializer by overriding the deserialize() method of com.google.gson.JsonDeserializer class.
In the below example, the implementation of custom de-serialization of JSON.
Example
import java.lang.reflect.Type; import com.google.gson.*; public class CustomJSONDeSerializerTest { public static void main(String[] args) { Gson gson = new GsonBuilder().registerTypeAdapter(Password.class, new PasswordDeserializer()).setPrettyPrinting().create(); String jsonStr = "{" + "\"firstName\": \"Adithya\"," + "\"lastName\": \"Sai\"," + "\"age\": 25," + "\"address\": \"Pune\"," + "\"password\": \"admin@123\"" + "}"; Student student = gson.fromJson(jsonStr, Student.class); System.out.println(student.getPassword().getPassword()); } } class PasswordDeserializer implements JsonDeserializer { @Override public Password deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException { String ecryptedPwd = json.getAsString(); return new Password(new StringBuffer(ecryptedPwd).toString()); } } // Student class class Student { private String firstName; private String lastName; private int age; private String address; private Password password; public Student(String firstName, String lastName, int age, String address) { super(); this.firstName = firstName; this.lastName = lastName; this.age = age; this.address = address; } 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 getAddress() { return address; } public void setAddress(String address) { this.address = address; } public Password getPassword() { return password; } public void setPassword(Password password) { this.password = password; } public String toString() { return "Student[ " + "firstName = " + firstName + ", lastName = " + lastName + ", age = " + age + ", address = " + address + " ]"; } } // Password class class Password { private String password; public Password(String password) { super(); this.password = password; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } }
Output
admin@123
- Related Articles
- How to implement custom JSON serialization with Gson in Java?
- How to implement custom FieldNamingStrategy using Gson in Java?
- How to implement custom JsonAdapter using Gson in Java?
- How to exclude a field in Gson during serialization in Java?
- How to parse a JSON to Gson Tree Model in Java?
- How to convert Java object to JSON using GSON library?
- Custom instance creator using Gson in Java?
- How to rename the properties of JSON using Gson in Java?
- How to pretty print JSON using the Gson library in Java?
- How to control serialization through @JSON annotation using flexjson in Java?\n
- How to parse a JSON without duplicate keys using Gson in Java?
- How can we ignore the fields during JSON serialization in Java?
- How to implement a custom Python Exception with custom message?
- Convert JSON object to Java object using Gson library in Java?\n
- Convert Java object to JSON using the Gson library in Java?\n

Advertisements