新增CSON链表操作
新增CSON链表添加元素接口 新增CSON新字符串接口 优化json无格式打印 定义几个方便使用的宏
This commit is contained in:
parent
ba65233636
commit
d7791abf34
|
@ -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;
|
||||||
|
|
|
@ -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模型解析工具
|
||||||
|
|
103
src/cson.c
103
src/cson.c
|
@ -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;
|
||||||
|
}
|
||||||
|
|
105
src/cson.h
105
src/cson.h
|
@ -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);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @}
|
* @}
|
||||||
|
|
Loading…
Reference in New Issue