Book Image

Instant GSON

By : Sandeep Patel
Book Image

Instant GSON

By: Sandeep Patel

Overview of this book

GSON is a specialized Java-based library for handling JSON data, developed by Google. GSON demonstrates an efficient use of CPU time, memory efficiency, the library, and developer productivity. This book will help you implement GSON in your Java project. Instant GSON is a practical, hands-on guide that provides you with all the implementation features of the GSON API through coded examples, which will help you take advantage of the real power that is behind the GSON API, and give you a good grounding to use it in your projects. Instant GSON starts with an introduction to the JSON data format and GSON library. It will take you through the configuration steps required to set up a GSON-based Java project. You will learn about the top features of the GSON library, which includes handling generic type and nested class objects in Java, implementing versioning support, exclusion strategies, and pretty printing. At the end of the book you will find some useful references to articles, blogs, and active communities.
Table of Contents (7 chapters)

Quick start – creating your first JSON in GSON

In this section, you will learn different ways to instantiate GSON and their significance, followed by a quick example code that shows the basic serialization of wrapper type Java objects.

Step 1 – instantiating GSON

To use the GSON library, an object of the class needs to be instantiated. A GSON object does not maintain any state; this characteristic helps in reusing the GSON object at multiple places.

The GSON library provides two ways for instantiating it:

  • The default approach

  • The builder approach with settings

The default approach

In this approach, the GSON class object can be instantiated using the new keyword. This approach creates a gsonobject instance without any setting.

The builder approach

In this approach, a GSON class object can be created using the GsonBuilder class and the create method:

Gsongson = new GsonBuilder ().create ();

The preceding code calls the create method of GsonBuilder, which returns a Gson object for...