Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What's the use of cJSON library?

2025-01-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Network Security >

Share

Shulou(Shulou.com)06/01 Report--

This article mainly shows you "what is the use of cJSON library", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "what is the use of cJSON library" this article.

What is the cJSON library?

CJSON is a lightweight json parsing library. It is very easy to use, the whole library is very concise, and the core functions are implemented in the cJSON.c file, which is very suitable for reading the source code to learn C language. Recently, I finished reading the source code of this library and shared some of my experiences.

What is json? copy the statement on json's official website:

JSON (JavaScript Object Notation) is a lightweight data exchange format. Easy for people to read and write. At the same time, it is also easy to parse and generate by machine. It is based on a subset of JavaScript Programming Language, Standard ECMA-262 3rd Edition-December 1999. JSON uses a completely language-independent text format, but also uses habits similar to those of the C language family (including C, Clipper, Java, JavaScript, Perl, Python, etc.). These features make JSON an ideal data exchange language.

What's in the cJSON library?

Cjson Library github address: https://github.com/DaveGamble/cJSON

The entire library contains cJSON.h and cJSON.c files, and the header file defines a series of API. The most basic and important function of this library is to parse an json string, using cJSON_Parse as the API. The cJSON_Parse function calls the cJSON_ParseWithOpts function, which implements the specific logic.

The prototypes of the two functions are 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)

Function receives a string, parses it, and returns. After parsing, a cjson structure is returned. The definition of cJSON structure is as follows:

Typedef struct cJSON {struct cJSON * next; / / backward pointer struct cJSON * prev; / / forward pointer struct cJSON * child; / / points to a child element, such as a subarray or the string value of the type char * valuestring; / / element of a child object char / / element, if type = = cJSON_String or type = = cJSO_Raw int valueint / / is obsolete. Now use cJSON_SetNumberValue to set the integer value of the double valuedouble; / / element. If type = = cJSON_Number char * string; / / represents the key value of the element, if it has child elements} how does cJSON; parse a json string?

Json's official website is here, http://www.json.org.

The home page of the site describes what json is and its format specification. With the specification, you can know how json is constructed, so there is a direction on how to parse json data.

Json is built using two structures, objects or arrays.

The object uses {as the beginning and} as the end, each element in which is an unordered collection of key-value pairs, the key name and value use: separate, use, separate each element; the array uses [as the beginning,] as the end, the elements inside are a collection of ordered values, and use them as delimiters.

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

Therefore, we can know:

1. You can judge the type of the entire json according to the initials of the json. If the json starts with'{', it is an object, and when it starts with'[', it is an array, otherwise it is a string or other constant.

2. If it is an object, then it must have a key name. Parse its key name first, and then its value. The process of parsing the value is the same as the first step, recursive parsing.

3. If it is an array, parse the elements in the array one by one until you encounter]. The process of parsing the elements in the array is also consistent with the first step, recursive parsing.

This is the idea of parsing json strings according to the definition of the json official website, and then take a look at how the cJSON library is implemented. The implementation flow chart of cJSON_Parse is as follows:

Parse_value is called in the cJSON_ParseWithOpts function, which is the core implementation of the whole function.

The flow chart of the parse_value function is as follows:

As you can see, parse_value judges the beginning of the json value, and then goes to the corresponding branch for parsing, and each branch is analyzed below. The parsed value is stored in the structure of cJSON, which is hereafter named item.

Constant

If the json value is' null','true','false', then the type of item is set to cJSON_NULL, cJSON_TRUE, cJSON_FALSE, respectively. Then continue to parse the remaining json values.

String

If you encounter the beginning of ", it means that the json value is a string, then parse its value, and you only need to get the value between the two". Saving string is also a structure, which needs to apply for memory. In the process of calculating the length, when an escape character is encountered, it needs to be recorded, because the escape character is not saved.

Number

When you encounter the beginning of a number, record the numeric characters that follow it, then convert them to integer numbers, and then check the range of values.

Array

When parsing an array, create a new json structure new_item for the elements of the array, then continue to parse the values in the array, use','to determine the location of the next element, save the resulting value to the structure, and join multiple elements with a linked list. Parse until the'] 'symbol is encountered.

Object

The process of parsing an object is similar to that of an array. Create a new json structure new_item for the elements of the object, and then continue to parse the values in the object. The object is composed of key-value pairs, so first get the value of the key, then use':'to determine the position of the value, and then continue to parse to get the value. Multiple key-value pairs are separated by',', and finally linked by a linked list. Parse until the'} 'symbol is encountered.

Other

Before parsing all values, the skip_whitespace function is called to filter all white space characters on both sides of the string. Here are characters whose ASCII code is less than or equal to 32, such as:\ t,\ n. Functions are as follows:

Static const unsigned char * skip_whitespace (const unsigned char * in) {while (in & * in & & (* in

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Network Security

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report