cJSON剖析一

来源:互联网 发布:闪讯mac客户端下载 编辑:程序博客网 时间:2024/05/05 12:07
cJSON:1.what;2.how;3.when;4.why;1.what:          JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。          JSON采用完全独立于语言的文本格式,但是也使用了类似于C语言家族的习惯(包括<a target=_blank target="_blank" href="http://baike.baidu.com/subview/10075/6770152.htm">C</a>、C++、<a target=_blank target="_blank" href="http://baike.baidu.com/view/6590.htm">C#</a>、<a target=_blank target="_blank" href="http://baike.baidu.com/subview/29/12654100.htm">Java</a>、JavaScript、<a target=_blank target="_blank" href="http://baike.baidu.com/view/46614.htm">Perl</a>、<a target=_blank target="_blank" href="http://baike.baidu.com/view/21087.htm">Python</a>等)。          这些特性使JSON成为理想的数据交换语言。 易于人阅读和编写,同时也易于机器解析和生成(一般用于提升网络传输速率)。                                                                                                                                                                                                 ---------come from  baidu         cJSON 是一个用c语言完成的JSON解释器。2.how:        实际上github上的代码已经容易理解怎么使用了,make之后看到的编译过程,或是直接看Makefile文件,        我们只需要把我们的字符串传入doit()函数即可,这样就可以将字符串转变成结构体的成员。3.when:       when we need a JSON;4.typedef struct cJSON {    struct cJSON *next,*prev;    /* next/prev allow you to walk array/object chains. Alternatively, use GetArraySize/GetArrayItem/GetObjectItem */    struct cJSON *child;        /* An array or object item will have a child pointer pointing to a chain of the items in the array/object. */    int type;                    /* The type of the item, as above. */    char *valuestring;            /* The item's string, if type==<span style="color:#FF0000;">cJSON_String</span> */    int valueint;                /* The item's number, if type==<span style="color:#FF0000;">cJSON_Number</span> */    double valuedouble;            /* The item's number, if type==<span style="color:#FF0000;">cJSON_Number</span> */    char *string;                /* The item's name string, if this item is the child of, or is in the list of subitems of an object. */} cJSON;我不写总结,我只做代码的搬运工
什么是cJSON_String,什么是cJSON_Number
#define cJSON_Number 3
#define cJSON_String 4
#define cJSON_Array 5
#define cJSON_Object 6

过程:
下面的函数见名只意:
static const char *parse_value(cJSON *item,const char *value);
static const char *parse_array(cJSON *item,const char *value);
static const char *parse_object(cJSON *item,const char *value);
static const char *parse_number(cJSON *item,const char *num);
static const char *parse_string(cJSON *item,const char *str);
static const char *skip(const char *in) {while (in && *in && (unsigned char)*in<=32) in++; return in;}//跳过空白字符或是控制符
cJSON *cJSON_ParseWithOpts(const char *value,const char **return_parse_end,int require_null_terminated);
cJSON *cJSON_Parse(const char *value) {return cJSON_ParseWithOpts(value,0,0);}

cJSON_Parse:
    cJSON_ParseWithOpts:

        parse_value:
           parse_number|parse_string|parse_array|parse_object
               
parse_number: nothing
parse_string: nothing
parse_array :
    parse_value

parse_object:
    circle:
    parse_string
 一般我们是从array,object 开始,对于array,直接进入parse_value,于是就进入了选择,判断是什么类型的数据,分别进入不同的分支。
如果一开始就是object,那么进入parse_string parse_string,从而完成数据格式的转化。
对于数据的存储,这里采用了双链表加上一个孩子域,不同的数据类型有不同的标示,OK;



0 0
原创粉丝点击