sunchb_cson/demo/test2.c

118 lines
58 KiB
C
Raw Normal View History

#include "cson.h"
#include "stdio.h"
#include "stdlib.h"
typedef struct {
char* name;
char* jump_url;
}ClassInfoChild;
typedef struct {
int has_child;
char* icon;
int id;
char* name;
2020-02-07 11:11:06 +08:00
size_t childrenNum;
ClassInfoChild* children;
}ClassInfo;
typedef struct {
long long timestamp;
2020-02-07 11:11:06 +08:00
size_t infoNum;
ClassInfo* info;
}Data;
typedef struct {
int status;
Data data;
int errcode;
}Response;
reflect_item_t ClassInfoChildTbl[] = {
_property_string(ClassInfoChild, name),
_property_string(ClassInfoChild, jump_url),
_property_end()
};
reflect_item_t ClassInfoTbl[] = {
_property_int(ClassInfo, has_child),
_property_string(ClassInfo, icon),
_property_int(ClassInfo, id),
_property_string(ClassInfo, name),
_property_int(ClassInfo, childrenNum),
_property_array_object(ClassInfo, children, ClassInfoChildTbl, ClassInfoChild, childrenNum),
_property_end()
};
reflect_item_t DataTbl[] = {
_property_int(Data, timestamp),
_property_int(Data, infoNum),
_property_array_object(Data, info, ClassInfoTbl, ClassInfo, infoNum),
_property_end()
};
reflect_item_t ResponseTbl[] = {
_property_int(Response, status),
_property_obj(Response, data, DataTbl),
_property_int(Response, errcode),
_property_end()
};
//const static char* jStr = "{\"status\":1,\"error\":\"\",\"data\":{\"timestamp\":1579058755,\"info\":[{\"has_child\":0,\"icon\":\"http://imge.kugou.com/mcommon/{size}/20181130/20181130172345416784.png\",\"id\":32,\"name\":\"年代\",\"subtitle\":\"\",\"description\":\"\"},{\"has_child\":0,\"icon\":\"http://imge.kugou.com/mcommon/{size}/20181130/20181130172408696210.png\",\"id\":58,\"name\":\"乐器\",\"subtitle\":\"\",\"description\":\"\"}]},\"errcode\":0}";
//const static char* jStr = "{\"status\":1,\"error\":\"\",\"data\":{\"timestamp\":1579058755,\"info\":[{\"has_child\":1,\"icon\":\"http://imge.kugou.com/mcommon/{size}/20181130/20181130172444711866.png\",\"id\":153,\"children\":[{\"jump_url\":\"https://miniapp.kugou.com/node/v2?type=1&id=74&path=%2Findex.html%23%2Fmobile%2Fhome%2F1\",\"album_tag_id\":0,\"icon\":\"http://imge.kugou.com/mcommon/{size}/20190402/20190402165900910180.png\",\"id\":185,\"name\":\"DJ专区\",\"special_tag_id\":0,\"bannerurl\":\"http://imge.kugou.com/v2/mobile_class_banner/{size}/T1hFCsBvW_1RCvBVdK.png\",\"song_tag_id\":0,\"is_hot\":0,\"imgurl\":\"http://imge.kugou.com/mcommon/{size}/20150825/20150825145251513968.png\",\"banner_hd\":\"\",\"is_new\":0,\"has_child\":1,\"theme\":1,\"subtitle\":\"\",\"description\":\"\"},{\"jump_url\":\"https://miniapp.kugou.com/node/v2?type=1&id=74&path=%2Findex.html%23%2Fmobile%2Fhome%2F45\",\"album_tag_id\":0,\"icon\":\"http://imge.kugou.com/mcommon/{size}/20190711/20190711142859901220.png\",\"id\":1401,\"name\":\"抖音专区\",\"special_tag_id\":0,\"bannerurl\":\"\",\"song_tag_id\":0,\"is_hot\":0,\"imgurl\":\"\",\"banner_hd\":\"\",\"is_new\":0,\"has_child\":1,\"theme\":1,\"subtitle\":\"\",\"description\":\"\"},{\"jump_url\":\"https://miniapp.kugou.com/node/v2?type=1&id=74&path=%2Findex.html%23%2Fmobile%2Fhome%2F15\",\"album_tag_id\":0,\"icon\":\"http://imge.kugou.com/mcommon/{size}/20181106/20181106162159569853.png\",\"id\":155,\"name\":\"影视专区\",\"special_tag_id\":0,\"bannerurl\":\"http://imge.kugou.com/v2/mobile_class_banner/{size}/T1xzKsBvWT1RCvBVdK.png\",\"song_tag_id\":0,\"is_hot\":0,\"imgurl\":\"http://imge.kugou.com/mcommon/{size}/20150916/20150916115630236448.png\",\"banner_hd\":\"\",\"is_new\":0,\"has_child\":1,\"theme\":1,\"subtitle\":\"\",\"description\":\"\"},{\"jump_url\":\"https://miniapp.kugou.com/node/v2?type=1&id=74&path=%2Findex.html%23%2Fmobile%2Fhome%2F25\",\"album_tag_id\":0,\"icon\":\"http://imge.kugou.com/mcommon/{size}/20181101/20181101110659435688.png\",\"id\":803,\"name\":\"戏曲专区\",\"special_tag_id\":0,\"bannerurl\":\"\",\"song_tag_id\":0,\"is_hot\":0,\"imgurl\":\"\",\"banner_hd\":\"\",\"is_new\":0,\"has_child\":1,\"theme\":1,\"subtitle\":\"\",\"description\":\"\"},{\"jump_url\":\"https://miniapp.kugou.com/node/v2?type=1&id=74&path=%2Findex.html%23%2Fmobile%2Fhome%2F13\",\"album_tag_id\":0,\"icon\":\"http://imge.kugou.com/mcommon/{size}/20181106/20181106162128954559.png\",\"id\":625,\"name\":\"粤剧专区\",\"special_tag_id\":0,\"bannerurl\":\"\",\"song_tag_id\":0,\"is_hot\":0,\"imgurl\":\"\",\"banner_hd\":\"\",\"is_new\":0,\"has_child\":1,\"theme\":1,\"subtitle\":\"\",\"description\":\"\"},{\"jump_url\":\"https://miniapp.kugou.com/node/v2?type=1&id=74&path=%2Findex.html%23%2Fmobile%2Fhome%2F31\",\"album_tag_id\":0,\"icon\":\"http://imge.kugou.com/mcommon/{size}/20191115/20191115142149416417.png\",\"id\":1031,\"name\":\"韩流专区\",\"special_tag_id\":0,\"bannerurl\":\"\",\"song_tag_id\":0,\"is_hot\":0,\"imgurl\":\"\",\"banner_hd\":\"\",\"is_new\":0,\"has_child\":1,\"theme\":1,\"subtitle\":\"\",\"description\":\"\"}],\"name\":\"专区\",\"subtitle\":\"\",\"description\":\"\"},{\"has_child\":1,\"icon\":\"http://imge.kugou.com/mcommon/{size}/20191104/20191104103209217784.png\",\"id\":2005,\"children\":[{\"jump_url\":\"\",\"album_tag_id\":0,\"icon\":\"\",\"id\":2167,\"name\":\"开心专区\",\"special_tag_id\":0,\"bannerurl\":\"\",\"song_tag_id\":0,\"is_hot\":0,\"imgurl\":\"\",\"banner_hd\":\"http://imge.kugou.com/mcommon/{size}/20191112/20191112114657687089.jpg\",\"is_new\":0,\"has_child\":1,\"theme\":1,\"subtitle\":\"\",\"description\":\"\"},{\"jump_url\":\"\",\"album_tag_id\":0,\"icon\":\"\",\"id\":2173,\"name\":\"想念专区\",\"special_tag_id\":0,\"bannerurl\":\"\",\"song_tag_id\":0,\"is_hot\":0,\"imgurl\":\"\",\"banner_hd\":\"http://imge.kugou.com/mcommon/{size}/20191112/20191112141710521727.jpg\",\"is_new\":0,\"has_child\":1,\"theme\":1,\"subtitle\":\"\",\"description\":\"\"},{\"jump_url\":\"\",\"album_tag_id\":0,\"icon\":\"\",\"id\":2179,\"name\":\"疗伤专区\"
const static char* jStr = "{\"status\":1,\"error\":\"\",\"data\":{\"timestamp\":1579069151,\"info\":[{\"has_child\":1,\"icon\":\"http://imge.kugou.com/mcommon/{size}/20181130/20181130172444711866.png\",\"id\":153,\"children\":[{\"jump_url\":\"https://miniapp.kugou.com/node/v2?type=1&id=74&path=%2Findex.html%23%2Fmobile%2Fhome%2F1\",\"album_tag_id\":0,\"icon\":\"http://imge.kugou.com/mcommon/{size}/20190402/20190402165900910180.png\",\"id\":185,\"name\":\"DJ专区\",\"special_tag_id\":0,\"bannerurl\":\"http://imge.kugou.com/v2/mobile_class_banner/{size}/T1hFCsBvW_1RCvBVdK.png\",\"song_tag_id\":0,\"is_hot\":0,\"imgurl\":\"http://imge.kugou.com/mcommon/{size}/20150825/20150825145251513968.png\",\"banner_hd\":\"\",\"is_new\":0,\"has_child\":1,\"theme\":1,\"subtitle\":\"\",\"description\":\"\"},{\"jump_url\":\"https://miniapp.kugou.com/node/v2?type=1&id=74&path=%2Findex.html%23%2Fmobile%2Fhome%2F45\",\"album_tag_id\":0,\"icon\":\"http://imge.kugou.com/mcommon/{size}/20190711/20190711142859901220.png\",\"id\":1401,\"name\":\"抖音专区\",\"special_tag_id\":0,\"bannerurl\":\"\",\"song_tag_id\":0,\"is_hot\":0,\"imgurl\":\"\",\"banner_hd\":\"\",\"is_new\":0,\"has_child\":1,\"theme\":1,\"subtitle\":\"\",\"description\":\"\"},{\"jump_url\":\"https://miniapp.kugou.com/node/v2?type=1&id=74&path=%2Findex.html%23%2Fmobile%2Fhome%2F3\",\"album_tag_id\":0,\"icon\":\"http://imge.kugou.com/mcommon/{size}/20190402/20190402165839822861.png\",\"id\":227,\"name\":\"儿童专区\",\"special_tag_id\":0,\"bannerurl\":\"http://imge.kugou.com/v2/mobile_class_banner/{size}/T1xNKsBmb_1RCvBVdK.png\",\"song_tag_id\":0,\"is_hot\":1,\"imgurl\":\"http://imge.kugou.com/mcommon/{size}/20150819/20150819142111579284.png\",\"banner_hd\":\"\",\"is_new\":0,\"has_child\":1,\"theme\":1,\"subtitle\":\"\",\"description\":\"\"},{\"jump_url\":\"https://miniapp.kugou.com/node/v2?type=1&id=74&path=%2Findex.html%23%2Fmobile%2Fhome%2F23\",\"album_tag_id\":0,\"icon\":\"http://imge.kugou.com/mcommon/{size}/20181101/20181101110341142930.png\",\"id\":781,\"name\":\"网络专区\",\"special_tag_id\":0,\"bannerurl\":\"\",\"song_tag_id\":0,\"is_hot\":0,\"imgurl\":\"\",\"banner_hd\":\"\",\"is_new\":0,\"has_child\":1,\"theme\":1,\"subtitle\":\"\",\"description\":\"\"},{\"jump_url\":\"https://miniapp.kugou.com/node/v2?type=1&id=74&path=%2Findex.html%23%2Fmobile%2Fhome%2F9\",\"album_tag_id\":0,\"icon\":\"http://imge.kugou.com/mcommon/{size}/20181101/20181101110304370142.png\",\"id\":405,\"name\":\"车载专区\",\"special_tag_id\":0,\"bannerurl\":\"http://imge.kugou.com/v2/mobile_class_banner/{size}/T1TJKsB_K_1RCvBVdK.png\",\"song_tag_id\":0,\"is_hot\":0,\"imgurl\":\"http://imge.kugou.com/mcommon/{size}/20151020/20151020185634232826.png\",\"banner_hd\":\"\",\"is_new\":0,\"has_child\":1,\"theme\":1,\"subtitle\":\"\",\"description\":\"\"},{\"jump_url\":\"https://miniapp.kugou.com/node/v2?type=1&id=74&path=%2Findex.html%23%2Fmobile%2Fhome%2F11\",\"album_tag_id\":0,\"icon\":\"http://imge.kugou.com/mcommon/{size}/20181101/20181101110531714649.png\",\"id\":483,\"name\":\"广场舞专区\",\"special_tag_id\":0,\"bannerurl\":\"http://imge.kugou.com/v2/mobile_class_banner/{size}/T13ebsBgC_1RCvBVdK.png\",\"song_tag_id\":0,\"is_hot\":0,\"imgurl\":\"http://imge.kugou.com/mcommon/{size}/20151030/20151030100347338896.png\",\"banner_hd\":\"\",\"is_new\":0,\"has_child\":1,\"theme\":1,\"subtitle\":\"\",\"description\":\"\"},{\"jump_url\":\"https://miniapp.kugou.com/node/v2?type=1&id=74&path=%2Findex.html%23%2Fmobile%2Fhome%2F17\",\"album_tag_id\":0,\"icon\":\"http://imge.kugou.com/mcommon/{size}/20181101/20181101110434661130.png\",\"id\":439,\"name\":\"轻音乐专区\",\"special_tag_id\":0,\"bannerurl\":\"http://imge.kugou.com/v2/mobile_class_banner/{size}/T1jTJsBmZT1RCvBVdK.png\",\"song_tag_id\":0,\"is_hot\":0,\"imgurl\":\"http://imge.kugou.com/mcommon/{size}/20151026/20151026102137944162.png\",\"banner_hd\":\"\",\"is_new\":0,\"has_child\":1,\"theme\":1,\"subtitle\":\"\",\"description\":\"\"},{\"jump_url\":\"https://miniapp.kugou.com/node/v2?type=1&id=74&path=%2Findex.html%23%2Fmobile%2Fhome%2F
static void* printProperty(void* pData, const reflect_item_t* tbl)
{
if (tbl->type == CSON_ARRAY || tbl->type == CSON_OBJECT) return NULL;
if (tbl->type == CSON_INTEGER || tbl->type == CSON_TRUE || tbl->type == CSON_FALSE) printf("%s:%d\n", tbl->field, *(int*)pData);
if (tbl->type == CSON_REAL) printf("%s:%f\n", tbl->field, *(double*)pData);
if (tbl->type == CSON_STRING) printf("%s:%s\n", tbl->field, *((char**)pData));
return NULL;
}
static void* freePointer(void* pData, const reflect_item_t* tbl)
{
if (tbl->type == CSON_ARRAY || tbl->type == CSON_STRING) {
printf("free field %s.\n", tbl->field);
free(*(void**)pData);
}
return NULL;
}
static void printPlayList(Response* list)
{
/* 调用loopProperty迭代结构体中的属性,完成迭代输出属性值 */
csonLoopProperty(list, ResponseTbl, printProperty);
}
static void freePlayList(Response* list)
{
/* 调用loopProperty迭代结构体中的属性,释放字符串和数组申请的内存空间 */
csonLoopProperty(list, ResponseTbl, freePointer);
}
void test2(){
printf("=========================================\n");
printf("\t\tRunning %s\n", __FUNCTION__);
printf("=========================================\n");
Response resp;
/* string to struct */
int ret = csonJsonStr2Struct(jStr, &resp, ResponseTbl);
/* test print */
printf("ret=%d\n", ret);
printPlayList(&resp);
char* jstrOutput;
ret = csonStruct2JsonStr(&jstrOutput, &resp, ResponseTbl);
printf("ret=%d\nJson:%s\n", ret, jstrOutput);
free(jstrOutput);
freePlayList(&resp);
}