Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Instant GSON
Instant GSON

Instant GSON: Learn to create JSON data from Java objects and implement them in an application with the GSON library

By Sandeep Patel
€16.99 €10.99
Book Aug 2013 60 pages 1st Edition
eBook
€16.99 €10.99
Print
€20.99
Subscription
€14.99 Monthly
eBook
€16.99 €10.99
Print
€20.99
Subscription
€14.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Aug 26, 2013
Length 60 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781783282036
Vendor :
Oracle
Category :
Table of content icon View table of contents Preview book icon Preview Book

Instant GSON

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...
Left arrow icon Right arrow icon

Key benefits

  • Learn something new in an Instant! A short, fast, focused guide delivering immediate results
  • Convert JAVA Objects to JSON representation and vice versa
  • Learn about the Field Exclusion strategy
  • Write your own JSON converter

Description

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.

What you will learn

Convert Java objects to and from JSON Handle generic and null java objects Maintain different versions of serialized JSON Alter the name of fields when serializing Creating user defined annotation to remove fields while serializing Learn about the implementation of GSON in web applications

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Aug 26, 2013
Length 60 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781783282036
Vendor :
Oracle
Category :

Table of Contents

7 Chapters
Instant GSON Chevron down icon Chevron up icon
Credits Chevron down icon Chevron up icon
About the Author Chevron down icon Chevron up icon
About the Reviewers Chevron down icon Chevron up icon
www.packtpub.com Chevron down icon Chevron up icon
packtlib.packtpub.com Chevron down icon Chevron up icon
Instant GSON Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.