"Source Analysis" Cjson Library learning

Source: Internet
Author: User

What is the Cjson library?

Cjson is a lightweight JSON parsing library. Very simple to use, the entire library is very concise, the core features are implemented in the Cjson.c file, is very suitable for reading the source code to learn C language. Recently read the source of the library, share some of their own harvest experience.

What is JSON, copy the JSON official website saying:

JSON (JavaScript Object Notation) is a lightweight data interchange format. Easy for people to read and write. It is also easy for machine parsing and generation. It is based on JavaScript programming Language, Standard ECMA-262 a subset of 3rd Edition-december 1999. JSON takes a completely language-independent text format, but also uses a similar idiom to the C language family (c, C + +, C #, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language.

What's inside the Cjson library?

Cjson Library GitHub Address: Https://github.com/DaveGamble/cJSON
The entire library contains cJSON.h and cjson.c two files, and the header file defines a series of APIs. The most basic and most important function of this library is to parse a JSON string, using the API is Cjson_parse. The Cjson_parse function calls the Cjson_parsewithopts function, which implements the specific logic.

The two functions are prototyped as follows:

Cjson_public (Cjson *) cjson_parse (const char *value); Cjson_public (Cjson *) cjson_parsewithopts (const char *value, const char **return_parse_end, Cjson_bool Require_null_ terminated);

The function receives a string and then parses it back. After parsing the return is a Cjson structure, the CJSON structure is defined as follows:

typedef struct cjson{struct Cjson *next;//Backward pointer struct Cjson *prev;//forward pointer struct Cjson *child;//point to child elements such as sub-arrays or sub-object int type; The type of the element char *valuestring; The string value of the element, if type = = cjson_string or type = = Cjso_raw int valueint; Deprecated, now use Cjson_setnumbervalue to set integer value double valuedouble; The integral type value of the element, if type = = Cjson_number char *string; The value that represents the key value of the element, if it has child elements} Cjson;
How do I parse a JSON string?

JSON's official website is here, http://www.json.org
The home page describes what JSON is and its format specifications, and with the specification, you can see how JSON is formed, so there is a way to parse the JSON data.

JSON uses two structures to construct, object, or array.

Object uses{For the beginning,}At the end, each element inside is an unordered collection of key-value pairs, with key names and values used:Separate, use,separates each element; the array uses [as the beginning,] the end, the elements inside are the set of ordered values, and uses the,Make a separator.

Each value can be a string, an integer, or a constant such as true,false,null, or it can be an object or an array, because the JSON structure can be nested.

Therefore, we can learn that:

1, the JSON can be based on the first letter to determine the type of the entire JSON, if the JSON at the ‘{‘ beginning, is the object, at the ‘[‘ beginning, is an array, or else is a string or other constants.

2, if it is an object, then it must have a key name, first resolve its key name, and then parse its value, the process of parsing the value of the same as the first step, recursive parsing

3, if it is an array, then parse the elements within the array, until ] the encounter, the process of parsing the elements inside the array is also consistent with the first step, recursive parsing.

This is the idea of parsing a JSON string based on the definition of the JSON official website, and then see how the Cjson library is implemented. The Cjson_parse implementation flowchart is as follows:

650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/437647/201704/437647-20170426154406303-1729897650. PNG "style=" margin:0px;padding:0px;border:0px; "alt=" 437647-20170426154406303-1729897650.png "/>

The cjson_parsewithopts function calls the Parse_value, which is the core implementation of the whole function.
The flowchart for the Parse_value function is as follows:
650) this.width=650; "Src=" http://images2015.cnblogs.com/blog/437647/201704/437647-20170426154431819-654359958. PNG "style=" margin:0px;padding:0px;border:0px; "alt=" 437647-20170426154431819-654359958.png "/>

As you can see, the parse_value is judged at the beginning of the JSON value and then into the corresponding branch for parsing, and each branch is analyzed below. The parsed value is stored in the Cjson structure, and the following name is item.

Constant

If the JSON value is ' null ', ' true ', ' false ', the item's type is set to Cjson_null, Cjson_true, Cjson_false, respectively. Then continue parsing the remaining JSON values.

String

If you encounter a "start, then the JSON value is a string, then resolve its value, at this point only need to get the value of two". Saving a string is also a struct, which requires memory to be applied, and when the escape character is encountered, it needs to be logged, because the escape characters are not saved.

Number

When a number begins, the number character after it is recorded, then the integer number is converted, and then the range of values is checked.

Array

When an array is parsed, a new JSON struct New_item is created for the elements of the array, which then continues to parse the values inside the arrays, using ', ' to determine the position of the next element, the resulting value is saved to the struct, and multiple elements are concatenated with the linked list. parsing until the '] ' sign is encountered.

Object

The process of parsing an object is similar to an array, creating a new JSON structure body New_item for an object's elements, and then continuing to parse the value inside the object, which is made up of key-value pairs, so that the value of the key is first obtained, and then the value is determined by the ': ' position, which continues to parse the value, and Separated, and finally linked together with a list of links. parsing until the '} ' symbol is encountered.

Other

Before all values are parsed, the Skip_whitespace function is called to filter all whitespace characters on both sides of the string. Here is the ASCII code less than or equal to 32 characters, such as: \ t, \ n. The functions are as follows:

static const unsigned char *skip_whitespace (const unsigned char *in) {while (in && *in && (*in <= 32    )) {in++; } return in;}


"Source Analysis" Cjson Library learning

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.