在CJson项目所get到的技能
- 从根据一个开源项目从零开始用C语言编写的
JSON
文本格式的解析库,
库的功能可以解析json文本,并把解析(parse)过后的文本转换成json格式的库函数,总结一下所学习到的内容
例如如下的JSON格式的文本:
" { "
"\"title\" : \"Design Patterns\" ,"
"\"subtitle\" : \"Subtitle\" ,"
"\"author\" : [\"Design Patterns\",\"Richard Helm\",\"Ralph Johnson\",\"John Vlissides\" ] ,"
"\"year\" : 2009 ,"
"\"weight\" : 1.8 ,"
"\"hardcover\" : true ,"
"\"publisher\" : { \"Company\" : \"Personal Education \" , \"Country\" : \" India\" } ,"
"\"website\" : null "
" } "
1,TDD 开发测试框架
-
TDD的主要思路:先写出一个测试函数,编译运行,不通过,然后添加代码,调试,让其通过自己的测试进而实现相应的功能,避免自己所写的代码冗余,减少不必要的代码量。
-
主函数里面进行计算测试通过的个数/测试总个数 = 通过率(避免以前所需要的每次打印输出来看代码运行结果)
#define EXPECT_EQ_BASE(equality, expect, actual, format) \
do {\
test_count++;\
if (equality)\
test_pass++;\
else {\
fprintf(stderr, "%s:%d: expect: " format " actual: " format "\n", __FILE__, __LINE__, expect, actual);\
main_ret = 1;\
}\
} while(0)
#define EXPECT_EQ_INT(expect, actual) EXPECT_EQ_BASE((expect) == (actual), expect, actual, "%d")
static void test_parse() {
test_parse_null();
test_parse_true();
test_parse_false();
test_parse_expect_value();
test_parse_invalid_value();
test_parse_root_not_singular();
}
int main() {
test_parse();
printf("%d/%d (%3.2f%%) passed\n", test_pass, test_count, test_pass * 100.0 / test_count);
return main_ret;
}
- 把所有的返回值,写成一个
枚举
的形式,则对应的数字0 1 2 3 ……
利用期望值和返回值做对应的形式,如果代码通过则返回实际值和期望值相等,test_pass +1,test_count +1。否则反之
结构形式简单,有必要以后按照这样的结构形式进行测试。
2,堆栈的使用
- 在学习单片机的时候,了解过堆栈的定义,基本上是:
向上生长,先进后出
,但是当时,也只是利用汇编进行简单的判断,并且不知道如何利用汇编实现这样的操作,没有自己实际工程中使用过,下面是CJSON 在解析器里面所使用的。
在解析数据并且需要把解析的内容需要保存下来,应该找到一块内存把解析的数据临时存储
起来,结合这样的思路,就是先 malloc 出一块内存,将所解析过的字符压入/push 到堆栈中去。等到解析结束之后,再将内容从stack里面弹/pop 出来,最后需要将malloc
出来的那块内存进行free
。具体的实现代码如下:
typedef struct {
const char* json;
char* stack;
size_t size, top;
}lept_context;
#ifndef LEPT_PARSE_STACK_INIT_SIZE
#define LEPT_PARSE_STACK_INIT_SIZE 256
#endif
#define PUTC(c, ch) do{ *(char*)lept_context_push(c, sizeof(char)) = (ch);} while(0)
static void* lept_context_push(lept_context* c, size_t size) {
void* ret;
assert(size > 0);
if(c->top +size >= c->size) {
if(c->size == 0) c->size =LEPT_PARSE_STACK_INIT_SIZE;
while(c->top +size >= c->size) c->size += c->size >> 1;
c->stack = (char*)realloc(c->stack, c->size);
}
ret = c->stack + c->top; //新的压入数据开始地址 = 原来堆栈地址 + 最高位置(堆栈尺寸)
c->top += size;
return ret;
}
static void* lept_context_pop(lept_context* c, size_t size) {
assert(c->top >= size);
return c->stack + (c->top -= size); //返回的相当于堆栈的首地址
}
3,define 的编写形式
-
一些函数的封装,可以直接利用define 的形式进行定义,这样的话可以使代码更加的方便。自己所使用的函数一般情况不带回返回值的,如下面的函数reverse(int a, int b) 在main函数里面调用,a ,b的值是不会改变的,只是在函数的内部改变了参数,并不改变外部变量的值,这样的话,若改变传入参数的值,需要传入参数的地址(利用指针)就可以了。
我们利用define 的形式就能改变参数值,可以使代码更加清晰。 -
可以用一个简单的例子就能说明问题
#define reverse_char(a, b) \
do{\
char c;\
c =a;\
a =b;\
b =c;\
}while(0)
void reverse(int a, int b) {
int temp;
temp =a;
a =b;
b =temp;
}
int main(void)
{
int a =2, b =5;
printf("a =%d,b =%d\n", a,b);
reverse(a,b);
printf("a =%d,b =%d\n", a,b);
}
- 可以看出其运行效果:函数的调用并没有把函数外面的数给改变,只是在内部发生变化,对外面没有影响。
此时定义的就很清楚 #define
> Executing task: e:\Codefield\CODE_C\C_Single\C_Study\CAndPrimer\C_Multiple\exercise\hello\main.exe <
a =2,b =5
a =2,b =5
a =5,b =2
按任意键关闭终端。
/***********************************************************
//Name :
//Function :测试 类型 error
//Parameter:
//*********************************************************/
#define TEST_ERROR(retvalue, json)\
do {\
lept_value v;\
lept_init(&v);\
EXPECT_EQ_INT(retvalue, lept_parse(&v, json));\
}while(0)
/***********************************************************
//Name :
//Function :测试 parse literal (version 2)
//Parameter:
//*********************************************************/
#define TEST_LITERAL(retvalue, json, lepttype)\
do {\
lept_value v;\
lept_init(&v);\
EXPECT_EQ_INT(retvalue, lept_parse(&v, json));\
EXPECT_EQ_INT(lepttype, lept_get_type(&v));\
}while(0)
4,递归(recursion)的思想
- 对于lept_value 格式的形式可以重复使用,在解析的时候,内嵌式的进行使用堆栈,要记得每一层使用堆栈之后,要进行free,互不相欠。