Sign In Start Free Trial
Account

Add to playlist

Create a Playlist

Modal Close icon
You need to login to use this feature.
  • Book Overview & Buying Instant GSON
  • Table Of Contents Toc
Instant GSON

Instant GSON

By : Sandeep Patel
close
close
Instant GSON

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

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 the GSON library

gson-2.2.2-sources

JAR

This JAR file contains all the source code for the GSON library

gson-2.2.2-javadoc

JAR

This JAR file contains the entire Java document generated by the GSON project

gson-2.2.2

JAR

This JAR file is your required GSON library

Configuring a Java project

To configure the GSON library in Eclipse for a Java project, a developer needs to execute the following steps:

  1. Create a new Java project in the Eclipse workspace. For example, GsonDemo.

  2. Create a new folder, say lib, inside the GsonDemo Java project and paste the gson-2.2.2.jar file in it.

  3. Add the JAR file in CLASSPATH of the project, and GSON is now all set to be used:

The preceding screenshot shows that the gson library is added to the Java project. Now, the GSON JAR file is listed in the Referenced Libraries.

Configuring a Maven project

All Maven project file dependencies are maintained by a project object model. In Maven 2, this file is known as pom.xml. All project specific configurations are added as dependencies in the plugin section of pom.xml.

There is a standard format to add a dependency to a Maven type of project. It requires certain tags and values to be included. To add the GSON dependency to a Maven-type project, perform the following steps:

  1. Create a Maven Java/Web project of JAR/WAR archetype, GsonMavenDemo.

  2. Add the GSON dependency in the plugin tag by using the dependency XML tag.

    The XML file looks as follows:

    <dependencies>
    <dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.2.2</version>
    <scope>compile</scope>
    </dependency>
    </dependencies>
  3. Save the pom.xml file and use the install Maven option. Maven install looks through the Maven repository on the network and downloads the GSON library to the Maven dependency:

In the preceding screenshot, after the Maven installation, the GSON library JAR file gets added to the Maven Dependencies folder.

CONTINUE READING
83
Tech Concepts
36
Programming languages
73
Tech Tools
Icon Unlimited access to the largest independent learning library in tech of over 8,000 expert-authored tech books and videos.
Icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Icon 50+ new titles added per month and exclusive early access to books as they are being written.
Instant GSON
notes
bookmark Notes and Bookmarks search Search in title playlist Add to playlist font-size Font size

Change the font size

margin-width Margin width

Change margin width

day-mode Day/Sepia/Night Modes

Change background colour

Close icon Search
Country selected

Close icon Your notes and bookmarks

Confirmation

Modal Close icon
claim successful

Buy this book with your credits?

Modal Close icon
Are you sure you want to buy this book with one of your credits?
Close
YES, BUY

Submit Your Feedback

Modal Close icon
Modal Close icon
Modal Close icon