优化 基础数据类型链表不再需要指针形式
This commit is contained in:
parent
0f8ffa381d
commit
40a830ddcd
|
@ -93,20 +93,20 @@ void csonTest(void)
|
|||
p = st->charList;
|
||||
while (p)
|
||||
{
|
||||
int *sst = p->obj;
|
||||
int sst = (int)(p->obj);
|
||||
if (p->obj)
|
||||
{
|
||||
logDebug("list: id: %d", *sst);
|
||||
logDebug("list: int: %d", sst);
|
||||
}
|
||||
p = p->next;
|
||||
}
|
||||
p = st->strList;
|
||||
while (p)
|
||||
{
|
||||
char **sst = p->obj;
|
||||
char *sst = p->obj;
|
||||
if (p->obj)
|
||||
{
|
||||
logDebug("list: id: %s", *sst);
|
||||
logDebug("list: str: %s", sst);
|
||||
}
|
||||
p = p->next;
|
||||
}
|
||||
|
@ -118,7 +118,8 @@ void csonTest(void)
|
|||
csonFreeJson(root);
|
||||
csonFree(st, model, sizeof(model)/sizeof(CsonModel));
|
||||
}
|
||||
SHELL_EXPORT_CMD(csonTest, csonTest, test);
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC),
|
||||
csonTest, csonTest, test);
|
||||
|
||||
|
||||
/** 项目结构体 */
|
||||
|
@ -164,7 +165,7 @@ void csonDemo(void)
|
|||
pHub->id, pHub->user, pHub->cson->id, pHub->cson->name);
|
||||
|
||||
/** 序列化对象 */
|
||||
char *formatJson = csonEncodeFormatted(pHub, hubModel, sizeof(hubModel)/sizeof(CsonModel));
|
||||
char *formatJson = csonEncode(pHub, hubModel, sizeof(hubModel)/sizeof(CsonModel), 512, 1);
|
||||
printf("format json: %s\r\n", formatJson);
|
||||
|
||||
/** 释放结构体对象 */
|
||||
|
@ -173,5 +174,5 @@ void csonDemo(void)
|
|||
/** 释放序列化生成的json字符串 */
|
||||
csonFreeJson(formatJson);
|
||||
}
|
||||
SHELL_EXPORT_CMD(csonDemo, csonDemo, test);
|
||||
|
||||
SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0)|SHELL_CMD_TYPE(SHELL_TYPE_CMD_FUNC),
|
||||
csonDemo, csonDemo, test);
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
# CSON
|
||||
|
||||
![version](https://img.shields.io/badge/version-1.0.3-brightgreen.svg)
|
||||
![build](https://img.shields.io/badge/build-2019.11.18-brightgreen.svg)
|
||||
![version](https://img.shields.io/badge/version-1.0.4-brightgreen.svg)
|
||||
![build](https://img.shields.io/badge/build-2020.09.13-brightgreen.svg)
|
||||
![license](https://img.shields.io/badge/license-MIT-brightgreen.svg)
|
||||
|
||||
基于[cJSON](https://github.com/kbranigan/cJSON),运行于C语言平台的json-struct模型解析工具
|
||||
|
|
30
src/cson.c
30
src/cson.c
|
@ -63,6 +63,12 @@ void csonInit(void *malloc, void *free)
|
|||
}
|
||||
|
||||
|
||||
signed char csonIsBasicListModel(CsonModel *model)
|
||||
{
|
||||
return (model >= &csonBasicListModel[0] && model <= &csonBasicListModel[13]) ? 1 : 0;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @brief 解析JSON整型
|
||||
*
|
||||
|
@ -167,7 +173,16 @@ void *csonDecodeList(cJSON *json, char *key, CsonModel *model, int modelSize)
|
|||
{
|
||||
for (short i = 0; i < cJSON_GetArraySize(array); i++)
|
||||
{
|
||||
list = csonListAdd(list, csonDecodeObject(cJSON_GetArrayItem(array, i), model, modelSize));
|
||||
void *obj = csonDecodeObject(cJSON_GetArrayItem(array, i), model, modelSize);
|
||||
if (csonIsBasicListModel(model))
|
||||
{
|
||||
list = csonListAdd(list, (void *)(*((int *)obj)));
|
||||
cson.free(obj);
|
||||
}
|
||||
else
|
||||
{
|
||||
list = csonListAdd(list, obj);
|
||||
}
|
||||
}
|
||||
}
|
||||
return list;
|
||||
|
@ -391,7 +406,14 @@ cJSON* csonEncodeList(CsonList *list, CsonModel *model, int modelSize)
|
|||
{
|
||||
if (p->obj)
|
||||
{
|
||||
item = csonEncodeObject(p->obj, model, modelSize);
|
||||
if (csonIsBasicListModel(model))
|
||||
{
|
||||
item = csonEncodeObject(&(p->obj), model, modelSize);
|
||||
}
|
||||
else
|
||||
{
|
||||
item = csonEncodeObject(p->obj, model, modelSize);
|
||||
}
|
||||
cJSON_AddItemToArray(root, item);
|
||||
}
|
||||
p = p->next;
|
||||
|
@ -577,6 +599,7 @@ char* csonEncodeUnformatted(void *obj, CsonModel *model, int modelSize)
|
|||
void csonFree(void *obj, CsonModel *model, int modelSize)
|
||||
{
|
||||
CsonList *list, *p;
|
||||
void *tmpNode;
|
||||
|
||||
for (short i = 0; i < modelSize; i++)
|
||||
{
|
||||
|
@ -601,7 +624,8 @@ void csonFree(void *obj, CsonModel *model, int modelSize)
|
|||
list = list->next;
|
||||
if (p->obj)
|
||||
{
|
||||
csonFree(p->obj,
|
||||
tmpNode = csonIsBasicListModel(model[i].param.sub.model) ? &(p->obj) : p->obj;
|
||||
csonFree(tmpNode,
|
||||
model[i].param.sub.model, model[i].param.sub.size);
|
||||
}
|
||||
cson.free(p);
|
||||
|
|
|
@ -16,7 +16,7 @@
|
|||
#include "cJSON.h"
|
||||
|
||||
|
||||
#define CSON_VERSION "1.0.3" /**< CSON版本 */
|
||||
#define CSON_VERSION "1.0.4" /**< CSON版本 */
|
||||
|
||||
/**
|
||||
* @defgroup CSON cson
|
||||
|
|
Loading…
Reference in New Issue