
- 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 de-serialize generic types using the Gson library in Java?n
If a Java class is a generic type and we are using it with the Gson library for JSON serialization and deserialization. The Gson library provides a class called com.google.gson.reflect.TypeToken to store generic types by creating a Gson TypeToken class and pass the class type. Using this type, Gson can able to know the class passed in the generic class.
Syntax
public class TypeToken<T> extends java.lang.Object
Example
import java.lang.reflect.Type; import java.util.*; import com.google.gson.*; import com.google.gson.reflect.*; public class GenericTypesJSONTest { public static void main(String[] args) { Gson gson = new GsonBuilder().setPrettyPrinting().create(); List<String> list = Arrays.asList("INDIA", "AUSTRALIA", "ENGLAND", "SOUTH AFRICA"); String jsonStr = gson.toJson(list); System.out.println(jsonStr); Type listType = new TypeToken<List<String>>() {}.getType(); list = gson.fromJson(jsonStr, listType); System.out.println(list); } }
Output
[ "INDIA", "AUSTRALIA", "ENGLAND", "SOUTH AFRICA" ] [INDIA, AUSTRALIA, ENGLAND, SOUTH AFRICA]
- Related Articles
- How to serialize a null field using Gson library in Java?
- How to serialize a map using the flexjson library in Java?\n
- How to serialize the order of properties using the Jackson library in Java?
- How to de-serialize a Polyline object from JSON in FabricJS?
- How to serialize and deserialize a JSON using the ExclusionStrategy interface in Java?
- How to serialize and deserialize an object in Java?
- How to serialize a lambda function in Java?
- How to serialize a Polygon object using FabricJS?
- How to format a date using the Gson library in Java?
- How to pretty print JSON using the Gson library in Java?
- How to use @Until annotation using the Gson library in Java?
- ArduinoJSON: Serialize and Deserialize
- How to serialize a JSON object with JsonWriter using Object Model in Java?
- How to convert Java object to JSON using GSON library?
- How can we serialize an array of objects using flexjson in Java?

Advertisements