GSON Annotations Example

Gson Annotations Example shows you some useful annotations that are provided by Google Gson, for example: @Expose, @SerializedName, @Since, @Until, and @JsonAdapter. With these annotations, we can customize the serialization of java object to JSON or desirialization of object from JSON.

Other interesting posts you may like

Table of contents:
1. Project structure
2. Maven Dependency
3. Create POJO with GSON annotations
4. Create Main class using @Expose for serialization/deserialization
5. Create Main class using Version for serialization/deserialization

Project structure
We are creating project with structure like below:

Gson-Annotation-Example

Maven Dependency

We will use the latest GSON version in this GSON Annotations Example

Create POJO with GSON annotations
We create java class with GSON annotaions like below:

Let’s dig deeper:

@Expose: this annotation is used to serialize/deserialize a field. This annotation has no effect unless you build Gson with a GsonBuilder and invoke GsonBuilder.excludeFieldsWithoutExposeAnnotation() method.

@SerializedName: this annotation is used to serialize a field with different name instead of using actual field name.

Gson Custom Serialization
Sometimes, we need to customize behavior of default serialization/deserialization for our specified requirement. We can do this by writing custom JsonSerializer and JsonDeserializer implementation and registering them with GsonBuilder.

In this Gson Annotations Example, we will customize the importedDate field. We create the MySerializerDate class for serialization like this:

and the MyDeserializerDate for deserialization like below:

Create Main class using @Expose for serialization/deserialization

We create main class to see how two above annotations work, it looks like below:

Run the above main on Eclipse, we will get the output like screen shot:

Gson Annotations Example Expose

@Since: this annotation is used to manage versioning of Json classes. We can serialize/deserialize a field based on its certain version. @Since annotation and version number indicate a field that we can only serialize/deserialize that field starting that version number. Otherwise, before that version, it will ignored.

@Until: this annotation is used to manage versioning of Json classes. We can serialize/deserialize a field based on its certain version. @Util annotation and version number indicate a field that we can only serialize/deserialize that field util certain version. Otherwise, after that version, it will ignored.

Create Main class using Version for serialization/deserialization
Here, We create other main class for serialization/deserialization using version. Simply, we just set version number to GSON builder object like below:

Running the above main we will get the output like below:

Gson Annotations Example Util Since

That’s all. Now You can create your own Gson Annotations Example by following all the above steps.

Download source code, click link below

Gson-Annotation-Example.zip

Leave a Comment

*

Please share it if you found this useful
Hide Buttons