
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Implement Custom JSON De-Serialization with Gson in Java
Deserialization is the process of converting JSON data back into Java objects. Gson provides a simple way to do this using the fromJson() method.
Custom JSON de-serialization with Gson
Gson is a Java library developed by Google to convert Java objects into their JSON format and vice versa. Custom JSON is a way we can modify or extend the standard JSON format so that it can suit our specific needs.
To use the Gson library, we need to add the Gson library to our project. If you are using Maven, add this to your pom.xml file:
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.8.9</version> </dependency>
If you are not using Maven, you can download the jar file from here. Follow the steps below to implement custom JSON de-serialization with Gson:
- First, import the Gson library, as discussed above.
- Then create a class Person with fields for name and age.
- Then create a custom deserializer class that implements the JsonDeserializer interface.
- In the custom deserializer, override the deserialize method to define how the JSON should be converted to the object.
- Then create an instance of GsonBuilder and register the custom deserializer using the registerTypeAdapter method.
- Finally, create a Gson instance and use it to deserialize the JSON string.
- Then create a JSON string.
- Then we will create an instance of the Gson class.
- Next, we will convert the JSON string to a Java object using the fromJson() method of Gson.
- Finally, we will print the Java object.
Example
Following is the code to implement custom JSON de-serialization with Gson:
import com.google.gson.Gson; import com.google.gson.GsonBuilder; import com.google.gson.JsonDeserializationContext; import com.google.gson.JsonDeserializer; import com.google.gson.JsonElement; public class CustomJsonDeSerializationExample { public static void main(String[] args) { // Create a JSON string String jsonString = "{"fullName":"Ansh", "yearsOld":23}"; // Create a custom deserializer for the Person class GsonBuilder gsonBuilder = new GsonBuilder(); gsonBuilder.registerTypeAdapter(Person.class, (JsonDeserializer<Person>) (json, typeOfT, context) -> { JsonElement jsonElement = json.getAsJsonObject().get("fullName"); String name = jsonElement.getAsString(); int age = json.getAsJsonObject().get("yearsOld").getAsInt(); return new Person(name, age); }); // Create a Gson instance with the custom deserializer Gson gson = gsonBuilder.create(); // Deserialize the JSON string to a Person object Person person = gson.fromJson(jsonString, Person.class); System.out.println(person); } }
Following is the output of the above code:
Person{name='Ansh', age=23}
Advertisements