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:
<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
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()
.disableHtmlEscaping()
.setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE)
.setPrettyPrinting()
.serializeNulls()
.create();
Using Gson and GsonBuilder Examples
- 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 of converting or serializing Java objects 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 use 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 their JSON representation.
- Gson - Serializing Inner Classes Example - In this article, we will discuss how to serialize/deserialize inner classes with an example.
- 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 the 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.
- How to Write JSON into File Using GSON in Java - In this example, we use the Gson tree model API to write Java objects into JSON files.
GitHub Repository
The source code of this tutorial hosted on my GitHub repository at https://github.com/RameshMF/gson-tutorial.
Comments
Post a Comment
Leave Comment