<!-- generator=" CodePongo (Base on Kukkaisvoima version 15b3TA)" -->
<rss version="2.0"
xmlns:content="http://purl.org/rss/1.0/modules/content/"
xmlns:wfw="http://wellformedweb.org/CommentAPI/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
>
<channel><title>CodePongo: cJSON</title><link>http://codepongo.com/blog</link><description></description><pubDate>Thu, 22 Aug 2013 17:30:06 +0200</pubDate><lastBuildDate>Thu, 22 Aug 2013 17:30:06 +0200</lastBuildDate><generator>http://codepongo.com/blog</generator><language>zh-cn</language><item><title>cJSON source analysis
</title><link>http://codepongo.com/blog/1FlZdK</link><comments>http://codepongo.com/blog/1FlZdK#comments</comments><pubDate>Thu, 22 Aug 2013 17:30:06 +0200</pubDate><dc:creator>zuohaitao</dc:creator><category>cJSON</category><guid isPermaLink="false">http://codepongo.com/blog/1FlZdK/</guid><description><![CDATA[ 
 [...]]]></description><content:encoded><![CDATA[
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

]]></content:encoded><wfw:commentRss>http://codepongo.com/blog/1FlZdK/feed/</wfw:commentRss></item><item><title>Function Pointers To Malloc And Free
</title><link>http://codepongo.com/blog/473ZQC</link><comments>http://codepongo.com/blog/473ZQC#comments</comments><pubDate>Thu, 08 Aug 2013 17:30:06 +0200</pubDate><dc:creator>zuohaitao</dc:creator><category>expat</category><category>sqlite</category><category>cJSON</category><guid isPermaLink="false">http://codepongo.com/blog/473ZQC/</guid><description><![CDATA[ 
 [...]]]></description><content:encoded><![CDATA[
Recently, I read the source of sqlite, cJSON and expat.
There is a structure in these source.
It looks like:


typdef struct {
    void* (*mallocFn)(size_t s)
void ( * freenFn)(void * p)
} Mem;


The structure has two function pointer members.
The one'type is the malloc function.The Other'type is the free function.

In the source, there is a function interface for set the function pointer.

When the program needs alloc and free memory, it calls the functions to be pointed
by the two members of the structure. 

In my opinion, there are three advantages:


Check memory leak.To implement the malloc and free functions with mark a 
record.When the program is end, check the records to find out memory leak.(I often use it)
Improve the performance.To implement the memory pool myself.(I never use it)
Out-Of-Memory testing.(this advantage is found in sqlite document)


]]></content:encoded><wfw:commentRss>http://codepongo.com/blog/473ZQC/feed/</wfw:commentRss></item></channel></rss>