Google GSON Tutorial

Gson is a Java library that can be used to convert Java Objects into their JSON representation. It can also be used to convert a JSON string to an equivalent Java object.

Java GSON library

Gson is a Java serialization/deserialization library to convert Java Objects into JSON and back. Gson was created by Google for internal use and later open-sourced. It is now used by a number of public projects and companies.

Gson Video Tutorial

This video tutorial is explained in below YouTube video tutorial:

Goals for Gson

These are Gson features:
  • Simple tools for Java object JSON serialization and deserialization.
  • Extensive support of Java Generics.
  • Custom representations for objects.
  • Support for arbitrarily complex objects.
  • Fast with a low memory footprint.
  • It allows a compact output and pretty printing.

GSON Maven Dependency

To use Gson with Maven2/3, you can use the Gson version available in Maven Central by adding the following dependency:

Two ways to create Gson objects

A Gson object can be created in two ways. The first way gives you a quick Gson object ready for faster coding, while the second way uses GsonBuilder to build a more sophisticated Gson object.
//First way to create a Gson object for faster coding
Gson gson = new Gson();
//Second way to create a Gson object using GsonBuilder
Gson gson = new GsonBuilder()

Using Gson and GsonBuilder Examples

  • GSON - Serializing and Deserializing Generic Types - In this quick article, we will see how to serialize and deserialize a generic class using GSON. Generic type information is lost while serializing because of Java Type Erasure. You can solve this problem by specifying the correct parameterized type for your generic type. Gson provides this with the TypeToken class.

  • GSON - Serializing and Deserializing Enums - In this quick article, we show you how to serialize and deserialize enum types to and from their JSON representation.

  • Gson - Serializing Inner Classes Example - In this article, we will discuss how to serialize/deserialize inner classes with an example.
  • GSON - Excluding fields from JSON with @Expose Annotation - In this quick article, we will discuss how to mark certain fields of our Java objects to be excluded for consideration for serialization and deserialization to JSON.

  • GSON - Null Object Support - Gson by default generates optimized JSON content ignoring the NULL values. But GsonBuilder provides flags to show NULL values in the JSON output using the GsonBuilder.serializeNulls() method.

  • GSON - Version Support Example - In this quick article, we will discuss how to use @Since annotation to support multiple versions of the same object. GSON introduced the @Since annotation to support multiple versions of the same object. We can use this annotation on Classes and Fields.
  • Convert JSON to a Map Using Gson - In this quick tutorial, we’ll learn how to convert a JSON string to a Map using Gson from Google. We’ll see three different approaches to accomplish that and discuss their pros and cons – with some practical examples.

GitHub Repository

The source code of this tutorial hosted on my GitHub repository at