Class GsonConverter

java.lang.Object
com.jummania.converter.GsonConverter
All Implemented Interfaces:
DataManager.Converter

public class GsonConverter extends Object implements 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

    Constructors
    Constructor
    Description
    Default constructor that initializes the Gson instance.
    GsonConverter (com.google.gson.Gson gson)
    Constructor that accepts a custom Gson instance.
  • Method Summary

    Modifier and Type
    Method
    Description
    <T> T
    fromJson (String json, Type typeOfT)
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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

      public <T> String toJson (T data)
      Converts a Java object to its JSON representation.
      Specified by:
      toJson in interface DataManager.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

      public <T> T fromJson (String json, Type typeOfT)
      Converts a JSON string into a Java object of the specified type.
      Specified by:
      fromJson in interface DataManager.Converter
      Type Parameters:
      T - the type of the object
      Parameters:
      json - the JSON string to be converted
      typeOfT - the type of the object to be returned
      Returns:
      the Java object represented by the JSON string
    • fromReader

      public <T> T fromReader (Reader json, Type typeOfT)
      Converts a JSON stream from a Reader into a Java object of the specified type.
      Specified by:
      fromReader in interface DataManager.Converter
      Type Parameters:
      T - the type of the object
      Parameters:
      json - the Reader containing the JSON data to be converted
      typeOfT - the type of the object to be returned
      Returns:
      the Java object represented by the JSON data from the Reader