-
Book Overview & Buying
-
Table Of Contents
Instant GSON
By :
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 the GSON library |
|
|
JAR |
This JAR file contains all the source code for the GSON library |
|
|
JAR |
This JAR file contains the entire Java document generated by the GSON project |
|
|
JAR |
This JAR file is your required GSON library |
To configure the GSON library in Eclipse for a Java project, a developer needs to execute the following steps:
Create a new Java project in the Eclipse workspace. For example, GsonDemo.
Create a new folder, say lib, inside the GsonDemo Java project and paste the gson-2.2.2.jar file in it.
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.
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:
Create a Maven Java/Web project of JAR/WAR archetype, GsonMavenDemo.
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>
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.
Change the font size
Change margin width
Change background colour