Welcome to Instant GSON. This book has been especially created to provide you with all the information that you need to get started with GSON. You will learn the basics of GSON, get started with building JSON representation, and discover some tips and tricks for coding GSON.
This book contains the following sections:
So, what is GSON? helps you find out what GSON actually is, what you can do with it, and why it's so great.
Configuring GSON in Eclipse will help you configure the GSON library in Eclipse IDE for a simple Java project and a Maven type Project.
Quick start – creating your first JSON in GSON will show you how to perform one of the core tasks of GSON: creating courses. Follow the examples discussed in this section to get started with GSON, which will be the common code of most of the GSON use.
Top 12 features you need to know about helps you learn how to perform handling generic-typed, custom serialization and field exclusion strategy. By the end of this section you will be able to:
Convert a Java object to JSON
Serialize and deserialize in GSON
Use the GSON nested classes handling mechanism
Convert a Java array to a JSON array
Handle generic-type classes while working with GSON serialization and deserialization
Handle a null object while working with GSON
Use versioning support in GSON
Use the no argument constructor support
Create a custom field name from a Java object to JSON output string
Exclude a JSON field while serializing it in GSON
People and places you should get to know provides you with many useful links to the project page and forums, as well as a number of helpful articles, tutorials, blogs, and Twitter feeds by the GSON super-contributors, as every open source project is centered around a community.
In this section, we will cover the JSON data exchange format and its significance, and give an introduction to the GSON library. Finally, we will look up the GSON library and its top features.
This chapter distills the theory of data exchange format from the main core. No previous knowledge about the subject is necessary, but I assume that you are familiar with the fundamentals of JavaScript.
The data exchange format is a very vital area of information technology. Data is read and consumed by many heterogeneous applications to form information. The heterogeneous nature means these applications take the input data in their proprietary format. This requires the input data to be preformatted before it is consumed by the main application, which adds an additional level of complexity to an application. To address this need, a common data exchange format is put in place for communication. This has led to the origin of many popular standard formats such as XML, YAML, and JSON.
Java...
In this section, you will learn about configuring the GSON library in Eclipse IDE for a simple Java project and a Maven type project.
The GSON library project is an open source project and is hosted by Google. This project is free for download and its use is based on Apache License 2.0. The current release of this library is 2.2.2.
The base URL of this library is http://code.google.com/p/google-gson. Navigate to the download section of this page and download the zipped version (google-gson-2.2.2-release.zip
). To verify the correct download of this ZIP file, you can compare the checksum that is provided at the base site of GSON.
You can see the following five files when you unzip the downloaded file. Two of them are text files and the other three are JAR files:
File |
Type |
Details |
---|---|---|
|
Text |
This text file has all the description about Apache License 2.0 and its use |
|
Text |
This text file contains the information and purpose of... |
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.
To use the GSON library, an object of the com.google.gson.Gson
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
In this approach, the GSON class object can be instantiated using the new
keyword. This approach creates a gsonobject
instance without any setting.
In this section, you will learn about the top features supported by the GSON library. You will also learn about how to implement these features.
Objects in GSON are referred as types of JsonElement:

The GSON library can convert any user-defined class objects to and from the JSON representation. The Student
class is a user-defined class, and GSON can serialize any Student
object to JSON.
The Student.java
class is as follows:
public class Student { private String name; private String subject; privateint mark; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public int getMark() { return mark; } public void setMark(int mark) { this.mark = mark; } }
The code for JavaObjectFeaturesUse.java
is as follows:
import com.google.gson.Gson; importcom.packt.chapter.vo.Student; public...
If you need help with GSON, here are some people and places that will prove invaluable:
Home page: http://code.google.com/p/google-gson/
Manual and documentation: http://google-gson.googlecode.com/svn/trunk/gson/docs/javadocs/index.html
Source code: http://code.google.com/p/google-gson/source/checkout
Design document: https://sites.google.com/site/gson/gson-design-document
User guide from Google: https://sites.google.com/site/gson/gson-user-guide
Mkyong's blog: http://www.mkyong.com/java/how-do-convert-java-object-to-from-json-format-gson-api/
Official forums: https://groups.google.com/forum/?fromgroups#!forum/google-gson
Unofficial forums: http://stackoverflow.com/questions/tagged/gson
Java code geek blog post: http://examples.javacodegeeks.com/core-java/gson/convert-java-object-to-from-json-using-gson-example/
GSON applications...