新增CSON链表操作

新增CSON链表添加元素接口
新增CSON新字符串接口
优化json无格式打印
定义几个方便使用的宏
This commit is contained in:
Letter 2019-11-18 19:27:29 +08:00
parent ba65233636
commit d7791abf34
4 changed files with 182 additions and 50 deletions

View File

@ -73,7 +73,7 @@ void csonTest(void)
"\"subjson\":{\"test\": \"hello\"}}"; "\"subjson\":{\"test\": \"hello\"}}";
struct test *st = csonDecode(jsonStr, model, sizeof(model)/sizeof(CsonModel)); struct test *st = csonDecode(jsonStr, model, sizeof(model)/sizeof(CsonModel));
logDebug("json 0x%08x, id: %d, num: %d, max: %d, value: %lf, name: %s\r\nsub: id: %d, test: %s", logDebug("json 0x%08x, id: %d, num: %d, max: %d, value: %f, name: %s\r\nsub: id: %d, test: %s",
st, st->id, st->num, st->max, st->value, st->name, st->sub ? st->sub->id : 0, st->sub ? st->sub->test : "null"); st, st->id, st->num, st->max, st->value, st->name, st->sub ? st->sub->id : 0, st->sub ? st->sub->test : "null");
logDebug("str: %s %s", st->str[0], st->str[1]); logDebug("str: %s %s", st->str[0], st->str[1]);
CsonList *p = st->list; CsonList *p = st->list;

View File

@ -1,7 +1,7 @@
# CSON # CSON
![version](https://img.shields.io/badge/version-1.0.2-brightgreen.svg) ![version](https://img.shields.io/badge/version-1.0.3-brightgreen.svg)
![build](https://img.shields.io/badge/build-2019.9.29-brightgreen.svg) ![build](https://img.shields.io/badge/build-2019.11.18-brightgreen.svg)
![license](https://img.shields.io/badge/license-MIT-brightgreen.svg) ![license](https://img.shields.io/badge/license-MIT-brightgreen.svg)
基于[cJSON](https://github.com/kbranigan/cJSON),运行于C语言平台的json-struct模型解析工具 基于[cJSON](https://github.com/kbranigan/cJSON),运行于C语言平台的json-struct模型解析工具

View File

@ -161,25 +161,13 @@ char csonDecodeBool(cJSON *json, char *key)
void *csonDecodeList(cJSON *json, char *key, CsonModel *model, int modelSize) void *csonDecodeList(cJSON *json, char *key, CsonModel *model, int modelSize)
{ {
CsonList *list = NULL; CsonList *list = NULL;
CsonList *node;
cJSON *array = cJSON_GetObjectItem(json, key); cJSON *array = cJSON_GetObjectItem(json, key);
if (array && array->type == cJSON_Array) if (array && array->type == cJSON_Array)
{ {
for (short i = 0; i < cJSON_GetArraySize(array); i++) for (short i = 0; i < cJSON_GetArraySize(array); i++)
{ {
if (!list) list = csonListAdd(list, csonDecodeObject(cJSON_GetArrayItem(array, i), model, modelSize));
{
list = cson.malloc(sizeof(CsonList));
node = list;
}
else
{
node->next = cson.malloc(sizeof(CsonList));
node = node->next;
}
node->obj = csonDecodeObject(cJSON_GetArrayItem(array, i), model, modelSize);
node->next = NULL;
} }
} }
return list; return list;
@ -500,17 +488,26 @@ cJSON* csonEncodeObject(void *obj, CsonModel *model, int modelSize)
cJSON_AddBoolToObject(root, model[i].key, *(char *)((int)obj + model[i].offset)); cJSON_AddBoolToObject(root, model[i].key, *(char *)((int)obj + model[i].offset));
break; break;
case CSON_TYPE_STRING: case CSON_TYPE_STRING:
if ((char *)(*(int *)((int)obj + model[i].offset)))
{
csonEncodeString(root, model[i].key, (char *)(*(int *)((int)obj + model[i].offset))); csonEncodeString(root, model[i].key, (char *)(*(int *)((int)obj + model[i].offset)));
}
break; break;
case CSON_TYPE_LIST: case CSON_TYPE_LIST:
if ((CsonList *)*(int *)((int)obj + model[i].offset))
{
cJSON_AddItemToObject(root, model[i].key, cJSON_AddItemToObject(root, model[i].key,
csonEncodeList((CsonList *)*(int *)((int)obj + model[i].offset), csonEncodeList((CsonList *)*(int *)((int)obj + model[i].offset),
model[i].param.sub.model, model[i].param.sub.size)); model[i].param.sub.model, model[i].param.sub.size));
}
break; break;
case CSON_TYPE_STRUCT: case CSON_TYPE_STRUCT:
if ((void *)(*(int *)((int)obj + model[i].offset)))
{
cJSON_AddItemToObject(root, model[i].key, csonEncodeObject( cJSON_AddItemToObject(root, model[i].key, csonEncodeObject(
(void *)(*(int *)((int)obj + model[i].offset)), (void *)(*(int *)((int)obj + model[i].offset)),
model[i].param.sub.model, model[i].param.sub.size)); model[i].param.sub.model, model[i].param.sub.size));
}
break; break;
case CSON_TYPE_ARRAY: case CSON_TYPE_ARRAY:
cJSON_AddItemToObject(root, model[i].key, csonEncodeArray( cJSON_AddItemToObject(root, model[i].key, csonEncodeArray(
@ -518,8 +515,11 @@ cJSON* csonEncodeObject(void *obj, CsonModel *model, int modelSize)
model[i].param.array.eleType, model[i].param.array.size)); model[i].param.array.eleType, model[i].param.array.size));
break; break;
case CSON_TYPE_JSON: case CSON_TYPE_JSON:
if ((char *)(*(int *)((int)obj + model[i].offset)))
{
cJSON_AddItemToObject(root, model[i].key, cJSON_AddItemToObject(root, model[i].key,
cJSON_Parse((char *)(*(int *)((int)obj + model[i].offset)))); cJSON_Parse((char *)(*(int *)((int)obj + model[i].offset))));
}
break; break;
default: default:
break; break;
@ -549,6 +549,24 @@ char* csonEncode(void *obj, CsonModel *model, int modelSize, int bufferSize, int
} }
/**
* @brief json字符串
*
* @param obj
* @param model
* @param modelSize
* @return char* json字符串
*/
char* csonEncodeUnformatted(void *obj, CsonModel *model, int modelSize)
{
cJSON *json = csonEncodeObject(obj, model, modelSize);
CSON_ASSERT(json, return NULL);
char *jsonStr = cJSON_PrintUnformatted(json);
cJSON_Delete(json);
return jsonStr;
}
/** /**
* @brief CSON解析出的对象 * @brief CSON解析出的对象
* *
@ -624,3 +642,62 @@ void csonFreeJson(const char *jsonStr)
cson.free((void *)jsonStr); cson.free((void *)jsonStr);
} }
/**
* @brief CSON链表添加节点
*
* @param list
* @param obj
* @return CsonList
*/
CsonList* csonListAdd(CsonList *list, void *obj)
{
if (!list)
{
list = cson.malloc(sizeof(CsonList));
if (!list)
{
return NULL;
}
list->next = NULL;
list->obj = NULL;
}
CsonList *p = list;
while (p->next)
{
p = p->next;
}
if (!p->obj)
{
p->obj = obj;
p->next = NULL;
}
else
{
CsonList *node = cson.malloc(sizeof(CsonList));
if (node)
{
node->obj = obj;
node->next = NULL;
p->next = node;
}
}
return list;
}
/**
* @brief CSON新字符串
*
* @param src
* @return char*
* @note 使
* 便使`csonFree`
*/
char* csonNewString(const char *src)
{
int len = strlen(src);
char *dest = cson.malloc(len + 1);
strcpy(dest, src);
return dest;
}

View File

@ -16,7 +16,7 @@
#include "cJSON.h" #include "cJSON.h"
#define CSON_VERSION "1.0.2" /**< CSON版本 */ #define CSON_VERSION "1.0.3" /**< CSON版本 */
/** /**
* @defgroup CSON cson * @defgroup CSON cson
@ -232,30 +232,6 @@ extern CsonModel csonBasicListModel[]; /**< 基础类型链表数据模型 */
action; \ action; \
} }
/**
* @brief json字符串
*
* @param obj
* @param model
* @param modelSize
*
* @return char * josn字符串
*/
#define csonEncodeUnformatted(obj, model, modelSize) \
csonEncode(obj, model, modelSize, 256, 0)
/**
* @brief json字符串
*
* @param obj
* @param model
* @param modelSize
*
* @return char * josn字符串
*/
#define csonEncodeFormatted(obj, model, modelSize) \
csonEncode(obj, model, modelSize, 256, 1)
/** /**
* @brief CSON初始化 * @brief CSON初始化
* *
@ -274,6 +250,16 @@ void csonInit(void *malloc, void *free);
*/ */
void *csonDecodeObject(cJSON *json, CsonModel *model, int modelSize); void *csonDecodeObject(cJSON *json, CsonModel *model, int modelSize);
/**
* @brief JSON对象
*
* @param json JSON对象
* @param model
* @return void*
*/
#define csonDecodeObjectEx(json, model) \
csonDecodeObject(json, model, sizeof(model) / sizeof(CsonModel))
/** /**
* @brief JSON字符串 * @brief JSON字符串
* *
@ -284,6 +270,16 @@ void *csonDecodeObject(cJSON *json, CsonModel *model, int modelSize);
*/ */
void *csonDecode(const char *jsonStr, CsonModel *model, int modelSize); void *csonDecode(const char *jsonStr, CsonModel *model, int modelSize);
/**
* @brief JSON字符串
*
* @param jsonStr json字符串
* @param model
* @return void*
*/
#define csonDecodeEx(jsonStr, model) \
csonDecode(jsonStr, model, sizeof(model) / sizeof(CsonModel));
/** /**
* @brief json字符串 * @brief json字符串
* *
@ -306,6 +302,38 @@ cJSON* csonEncodeObject(void *obj, CsonModel *model, int modelSize);
*/ */
char* csonEncode(void *obj, CsonModel *model, int modelSize, int bufferSize, int fmt); char* csonEncode(void *obj, CsonModel *model, int modelSize, int bufferSize, int fmt);
/**
* @brief json字符串
*
* @param obj
* @param model
* @param bufferSize json字符串的空间大小
* @param fmt json字符串
* @return char* json字符串
*/
#define csonEncodeEx(obj, model, bufferSize, fmt) \
csonEncode(obj, model, sizeof(model)/sizeof(CsonModel), bufferSize, fmt)
/**
* @brief json字符串
*
* @param obj
* @param model
* @param modelSize
* @return char* json字符串
*/
char* csonEncodeUnformatted(void *obj, CsonModel *model, int modelSize);
/**
* @brief json字符串
*
* @param obj
* @param model
* @return char* json字符串
*/
#define csonEncodeUnformattedEx(obj, model) \
csonEncodeUnformatted(obj, model, sizeof(model) / sizeof(CsonModel))
/** /**
* @brief CSON解析出的对象 * @brief CSON解析出的对象
* *
@ -315,6 +343,15 @@ char* csonEncode(void *obj, CsonModel *model, int modelSize, int bufferSize, int
*/ */
void csonFree(void *obj, CsonModel *model, int modelSize); void csonFree(void *obj, CsonModel *model, int modelSize);
/**
* @brief CSON解析出的对象
*
* @param obj
* @param model
*/
#define csonFreeEx(obj, model) \
csonFree(obj, model, sizeof(model) / sizeof(CsonModel))
/** /**
* @brief cson编码生成的json字符串 * @brief cson编码生成的json字符串
* *
@ -322,6 +359,24 @@ void csonFree(void *obj, CsonModel *model, int modelSize);
*/ */
void csonFreeJson(const char *jsonStr); void csonFreeJson(const char *jsonStr);
/**
* @brief CSON链表添加节点
*
* @param list
* @param obj
* @return CsonList
*/
CsonList* csonListAdd(CsonList *list, void *obj);
/**
* @brief CSON新字符串
*
* @param src
* @return char*
* @note 使
* 便使`csonFree`
*/
char* csonNewString(const char *src);
/** /**
* @} * @}