Introduction
cJSON paser with a single file of C, and a single header file.like description of README, the library can take away as much legwork and is the dumbest possible parser.
homepage:http://sourceforge.net/projects/cjson/?source=directory
mirror:https://github.com/openxc/cJSON
license:MIT
the function of the cJSON library is that the string formatted json(JSON data) and the json structure(cJSON object) convert to each other.
Notice
- no safe with multi-threads
- when cJSON_Parse() and cJSON_CreateXXX() functions are finished, call cJSON_Delete to free.
- when cJSON_PrintXXX() functions are finished, call Hooks::free_fn to free.
- strdup() needs free.
Structure
In README, the author introducts two way to use the libaray.one is AUTO mode and the other is MANUAL mode. There is a structure introduction in the manual mode part.
Here's the structure: typedef struct cJSON { struct cJSON *next,*prev; struct cJSON *child; int type; char *valuestring; int valueint; double valuedouble; char *string; } cJSON; By default all values are 0 unless set by virtue of being meaningful. next/prev is a doubly linked list of siblings. next takes you to your sibling, prev takes you back from your sibling to you. Only objects and arrays have a "child", and it's the head of the doubly linked list. A "child" entry will have prev==0, but next potentially points on. The last sibling has next=0. The type expresses Null/True/False/Number/String/Array/Object, all of which are #defined in cJSON.h A Number has valueint and valuedouble. If you're expecting an int, read valueint, if not read valuedouble. Any entry which is in the linked list which is the child of an object will have a "string" which is the "name" of the entry. When I said "name" in the above example, that's "string". "string" is the JSON name for the 'variable name' if you will. Now you can trivially walk the lists, recursively, and parse as you please. You can invoke cJSON\_Parse to get cJSON to parse for you, and then you can take the root object, and traverse the structure (which is, formally, an N-tree), and tokenise as you please.
In fact, json structure is a tree, so cJSON struct is like the node of tree that has child node pointer and sibling pointers there are three type structure in json. those are object, number and string. the type member is as the type in json. the type member'value are False, True, NULL, Number, String, Array and Object. the value is stored in the one of valuestring, valueint and valuedouble by the type.
+------+ +-----+ +-----+ |cJSON |---prev---|cJSON| ---next---|cJSON| +------+ +-----+ +-----+ | child | +-----+ +-----+ +-----+ +--------|cJSON|---prev---|cJSON|---next---|cJSON| +-----+ +-----+ +-----+
Functions
print_xxxx functions are convert json to string parse_xxxx functions are convert string to json
convert functions
- cJSON_strcasecmp() - strcmpcase()
- cJSON_strdup() - strdup()
- parse_number() - atoi() and atof()
- print_number() - itoa() and fto2()
- parse_string() - to a unescape string
- print_string() - to a unescape string
- print_string_ptr() - to a escape string
parse functions
- skip() - trim invisible char such as whitespace cr lf
- parse_value() - parse json token
- print_value() - json to string
- parse_array() - parse json array token
- print_array() - json array to string
- parse_object()
all function
- cJSON_strcasecmp() - strcmpcase()
- cJSON_strdup() - strdup()
- cJSON_InitHooks() - set malloc and free
- cJSON_New_Item() - structure
- cJSON_Delete()
- parse_number() - atoi() and atof()
- print_number() - itoa() and fto2()
- parse_string() - to a unescape string
- print_string() - to a unescape string
- print_string_ptr() - to a escape string
- skip() - trim invisible char such as whitespace cr lf
- cJSON_ParseWithOpts - return_parse_end:the point to the remain string require_null_terminated:there is no remain string or return error
- cJSON_Parse() - string to json the same as cJSON_ParseWithOpts(value, 0, 0)
- cJSON_Print() - json to string
- cJSON_PrintUnformatted() - json to unformatted string
- parse_value() - parse json token
- print_value() - json to string
- parse_array() - parse json array token to string
- print_array() - json array to string
- parse_object() - string to json object
- print_object() - json object
- cJSON_GetArraySize() - json array size
- cJSON_AddItemToArray() - add json to json array
- cJSON_AddItemToObject() - add json to json object
- cJSON_AddItemReferenceToArray() - add json to json array without clone
- cJSON_AddItemReferenceToObject() - add json to json object without clone
- cJSON_DetachItemFormObject
- parse_object()
Process
parse_value() - parse the type of json or call parse_array() or parse_object() parse_array() - in loop, parse call parse_value() for every object in array. parse_object() - call parse self and loop to parse its childern by parse_string