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.
Gson can work with arbitrary Java objects including pre-existing objects that you do not have a source code of. The following tutorials will demonstrate how you can leverage GSON to manage your JSON conversions.
Gson was originally created for use inside Google where it is currently used in a number of projects. It is now used by a number of public projects and companies.

Goals for Gson

  • Provide easy to use mechanisms like toString() and constructor (factory method) to convert Java to JSON and vice-versa
  • Allow pre-existing unmodifiable objects to be converted to and from JSON
  • Allow custom representations for objects
  • Support arbitrarily complex objects
  • Generate compact and readable JSON output

GSON Maven Dependency

To use Gson with Maven2/3, you can use the Gson version available in Maven Central by adding the following dependency:
<dependencies>
    <dependency>
      <groupId>com.google.code.gson</groupId>
      <artifactId>gson</artifactId>
      <version>2.8.5</version>
      <scope>compile</scope>
    </dependency>
</dependencies>

Two ways to create Gson objects

Gson object can be created in two ways. 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()
             .disableHtmlEscaping()
             .setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
             .setPrettyPrinting()
             .serializeNulls()
             .create();

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 its JSON representation.

  • Gson - Serializing Inner Classes Example
  • In this article, we will discuss how to serialization/deserialization of classes having inner classes.
  • 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.

Reference

Comments