Package com.jummania.converter
Class GsonConverter
java.lang.Object
com.jummania.converter.GsonConverter
- All Implemented Interfaces:
DataManager.Converter
GsonConverter is an implementation of the Converter interface
that utilizes the Gson library for converting Java objects
to JSON format and vice versa.
This class provides methods to serialize Java objects to JSON strings and to deserialize JSON strings back into Java objects using the Gson library.
Created by Jummania on 08, December, 2024. Email: sharifuddinjumman@gmail.com Dhaka, Bangladesh.
-
Constructor Summary
ConstructorsConstructorDescriptionDefault constructor that initializes the Gson instance.GsonConverter
(com.google.gson.Gson gson) Constructor that accepts a custom Gson instance. -
Method Summary
Modifier and TypeMethodDescription<T> T
Converts a JSON string into a Java object of the specified type.<T> T
fromReader
(Reader json, Type typeOfT) Converts a JSON stream from a Reader into a Java object of the specified type.<T> String
toJson
(T data) Converts a Java object to its JSON representation.
-
Constructor Details
-
GsonConverter
public GsonConverter()Default constructor that initializes the Gson instance. -
GsonConverter
public GsonConverter(com.google.gson.Gson gson) Constructor that accepts a custom Gson instance.- Parameters:
gson
- the Gson instance to be used for conversions
-
-
Method Details
-
toJson
Converts a Java object to its JSON representation.- Specified by:
toJson
in interfaceDataManager.Converter
- Type Parameters:
T
- the type of the object- Parameters:
data
- the Java object to be converted to JSON- Returns:
- a JSON string representing the object
-
fromJson
Converts a JSON string into a Java object of the specified type.- Specified by:
fromJson
in interfaceDataManager.Converter
- Type Parameters:
T
- the type of the object- Parameters:
json
- the JSON string to be convertedtypeOfT
- the type of the object to be returned- Returns:
- the Java object represented by the JSON string
-
fromReader
Converts a JSON stream from a Reader into a Java object of the specified type.- Specified by:
fromReader
in interfaceDataManager.Converter
- Type Parameters:
T
- the type of the object- Parameters:
json
- the Reader containing the JSON data to be convertedtypeOfT
- the type of the object to be returned- Returns:
- the Java object represented by the JSON data from the Reader
-