C json practice engine 5, optimized refactoring, json practice

Source: Internet
Author: User

C json practice engine 5, optimized refactoring, json practice

Introduction

Scjson is a compact pure c cross-platform compact engine. It is suitable for replacing the old cJSON engine. The data structure and code layout have been greatly improved.

The advantages are embodied in the following aspects:

1) cross-platform (Windows 10 + VS2017/ubuntu 16.10 + gcc 6.2 passed the test)

2) Data Structure Reconstruction (the memory structure is reduced by half)

3) simplified code thinking (all add up and drop comments, maybe less than 800 lines)

4) json text annotation features are supported // or /**/

5) fewer interfaces, easier to grasp all

Resources used in this article

Previous blog article c json practice engine 4, final jump

Source Code address https://github.com/wangzhione/simplec embedded into simplec framework

Files to be used

Scjson. h-> parse json interface file-> simplec/module/schead/Include/Scjson. h

Scjson. c-> parse json-> simplec/module/schead/scjson. c

Tstr. h-> c string structure interface file used to deal with files-> simplec/module/struct/include/tstr. h

Tstr. c-> specific implementation of the c string structure used to deal with files-> simplec/module/struct/tstr. c

I have written countless codes, and I still find it interesting to write the code ~.

Wandering in http://music.163.com/#/song? Id = 26075648

 

Preface

Before that, I would like to have some interesting snacks. I don't know if there are some people who may feel awkward about using the malloc interface. For details, I can understand the unified entrance of C basic memory.

Here we will deepen our understanding of the internal mechanism of malloc. First of all, we will look at the information we have found, mainly about the maximum size of user-state application memory on linux.

Malloc: https://www.zhihu.com/question/20836462

I did some tests later.

# Include <stdio. h> # include <stdlib. h> # include <limits. h>/** test calloc return result information */int main (void) {void * node; printf ("UINT_MAX = % ld. \ n ", UINT_MAX); node = malloc (UINT_MAX); printf (" malloc UINT_MAX node = % p. \ n ", node); if (node) {puts (" magic! "); Free (node) ;}// test calloc result node = calloc (UINT_MAX, sizeof (char); printf (" calloc UINT_MAX node = % p. \ n ", node); if (node) {puts (" speechless "); free (node);} return 0 ;} /// test result-> System Blue Screen // conclusion-> the behavior of calloc and malloc is determined by the system, and assert may be the best choice.

After the preceding code is run, the window 10 system will directly display a blue screen. This shows that all the functions that users such as malloc and calloc need memory from the system are not transparent.

All of them have undefined behaviors. Therefore, the conclusion is:

1. A sub-system is required to handle malloc. windows applications for large memory crashes.

2. We recommend that you use assert for processing malloc/calloc later.

The above is an explanation of malloc. I found that Microsoft's Visual Studio 2017 is really amazing, and black technology is so fierce. I strongly recommend you try it !!

Here we will start to introduce the design idea of scjson. Here we will use a Data Structure tstr. The design diagram is as follows:

The complete code can be found on github. It is still quite good about 200 lines of code to deepen the understanding of the C string.

Here, another point is that str points to the memory, and tstr points to the memory, which remains unchanged. Therefore, two pieces of memory are used for storage to facilitate later optimization, for example, adding string references and string enumeration.

For details, try to understand how to parse the cloudwu string.

 

Body

Here we will mainly explain the ideas of scjson parsing. We will continue to talk about it in the beginning and find that the anonymous structure or consortium of C11 is quite cool (like C99). For example, the designed c node Structure for saving json nodes

Struct cjson {struct cjson * next; // uses the linked list structure for processing, discard the binary tree structure, and optimize the memory struct cjson * child; // type = (_ CJSON_ARRAY or _ CJSON_OBJECT) then the child is not null for the unsigned char type; // data type and method definition, a nice wish char * key; // The key name of the json content block union {char *; // type = _ CJSON_STRING, which is a string double vd; // type = _ CJSON_NUMBER, which is a num value (int) c-> vd) convert to int or bool };};

In the union, vs and vd can also achieve this effect in the old C89, but there is a small trick in the Linux kernel.

    union {        char * vs;         double vd;    };

The counterfeit implementation is as follows. The disadvantage of this method is macro pollution, which is difficult to understand.

union {  char * vs;  double vd;    } __v;
#define vs __v.vs#define vd __v.vd

 

Here, the memory structure of the scjson parsing engine is as follows:

The code type is

// Several data structures and method definitions in json. The most difficult thing for program development is to understand the concept (idea or business) # define _ CJSON_FALSE (0u) # define _ CJSON_TRUE (1u) # define _ CJSON_NULL (1u <1) # define _ CJSON_NUMBER (1u <2) # define _ CJSON_STRING (1u <3) # define _ CJSON_ARRAY (1u <4) # define _ CJSON_OBJECT (1u <5) # define _ CJSON_ISREF (1u <6) // if it is used in set, it will not be released if it is referenced. # define _ CJSON_ISCONST (1u <7) // used in set. If it is const char *, it will not be released.

The above is the specific structure type after resolution. The following is a brief analysis of text parsing rules.

The main idea is recursive descent analysis. Here we have finished introducing the scjson detailed design diagram. Removing the comments may not contain a total of 800 lines of code. May this sparrow crash (too many rows) Crash

 

Postscript

On s 'en va (Shy 'm)Http://music.163.com/m/song? Id = 29814206 & userid = 16529894

It's better to forget the rivers and lakes-"Chuang Tzu dazongshi tianyun"

Related Article

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.