Guide to google gson library
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.
- 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
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>
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();
- GSON - Serialize and Deserialize Primitives Types In this quick article, we will discuss how to use GSON to serialize or deserialize any primitive type into JSON representation.
- Convert Java Object to JSON using GSON In this article, we will create an example to converting or serializing Java object to JSON representation using the GSON library.
- Convert JSON to Java Object using GSON In this article, we will create an example to convert JSON representation to Java Object using the GSON library.
- GSON - Serialize and Deserialize Collections Example In this example, we serialize a collection of Integer and Employee objects into JSON representation and using the TypeToken to deserialize the collection of Integers into the arbitrary Java Object.
- 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 - Array and Multi-Dimensional Array Example In this quick article, we show you how to serialize and deserialize an array or a multidimensional array to and from its JSON representation.
- GSON - Custom Serialization and Deserialization Examples Many times, we need to write/read the JSON values which do not default representation of java object. In that case, Gson allows you to register your own custom serializer and deserializer.
- 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.