Home Programming Instant GSON

Instant GSON

By Sandeep Patel
books-svg-icon Book
Subscription FREE
eBook $16.99
Print + eBook $26.99
READ FOR FREE Free Trial for 7 days. $15.99 p/m after trial. Cancel Anytime! BUY NOW BUY NOW
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
READ FOR FREE Free Trial for 7 days. $15.99 p/m after trial. Cancel Anytime! BUY NOW BUY NOW
Subscription FREE
eBook $16.99
Print + eBook $26.99
What do you get with a Packt Subscription?
This book & 7000+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook + Subscription?
Download this book in EPUB and PDF formats
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with a Packt Subscription?
This book & 6500+ ebooks & video courses on 1000+ technologies
60+ curated reading lists for various learning paths
50+ new titles added every month on new and emerging tech
Early Access to eBooks as they are being written
Personalised content suggestions
Customised display settings for better reading experience
50+ new titles added every month on new and emerging tech
Playlists, Notes and Bookmarks to easily manage your learning
Mobile App with offline access
What do you get with eBook?
Download this book in EPUB and PDF formats
Access this title in our online reader
DRM FREE - Read whenever, wherever and however you want
Online reader with customised display settings for better reading experience
What do you get with video?
Download this video in MP4 format
Access this title in our online reader
DRM FREE - Watch whenever, wherever and however you want
Online reader with customised display settings for better learning experience
What do you get with Audiobook?
Download a zip folder consisting of audio files (in MP3 Format) along with supplementary PDF
About 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.
Publication date:
August 2013
Publisher
Packt
Pages
60
ISBN
9781783282036

 

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:

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

 

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...
 
About the Author
Latest Reviews (1 reviews total)
Instant GSON
Unlock this book and the full library FREE for 7 days
Start now