Gson

Gson, or Google Gson, is an open-source Java library that serializes Java objects to JSON (and deserializes them back to Java).

History
The Gson library was originally developed for internal purposes at Google, with Version 1.0 released on May 22, 2008, under the terms of the Apache License 2.0. The latest version, 2.10.1, was released on January 6, 2023.

Usage
Gson utilizes reflection, meaning that classes do not have to be modified to be serialized or deserialized. By default, a class only needs a defined default (no-args) constructor; however, this requirement can be circumvented (see Features).

The following example demonstrates the basic usage of Gson when serializing a sample object:

Calling the code of the above Main class will result in the following JSON output:



Since the Person's field age is marked as transient, it is not included in the output.

To deserialize the output produced by the last example, you can execute the code above, which generates the following output:

This shows how Gson can be used with the Java Platform Module System for the example above:

For more extensive examples, see Gson's usage guide on their GitHub repository.

Features

 * Gson can handle collections, generic types, and nested classes (including inner classes, which cannot be done by default).
 * When deserializing, Gson navigates the type tree of the object being deserialized, which means that it ignores extra fields present in the JSON input.
 * The user can:
 * write a custom serializer and/or deserializer so that they can control the whole process, and even deserialize instances of classes for which the source code is inaccessible.
 * write an InstanceCreator, which allows them to deserialize instances of classes without a defined no-args constructor.
 * Gson is highly customizable, as you can specify:
 * Compact/pretty printing (whether you want compact or readable output)
 * How to handle null object fields – by default they are not present in the output
 * Excluding fields - rules of what fields are intended to be excluded from deserialization
 * How to convert Java field names