Reader small image

You're reading from  Instant GSON

Product typeBook
Published inAug 2013
Reading LevelIntermediate
PublisherPackt
ISBN-139781783282036
Edition1st Edition
Languages
Right arrow
Author (1)
Sandeep Patel
Sandeep Patel
author image
Sandeep Patel

http://www.packtpub.com/developing-responsive-web-applications-with-ajax-and-jquery/book
Read more about Sandeep Patel

Right arrow

Chapter 1. Instant GSON

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.

So, what is GSON?


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...

Configuring GSON in Eclipse


In this section, you will learn about configuring the GSON library in Eclipse IDE for a simple Java project and a Maven type project.

Getting the GSON library

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:

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 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

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...

Top 12 features you need to know about


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.

Java objects support

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...
You have been reading a chapter from
Instant GSON
Published in: Aug 2013Publisher: PacktISBN-13: 9781783282036
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Sandeep Patel

http://www.packtpub.com/developing-responsive-web-applications-with-ajax-and-jquery/book
Read more about Sandeep Patel

File

Type

Details

License

Text

This text file has all the description about Apache License 2.0 and its use

ReadMe

Text

This text file contains the information and purpose of...