Javatpoint Logo
Javatpoint Logo

Methods of Gson in Java

In Java, Gson is defined as a library which was developed by the Google for serialization and deserialization of JSON (JavaScript Object Notation). The main purpose of Gson in Java is converting objects of Java into formats of JSON and vice versa.

Let's understand in detail about Gson and its methods in Java.

Gson in Java

Gson is considered as one of the powerful library that was created by the Google for the main purpose of maintenance serialization and deserialization of JSON. The process of conversion of objects of Java into the formats of JSON and vice versa are made simple with the help of Gson. This is the reason it is a popular option for users when dealing with data related to JSON in the applications of Java.

The main class in Gson can be labelled as the "Gson" class. This class is responsible for providing methods which can be utilized by users for serialization and deserialization. Let us understand about about Gson in Java in detail with the help of a Java example program.

Example 1:

Filename: GsonClassExample.java

Example 2:

Filename: GsonClass.java

Now let us look at few methods of Gson in Java and their functionalities.

Methods of Gson in Java

There are several methods available in the Gson class of the Java. Let us understand about each method in detail and their functionality.

Method Functionality
toJson( Object o) This method serializes and converts specific object into the similar representation of JSON.
toJson( JsonElement j) This method is used for serializing a JSON element and convert it into a similar representation of JSON.
fromJson( String s, Class C) This method deserializes the specific string of JSON and converts it into an object of specific class.
fromJson( JsonElement j, Class c) This method deserializes a Json element and converts it into an object of the specific class.
fromJson( Reader r, Class C) This method deserializes a string of JSON from the Reader and converts it into an object of the specific class.
toJsonTree( Object o) This method is used for converting the specific object and converting it into an element of JSON.
toJson( JsonElement j, Appendable w) This method is used for serializing an Json element and converting it into a specific Appendable.
fromJson( String S, Type t) This method deserializes the required string of JSON and converts into an object of the required type.
fromJson( JsonElement j , Type t) This method deserializes the Json element and converts it into an object of the required type.
fromJson( Reader r, Type t) This method deserializes the string from a Reader and converts it into an object of the required type.
fromJson( Reader r, TypeToken TT) This method deserializes a string of Json from the Reader and converts it into an object with the help of a TypeToken.
fromJson( JsonElement j, TypeToken TT) This method deserializes a Json element and converts it into an object with the help of TypeToken.
fromJson( String s, TypeToken TT) This method deserializes the string of the Json and converts it into an object with the help of TypeToken.
fromJson( JsonElement j, ParameterizedType PT) This method deserializes an Json element and converts it into an object of the required parameterized type.

These are few of the methods of Gson in Java and their purpose or functionalities have been explained in detail. It was all the required explanation about Gson and methods of Gson in Java with examples.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA