From af950ec919d2e1a9ba12d043bce75f309790309a Mon Sep 17 00:00:00 2001 From: suncb Date: Tue, 21 Jul 2020 11:06:37 +0800 Subject: [PATCH] bug fix: parse error on int8/int16/int64 array. --- demo/test.c | 20 +++++----- demo/test5.c | 2 +- inc/cson.h | 102 +++++++++++++++------------------------------------ src/cson.c | 25 +++++++++++-- 4 files changed, 63 insertions(+), 86 deletions(-) diff --git a/demo/test.c b/demo/test.c index 11793fb..ade0198 100644 --- a/demo/test.c +++ b/demo/test.c @@ -113,7 +113,7 @@ typedef struct { size_t lyricNum; Lyric* lyric; size_t keyNum; - int* key; + char* key; size_t strNum; char** strList; } SongInfo; @@ -151,7 +151,7 @@ reflect_item_t song_ref_tbl[] = { _property_int_ex(SongInfo, lyricNum, _ex_args_all), _property_array_object(SongInfo, lyric, lyric_ref_tbl, Lyric, lyricNum, 1), _property_int_ex(SongInfo, keyNum, _ex_args_all), - _property_array_int(SongInfo, key, int, keyNum, 1), + _property_array_int8(SongInfo, key, char, keyNum, 1), _property_int_ex(SongInfo, strNum, _ex_args_all), _property_array_string(SongInfo, strList, char*, strNum, 1), _property_end() @@ -175,7 +175,7 @@ reflect_item_t play_list_ref_tbl[] = { static void printPlayList(PlayList* list); static void freePlayList(PlayList* list); -const static char* jStr = "{\"name\":\"jay zhou\",\"creater\":\"dahuaxia\",\"songList\":[{\"songName\":\"qilixiang\",\"signerName\":\"jay zhou\",\"albumName\":\"qilixiang\",\"url\":\"www.kugou.com\",\"duration\":20093999939292928292234.1,\"paid\":false,\"price\":6.66,\"lyric\":[{\"time\":1,\"text\":\"Sparrow outside the window\"},{\"time\":10,\"text\":\"Multi mouth on the pole\"}],\"key\":[1111,2222,3333]},{\"songName\":\"dongfengpo\",\"signerName\":\"jay zhou\",\"albumName\":\"dongfengpo\",\"url\":\"music.qq.com\",\"duration\":180.9,\"paid\":true,\"price\":0.88,\"lyric\":[{\"time\":10,\"text\":\"A sad parting, standing alone in the window\"},{\"time\":20,\"text\":\"I'm behind the door pretending you're not gone\"}],\"key\":[1234,5678,9876],\"strList\":[\"abcd\",\"efgh\",\"ijkl\"]}],\"extData\":{\"a\":999,\"b\":1}}"; +const static char* jStr = "{\"name\":\"jay zhou\",\"creater\":\"dahuaxia\",\"songList\":[{\"songName\":\"qilixiang\",\"signerName\":\"jay zhou\",\"albumName\":\"qilixiang\",\"url\":\"www.kugou.com\",\"duration\":20093999939292928292234.1,\"paid\":false,\"price\":6.66,\"lyric\":[{\"time\":1,\"text\":\"Sparrow outside the window\"},{\"time\":10,\"text\":\"Multi mouth on the pole\"}],\"key\":[1,2,3]},{\"songName\":\"dongfengpo\",\"signerName\":\"jay zhou\",\"albumName\":\"dongfengpo\",\"url\":\"music.qq.com\",\"duration\":180.9,\"paid\":true,\"price\":0.88,\"lyric\":[{\"time\":10,\"text\":\"A sad parting, standing alone in the window\"},{\"time\":20,\"text\":\"I'm behind the door pretending you're not gone\"}],\"key\":[4,5,6],\"strList\":[\"abcd\",\"efgh\",\"ijkl\"]}],\"extData\":{\"a\":999,\"b\":1}}"; static void checkResult(PlayList* playList, char* jstrOutput); /* @@ -213,7 +213,7 @@ void test1() void checkResult(PlayList* playList, char* jstrOutput) { - const char* encodeTest = "{\"name\":\"jay zhou\",\"creater\":\"dahuaxia\",\"songList\":[{\"songName\":\"qilixiang\",\"signerName\":\"jay zhou\",\"albumName\":\"qilixiang\",\"url\":\"www.kugou.com\",\"duration\":0,\"paid\":false,\"price\":6.66,\"lyric\":[{\"time\":1,\"text\":\"Sparrow outside the window\"},{\"time\":10,\"text\":\"Multi mouth on the pole\"}],\"key\":[1111,2222,3333]},{\"songName\":\"dongfengpo\",\"signerName\":\"jay zhou\",\"albumName\":\"dongfengpo\",\"url\":\"music.qq.com\",\"duration\":180,\"paid\":true,\"price\":0.88,\"lyric\":[{\"time\":10,\"text\":\"A sad parting, standing alone in the window\"},{\"time\":20,\"text\":\"I'm behind the door pretending you're not gone\"}],\"key\":[1234,5678,9876],\"strList\":[\"abcd\",\"efgh\",\"ijkl\"]}],\"extData\":{\"a\":999,\"b\":1}}"; + const char* encodeTest = "{\"name\":\"jay zhou\",\"creater\":\"dahuaxia\",\"songList\":[{\"songName\":\"qilixiang\",\"signerName\":\"jay zhou\",\"albumName\":\"qilixiang\",\"url\":\"www.kugou.com\",\"duration\":0,\"paid\":false,\"price\":6.66,\"lyric\":[{\"time\":1,\"text\":\"Sparrow outside the window\"},{\"time\":10,\"text\":\"Multi mouth on the pole\"}],\"key\":[1,2,3]},{\"songName\":\"dongfengpo\",\"signerName\":\"jay zhou\",\"albumName\":\"dongfengpo\",\"url\":\"music.qq.com\",\"duration\":180,\"paid\":true,\"price\":0.88,\"lyric\":[{\"time\":10,\"text\":\"A sad parting, standing alone in the window\"},{\"time\":20,\"text\":\"I'm behind the door pretending you're not gone\"}],\"key\":[4,5,6],\"strList\":[\"abcd\",\"efgh\",\"ijkl\"]}],\"extData\":{\"a\":999,\"b\":1}}"; /* assert test */ CHECK_STRING(playList->name, "jay zhou"); @@ -232,9 +232,9 @@ void checkResult(PlayList* playList, char* jstrOutput) CHECK_NUMBER(playList->songList[0].lyric[1].time, 10); CHECK_STRING(playList->songList[0].lyric[1].text, "Multi mouth on the pole"); CHECK_NUMBER(playList->songList[0].keyNum, 3); - CHECK_NUMBER(playList->songList[0].key[0], 1111); - CHECK_NUMBER(playList->songList[0].key[1], 2222); - CHECK_NUMBER(playList->songList[0].key[2], 3333); + CHECK_NUMBER(playList->songList[0].key[0], 1); + CHECK_NUMBER(playList->songList[0].key[1], 2); + CHECK_NUMBER(playList->songList[0].key[2], 3); CHECK_NUMBER(playList->songList[0].strNum, 0); CHECK_STRING(playList->songList[1].songName, "dongfengpo"); @@ -250,9 +250,9 @@ void checkResult(PlayList* playList, char* jstrOutput) CHECK_NUMBER(playList->songList[1].lyric[1].time, 20); CHECK_STRING(playList->songList[1].lyric[1].text, "I'm behind the door pretending you're not gone"); CHECK_NUMBER(playList->songList[1].keyNum, 3); - CHECK_NUMBER(playList->songList[1].key[0], 1234); - CHECK_NUMBER(playList->songList[1].key[1], 5678); - CHECK_NUMBER(playList->songList[1].key[2], 9876); + CHECK_NUMBER(playList->songList[1].key[0], 4); + CHECK_NUMBER(playList->songList[1].key[1], 5); + CHECK_NUMBER(playList->songList[1].key[2], 6); CHECK_NUMBER(playList->songList[1].strNum, 3); CHECK_STRING(playList->songList[1].strList[0], "abcd"); CHECK_STRING(playList->songList[1].strList[1], "efgh"); diff --git a/demo/test5.c b/demo/test5.c index ad9a02b..254d202 100644 --- a/demo/test5.c +++ b/demo/test5.c @@ -28,7 +28,7 @@ reflect_item_t data_ref_tbl[] = { }; reflect_item_t multi_array_ref_tbl[] = { - _property_array_int(MultiArrayTest, array, int, NULL, 3), //3D array + _property_array_int32(MultiArrayTest, array, int, NULL, 3), //3D array _property_array_string(MultiArrayTest, strArray, char*, NULL, 3), //3D array _property_array_object(MultiArrayTest, objArray, data_ref_tbl, Data, NULL, 3), //3D array _property_end() diff --git a/inc/cson.h b/inc/cson.h index 70d186f..3bcd97f 100644 --- a/inc/cson.h +++ b/inc/cson.h @@ -101,7 +101,10 @@ typedef struct reflect_item_t { int exArgs; /**< paser return failure when the field is not found and nullable equals to 0 */ } reflect_item_t; -extern const reflect_item_t integerReflectTbl[]; +extern const reflect_item_t integer8ReflectTbl[]; +extern const reflect_item_t integer16ReflectTbl[]; +extern const reflect_item_t integer32ReflectTbl[]; +extern const reflect_item_t integer64ReflectTbl[]; extern const reflect_item_t stringReflectTbl[]; extern const reflect_item_t boolReflectTbl[]; extern const reflect_item_t realReflectTbl[]; @@ -116,52 +119,6 @@ extern const reflect_item_t realReflectTbl[]; #define _property(type, field, jtype, tbl, nullable) {#field, _offset(type, field), _size(type, field), jtype, tbl, 0, NULL, 0, nullable} #define _property_end() {NULL, 0, 0, CSON_NULL, NULL, 0, NULL, 0, 1} -/** - * @brief Declaring integer properties. - * - * @param type: type of struct - * @param field: field name of properties - * - */ -#define _property_int(type, field) _property(type, field, CSON_INTEGER, integerReflectTbl, _ex_args_nullable) - -/** - * @brief Declaring real properties. - * - * @param type: type of struct - * @param field: field name of properties - * - */ -#define _property_real(type, field) _property(type, field, CSON_REAL, realReflectTbl, _ex_args_nullable) - -/** - * @brief Declaring bool properties. - * - * @param type: type of struct - * @param field: field name of properties - * - */ -#define _property_bool(type, field) _property(type, field, CSON_TRUE, boolReflectTbl, _ex_args_nullable) - -/** - * @brief Declaring string properties. - * - * @param type: type of struct - * @param field: field name of properties - * - */ -#define _property_string(type, field) _property(type, field, CSON_STRING, stringReflectTbl, _ex_args_nullable) - -/** - * @brief Declaring struct properties. - * - * @param type: type of struct - * @param field: field name of properties - * @param tbl: property description table of sub-struct - * - */ -#define _property_obj(type, field, tbl) _property(type, field, CSON_OBJECT, tbl, _ex_args_nullable) - /** * @brief Declaring array properties. * @@ -173,34 +130,31 @@ extern const reflect_item_t realReflectTbl[]; * @param dimen: dimension of array * */ +#define _property_int(type, field) _property(type, field, CSON_INTEGER, integer32ReflectTbl, _ex_args_nullable) +#define _property_real(type, field) _property(type, field, CSON_REAL, realReflectTbl, _ex_args_nullable) +#define _property_bool(type, field) _property(type, field, CSON_TRUE, boolReflectTbl, _ex_args_nullable) +#define _property_string(type, field) _property(type, field, CSON_STRING, stringReflectTbl, _ex_args_nullable) +#define _property_obj(type, field, tbl) _property(type, field, CSON_OBJECT, tbl, _ex_args_nullable) + #define _property_array(type, field, tbl, subType, count, dimen) {#field, _offset(type, field), _size(type, field), CSON_ARRAY, tbl, sizeof(subType), #count, dimen, _ex_args_nullable} -#define _property_array_object(type, field, tbl, subType, count, dimen) _property_array(type, field, tbl, subType, count, dimen) -#define _property_array_int(type, field, subType, count, dimen) _property_array(type, field, integerReflectTbl, subType, count, dimen) -#define _property_array_string(type, field, subType, count, dimen) _property_array(type, field, stringReflectTbl, subType, count, dimen) +#define _property_array_int8(type, field, subType, count, dimen) _property_array(type, field, integer8ReflectTbl, subType, count, dimen) +#define _property_array_int16(type, field, subType, count, dimen) _property_array(type, field, integer16ReflectTbl, subType, count, dimen) +#define _property_array_int32(type, field, subType, count, dimen) _property_array(type, field, integer32ReflectTbl, subType, count, dimen) +#define _property_array_int64(type, field, subType, count, dimen) _property_array(type, field, integer64ReflectTbl, subType, count, dimen) #define _property_array_real(type, field, subType, count, dimen) _property_array(type, field, realReflectTbl, subType, count, dimen) #define _property_array_bool(type, field, subType, count, dimen) _property_array(type, field, boolReflectTbl, subType, count, dimen) +#define _property_array_string(type, field, subType, count, dimen) _property_array(type, field, stringReflectTbl, subType, count, dimen) +#define _property_array_object(type, field, tbl, subType, count, dimen) _property_array(type, field, tbl, subType, count, dimen) /** - * @brief nonull definitions. parser will stop and return error code when field not found which declared whit it. + * @brief . * - * @param refer to comment of nullable definition - */ -// #define _property_int_nonull(type, field) _property(type, field, CSON_INTEGER, NULL, 0) -// #define _property_real_nonull(type, field) _property(type, field, CSON_REAL, NULL, 0) -// #define _property_bool_nonull(type, field) _property(type, field, CSON_TRUE, NULL, 0) -// #define _property_string_nonull(type, field) _property(type, field, CSON_STRING, NULL, 0) -// #define _property_obj_nonull(type, field, tbl) _property(type, field, CSON_OBJECT, tbl, 0) -// #define _property_array_nonull(type, field, tbl, subType, count, dimen) {#field, _offset(type, field), _size(type, field), CSON_ARRAY, tbl, sizeof(subType), #count, dimen, 0} -// #define _property_array_object_nonull(type, field, tbl, subType, count, dimen) _property_array_nonull(type, field, tbl, subType, count, dimen) -// #define _property_array_int_nonull(type, field, subType, count, dimen) _property_array_nonull(type, field, integerReflectTbl, subType, count, dimen) -// #define _property_array_string_nonull(type, field, subType, count, dimen) _property_array_nonull(type, field, stringReflectTbl, subType, count, dimen) -// #define _property_array_real_nonull(type, field, subType, count, dimen) _property_array_nonull(type, field, realReflectTbl, subType, count, dimen) -// #define _property_array_bool_nonull(type, field, subType, count, dimen) _property_array_nonull(type, field, boolReflectTbl, subType, count, dimen) - -/** - * @brief nonull definitions. parser will stop and return error code when field not found which declared whit it. - * - * @param refer to comment of nullable definition + * @param type: type of struct + * @param field: field name of properties + * @param tbl: property description table of type of array + * @param subType: type of array + * @param count: property to save the array size + * @param dimen: dimension of array * @param args opptional with _ex_args_nullable(0x01) _ex_args_exclude_decode(0x02) _ex_args_exclude_encode(0x04) */ #define _property_int_ex(type, field, args) _property(type, field, CSON_INTEGER, NULL, args) @@ -208,12 +162,16 @@ extern const reflect_item_t realReflectTbl[]; #define _property_bool_ex(type, field, args) _property(type, field, CSON_TRUE, NULL, args) #define _property_string_ex(type, field, args) _property(type, field, CSON_STRING, NULL, args) #define _property_obj_ex(type, field, tbl, args) _property(type, field, CSON_OBJECT, tbl, args) + #define _property_array_ex(type, field, tbl, subType, count, dimen, args) {#field, _offset(type, field), _size(type, field), CSON_ARRAY, tbl, sizeof(subType), #count, dimen, args} -#define _property_array_object_ex(type, field, tbl, subType, count, dimen, args) _property_array_ex(type, field, tbl, subType, count, dimen) -#define _property_array_int_ex(type, field, subType, count, dimen, args) _property_array_ex(type, field, integerReflectTbl, subType, count, dimen) -#define _property_array_string_ex(type, field, subType, count, dimen, args) _property_array_ex(type, field, stringReflectTbl, subType, count, dimen) +#define _property_array_int8_ex(type, field, subType, count, dimen, args) _property_array_ex(type, field, integer8ReflectTbl, subType, count, dimen) +#define _property_array_int16_ex(type, field, subType, count, dimen, args) _property_array_ex(type, field, integer16ReflectTbl, subType, count, dimen) +#define _property_array_int32_ex(type, field, subType, count, dimen, args) _property_array_ex(type, field, integer32ReflectTbl, subType, count, dimen) +#define _property_array_int64_ex(type, field, subType, count, dimen, args) _property_array_ex(type, field, integer64ReflectTbl, subType, count, dimen) #define _property_array_real_ex(type, field, subType, count, dimen, args) _property_array_ex(type, field, realReflectTbl, subType, count, dimen) #define _property_array_bool_ex(type, field, subType, count, dimen, args) _property_array_ex(type, field, boolReflectTbl, subType, count, dimen) +#define _property_array_string_ex(type, field, subType, count, dimen, args) _property_array_ex(type, field, stringReflectTbl, subType, count, dimen) +#define _property_array_object_ex(type, field, tbl, subType, count, dimen, args) _property_array_ex(type, field, tbl, subType, count, dimen) /** * @brief function type of csonLoopProperty. diff --git a/src/cson.c b/src/cson.c index 6671c8b..8afa28b 100644 --- a/src/cson.c +++ b/src/cson.c @@ -34,8 +34,23 @@ extern cson_interface csomImpl; #define cson_is_number(type) (type == CSON_REAL || type == CSON_INTEGER) #define cson_is_bool(type) (type == CSON_TRUE || type == CSON_FALSE) -const reflect_item_t integerReflectTbl[] = { - {"0Integer", 0, sizeof(int), CSON_INTEGER, NULL, 0, NULL, 1}, +const reflect_item_t integer8ReflectTbl[] = { + {"0Integer", 0, 1, CSON_INTEGER, NULL, 0, NULL, 1}, + {} +}; + +const reflect_item_t integer16ReflectTbl[] = { + {"0Integer", 0, 2, CSON_INTEGER, NULL, 0, NULL, 1}, + {} +}; + +const reflect_item_t integer32ReflectTbl[] = { + {"0Integer", 0, 4, CSON_INTEGER, NULL, 0, NULL, 1}, + {} +}; + +const reflect_item_t integer64ReflectTbl[] = { + {"0Integer", 0, 8, CSON_INTEGER, NULL, 0, NULL, 1}, {} }; @@ -686,7 +701,11 @@ static void* printPropertySub(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_TRUE) printf("%s:True\n", tbl->field); + + if (tbl->type == CSON_FALSE) printf("%s:False\n", tbl->field); + + if (tbl->type == CSON_INTEGER) printf("%s:%lld\n", tbl->field, getIntegerValueFromPointer(pData, tbl->size)); if (tbl->type == CSON_REAL) printf("%s:%f\n", tbl->field, *(double*)pData);