Google Json – Gson is an open source Java library that can be used to serialize or deserialize Java Objects to (or from) JSON representation. It provides support for converting pre-existing unmodifiable objects – which you don’t have the source code of -, It has extensive support for Java Generics. Allows custom representations for objects and supports arbitrarily complex objects with deep inheritance hierarchies and extensive use of generic types. The following tutorials will demonstrate how you can leverage GSON to manage your JSON conversions.
<dependency> <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> <version>2.6.2</version> </dependency>
<dependency org="com.google.code.gson" name="gson" rev="2.6.2"/>
- Gson Streaming API – Reading and Writing
Gson Streaming API is used to process large JSON objects and is available via the JsonReader and JsonWriter classes. This streaming approach is very useful in situations where it is not desirable to load complete object model in memory, because of the danger of getting an out of memory exception when reading your JSON document.
- Gson Streaming API Mixing With Object-Model
This example we’ll mix GSON Streaming API together with GSON Object Model. This leaves us with the benefit of not loading the entire JSON document in memory but small portions of the document and it’s very concise since it uses GSON Object Model.
- Gson Tree Model – Write and Parse to and from JSON
Gson Tree Model API provides support to read and parse a JSON String into a Tree Model. We can use the JsonParser to parse the JSON string into a Tree Model of type JsonElement.
- Serialize and Deserialize Primitives
GSON can serialize andy primitive type into their JSON representation and deserialize any JSON string into their arbitrary Java Object.
- Serialize and Deserialize Collections
GSON can serialize and Deserialize collections. In this example we serialize a collection of Integer into JSON representation and using the TypeToken to deserialize the collection of Integers into the arbitrary Java Object.
- Serialize and Deserialize Arrays and Multi Dimensional Arrays
Arrays are a very basic structure and can be serialized or deserialized using GSON API. Here we show you how to serialize and deserialize and array or a multi dimensional array to and from its JSON representation.
- Serializing and Deserializing Enums
Gson can serialize and deserialize enums using the @SerializedName annotation. If we annotate an enum with the @SerializedName we can supply a value which’ll be mapped when gson serializes or deserializes the json to or from Java Object.
- Serialize Java Object to JSON
We get familiar with GSON. See which dependencies are required to run Google GSON. Finally we show you how to serialize a Java Object into its JSON representation.
- Deserialize JSON to Java Object
Next we look at how you can deserialize a JSON string into the arbitrary Java Object.
- Serializing and Deserializing Generic Types
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.
- Custom Serialization and Deserialization
GSON Custom serialization and deserialization is often necessary when working with library classes like date and time. Gson allows you to register your own custom serializers and deserializers.
- Custom JSON Field Naming
Gson supports custom naming for JSON fields. You can annotate your fields with @SerializedName to provide this functionality. If there is an error in the naming of the field, it’ll throw a Runtime exception. In this tutorial we will show you how you can change the name of a field inside your Java Class.
- Format date in JSON object
You can globally specify how to format dates in JSON objects using GSON. Very easy, you will only have to declare your custom date once. GSON Format Date method setDateFormat() witch takes an int a pattern or dateStyle and timeStyle which are both primitive int types.
- Excluding field from JSON representation
GSON provides a way where you can mark certain fields of your objects to be excluded for consideration for serialization and deserialization to JSON. To use this @Expose GSON annotation, you must create Gson by using new GsonBuilder().excludeFieldsWithoutExposeAnnotation().create(). The Gson instance created will exclude all fields in a class that are not marked with @Expose annotation.
- Excluding fields from JSON using modifiers
By default, if you mark a field as transient, it will be excluded. As well, if a field is marked as “static” then by default it will be excluded. Here is another way you can exclude fields from GSON using modifiers.
- Dynamically exclude fields from JSON
In this tutorials we show you how to dynamically exclude JSON fields with GSON. When you have a use case that you want to exclude a field for whatever reason. You can create a custom JsonSerializer. With this serializer you can modify your exposed json object dynamically.
- Versioning Support
GSON introduced the @Since annotation to support multiple versions of the same object. We can use this annotation on Classes and Fields. In order to enable this feature we need to register a version number which to use when serializing/deserializing.