- Instant GSON
- Sandeep Kumar Patel
- 685字
- 2021-04-02 10:33:59
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 Script Object Notation (JSON) originates from the most rocking JavaScript language. JSON has derived its syntax from JavaScript objects. In JavaScript, an object can be created by using the new
keyword or anonymously. An anonymous JavaScript object can be created using curly braces with or without a key/value pair.
A typical anonymous JavaScript object syntax is as follows:
{}, {key: value}, {key1: value1, key2: value2}…
In the JavaScript language, almost everything is treated as an object, starting from a function to an array. A typical anonymous array in JavaScript can follow one of the following syntaxes:
[],[{}],[{key: value}],[{key1: value1},{key2: value2}...
Let's test it:
VarnumberArray = [10,20,30,40]; console.log (typeofnumberArray);
The output of the preceding code is an object.
In the Firebug console, it would look as follows:

In the preceding code, a JavaScript array is defined with numbers as elements. In the second line, we are printing its type on the console. It displays the object text in the Console. From the output, it is evident that arrays in JavaScript are objects.
A Web 2.0-based client and server application exchanges data in AJAX mode. XML and JSON are the most preferred data formats in web application development. JSON is the most preferred for the data interchange format because of its following features:
- Lightweight in footprints: The size of a JSON response or file is much smaller than that of an XML one.
- Native nature: All browsers have JavaScript as their native language and JSON follows the same object concept of JavaScript, which makes its rendering faster.
- Simpler syntax: JSON has simpler syntaxes to represent data comprising curly braces and the key/value pairs separated by a colon. This makes the job of parsing easier.
GSON is an open source application programming interface project written in the Java language for handling the JSON data format. It converts a Java object into its JSON representation. It can also be used to convert a JSON string into an equivalent Java object.

The root gson
package contains all the common classes and interfaces for JSON data handling. The subpackages inside gson
are reflect
, annotation
, and stream
. The reflect
package holds the classes and interfaces that deal with information on the Java generic types. The annotation
package contains the related classes and interfaces for custom name mapping for object properties. The stream
package contains the classes and interfaces that are related to reading and writing to and from a stream.
Initial goals of the GSON designs are as follows:
- To have a simple conversion mechanism for converting a Java object to and from JSON. The GSON Java project has a large number of utility methods exposed as APIs for the developers to use.
- To allow pre-existing unmodifiable Java objects to be converted to and from JSON.
- To customize the representation of objects. GSON provides custom mapping of names for the object fields while serializing it to a JSON string.
- To provide a compact and formatted output. By default, the generated JSON string for a Java object is in a compact form. GSON provides pretty printing facilities to get it in the human-readable format.
- 性能測(cè)試從零開(kāi)始
- IBM Lotus Notes 8.5 User Guide
- 架構(gòu)之美
- 3ds Max & Unreal Engine 4:VR三維建模技術(shù)實(shí)例教程(附VR模型)
- Word 2010實(shí)戰(zhàn)技巧精粹
- SolidWorks2014基礎(chǔ)實(shí)例教程
- PowerPoint 2019從入門(mén)到精通(移動(dòng)學(xué)習(xí)版)
- MATLAB 2015從入門(mén)到精通
- Android User Interface Development: Beginner's Guide
- Jetpack Compose:Android全新UI編程
- Building Websites with Joomla! 1.5
- 說(shuō)服力:工作型PPT該這樣做(第2版)
- ANSYS Workbench中文版超級(jí)學(xué)習(xí)手冊(cè)
- NetSuite OneWorld Implementation 2011 R2
- UG NX 12.0中文版從入門(mén)到精通