官术网_书友最值得收藏!

  • 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:

So, what is GSON?

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.

So, what is GSON?

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.
主站蜘蛛池模板: 任丘市| 阿拉善左旗| 三台县| 富蕴县| 揭西县| 紫云| 南部县| 阜城县| 会同县| 蓝田县| 西平县| 离岛区| 平陆县| 五寨县| 新平| 油尖旺区| 共和县| 昭苏县| 哈密市| 施秉县| 潼南县| 日照市| 英德市| 安溪县| 万全县| 嘉义县| 同德县| 新兴县| 安吉县| 社会| 会宁县| 鲁甸县| 东山县| 汉中市| 固原市| 伊吾县| 习水县| 镇平县| 德阳市| 和顺县| 固镇县|