当前位置:网站首页>[FFH] openharmony gnawing paper growth plan -- Application of cjson in traditional c/s model
[FFH] openharmony gnawing paper growth plan -- Application of cjson in traditional c/s model
2022-07-24 08:48:00 【Go to bed after finishing】
【FFH】OpenHarmony Gnawing paper growth plan ---cJSON In tradition C/S Application under the model
【 Brief review 】
①【FFH】OpenHarmony Gnawing paper growth plan — Why? JSON Will gradually replace XML?
②【FFH】OpenHarmony Gnawing paper growth plan — Several common JSON Parser comparison
③【FFH】OpenHarmony Gnawing paper growth plan —JSON-RPC
④【FFH】OpenHarmony Gnawing paper growth plan — On serialization specification
⑤【FFH】OpenHarmony Gnawing paper growth plan —Flatbuffers be applied to MQTT agreement
⑥【FFH】OpenHarmony Gnawing paper growth plan — Overview of the development and application of serialization technology
⑦【FFH】OpenHarmony Gnawing paper growth plan —Apache Avro And Twiste
【 This issue focuses on 】
Hello everyone , Review the previous articles , Those we see are the whole JSON The content of the field , This time, let's watch something with OpenHarmony dependent . Let's explore the content of this time OpenHarmony What is the system source code used JSON Parsing library .
- How to find OpenHarmony Referenced third-party open source libraries
- Literature scene summary
- cJSON Structural analysis
- cJSON In tradition C/S Application under the model
How to find OpenHarmony Referenced third-party open source libraries
Before the start , Let's see how to find OpenHarmony Referenced third-party open source libraries .
It's very simple , Only need Gitee Global search on OpenHarmony third party+ Key terms in related fields , For example, I want to see OpenHarmony With the heel JSON Related libraries , That is global search OpenHarmony third party json Field .

Then we can see our search results , as follows , What we are discussing this time is cJSON This C Open source language JSON Parsing library .

Literature scene summary
cJSON It is used in the underlying data communication C Linguistic JSON Data analysis library . For example, it is used at the bottom of a system socket Way to communicate , Both ends of the communication can be used cJSON To deliver JSON Data object processing , Now let's take a look at an article about cJSON In tradition C/S(Client/Server) Application of the model .
cJSON github The open source address is as follows :
https://github.com/DaveGamble/cJSON
cJSON Structural analysis
Let's see cJSON The structure part of the source code :
/* The cJSON structure: */
typedef struct cJSON
{
struct cJSON *next;
struct cJSON *prev;
struct cJSON *child;
int type;
char *valuestring;
int valueint;
double valuedouble;
char *string;
} cJSON;
*next/*prev:Point to the next / Previous key value pair (key-value) node .type:Indicates the data type of the value in the key value pair node .*valuestring/valueint/valuedouble:If type For the string / integer / Floating point type , Then it points to the key value .*string:Used to store all keys (key) The name of , Include child Medium key.*child:When the value type is complex data structure , Such as arrays , object , nesting JSON wait , Then the pointer points to the key value .
You can see cJSON It's through Linked list To store JSON Data , This design is quite ingenious , Compare with JSONcpp,cJSON It can ensure that the written data and the output data are in the same order , and JSONcpp The order consistency of writing and output cannot be guaranteed , Because when storing JSONcpp According to key-value Sort internally . So for some requirements JSON Data sequential business ,cJSON be relative to JSONcpp It's a better choice .
Through this data structure, we can also know each JSON All key value pairs in the object pass next and prev Pointers link adjacent key value pairs of nodes , To ensure the data order we just mentioned .
From this data structure, we can see another ingenious place , Namely *child The pointer , This pointer makes this cJSON Support complex data structures , For example, array , object , nesting JSON Data objects, etc .
cJSON In tradition C/S Application under the model

Pictured ,cJSON There are many function methods in the Library , A series of creation and processing can be carried out JSON Operations on objects .
Now let's take a look at the traditional client End to server End pass cJSON The specific process of data exchange processing .
Data packaging process

The data package needs to be encapsulated into JSON Format , And then over the network . therefore , You need to create a cJSON Object of type , And then call cJSON_AddStringToObject() function , Add the attribute value of information to the object , At this point, by calling cJSON_Print() The function of parses the attributes of the object into the cache , It can be thought that cJSON Objects are serialized as strings , So actually , A string is stored in the cache . here , According to Ascii The code table will be cached Ascii Values are sequentially converted to hexadecimal numbers . Last , After sending the hexadecimal number to the server , So far, the packaging work is completed .
The process of parsing packets

· Again , After the server sends the information to the client , Data packets need to be properly analyzed according to the format of the Protocol . therefore , Call at this time cJSON_Parse() Function to parse JSON Data packets , Besides , You also need to create cJSON Object to receive the parsed data , Last , Use JSON_GetObjectItem() Function to get cJSON Different attribute values of objects , This completes the unpacking of data packets . It should be noted that , After processing the data , Must call cJSON_Delete() Function to release the created object . Otherwise, memory leaks can result , And lead to system instability .
At the end of the article
We are simply right OpenHarmony Apply this third-party open source library cJSON The scenario application analysis and simple source code analysis are carried out .
In the next article, let's make a horizontal comparison with other open source C/C++ JSON Parsing library .
reference
边栏推荐
- Wargames NATAS (0-10) problem solving essay
- dba
- Chinese brands in the historical process
- Kotlin learning note 1 - variables, functions
- Beandefinition three ways to load beans
- 情人节
- Learn the rxjs operator
- Wargames bandit (21-33) problem solving essay
- Meta tags let search engines search your website
- [Sheung Shui Shuo series] EE feedback details
猜你喜欢

超全总结:Go语言如何操作文件

2、 Encapsulation and tool classes for adding, deleting, modifying and checking in midway

Move protocol starts with running and builds a healthy ecosystem of sports

Ubuntu20.04 install MySQL 5.7

Read and understand move2earn project - move

Brief notes on the key points of distributed system principle introduction

4、 Midway integrates swagger and supports JWT bearers

3、 Midway interface security certification

Typora提示【This beta version of Typora is expired, please download and install a newer version】的解决方案

Using OpenCV to do a simple face recognition
随机推荐
Protocol buffer learning notes
基于FPGA的VGA字符显示
SQL learning
Houdini notes
Advantages and disadvantages of redis and ZK implementing distributed locks
超全总结:Go语言如何操作文件
Hack the box - Introduction to networking module detailed Chinese tutorial
redis学习一redis介绍及NIO原理介绍
Limited and unlimited Games: crypto
Play to earn: a new and more promising game paradigm in the future
Wargames bandit (21-33) problem solving essay
office回退版本,从2021到2019
Dynamic programming & backtracking various deformation problems
如何将CAD文件导入图新地球中,与影像地形倾斜模型准确叠加
Developing ebpf program with go language
Source code analysis of BlockingQueue (arraybq and linkedbq)
Hack the box - File Inclusion module detailed Chinese tutorial
table-rowspan
G1 (garbage first) collector
Ubuntu20.04 install MySQL 5.7