From 8df09da0732a51e85e7c41c1406ad8435c7c9758 Mon Sep 17 00:00:00 2001 From: armink Date: Thu, 3 Nov 2016 08:34:52 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E3=80=90=E6=9B=B4=E6=96=B0=E3=80=91T?= =?UTF-8?q?ypes=20=E6=8F=92=E4=BB=B6=E4=BD=BF=E7=94=A8=E7=9A=84=20struct2j?= =?UTF-8?q?son=20=E5=BA=93=E8=87=B3=E6=9C=80=E6=96=B0=E7=89=88=E6=9C=AC?= =?UTF-8?q?=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: armink --- easyflash/plugins/types/struct2json/inc/s2j.h | 10 +- .../plugins/types/struct2json/inc/s2jdef.h | 6 + easyflash/plugins/types/struct2json/readme.md | 211 ++---------------- easyflash/plugins/types/struct2json/src/s2j.c | 2 +- 4 files changed, 39 insertions(+), 190 deletions(-) diff --git a/easyflash/plugins/types/struct2json/inc/s2j.h b/easyflash/plugins/types/struct2json/inc/s2j.h index 3b91fd1..73ec4df 100644 --- a/easyflash/plugins/types/struct2json/inc/s2j.h +++ b/easyflash/plugins/types/struct2json/inc/s2j.h @@ -29,8 +29,8 @@ #ifndef __S2J_H__ #define __S2J_H__ +#include #include -#include "cJSON.h" #include "s2jdef.h" #ifdef __cplusplus @@ -44,6 +44,10 @@ extern "C" { #define s2j_create_json_obj(json_obj) \ S2J_CREATE_JSON_OBJECT(json_obj) +/* Delete JSON object */ +#define s2j_delete_json_obj(json_obj) \ + S2J_DELETE_JSON_OBJECT(json_obj) + /* Set basic type element for JSON object */ #define s2j_json_set_basic_element(to_json, from_struct, type, element) \ S2J_JSON_SET_BASIC_ELEMENT(to_json, from_struct, type, element) @@ -60,6 +64,10 @@ extern "C" { #define s2j_create_struct_obj(struct_obj, type) \ S2J_CREATE_STRUCT_OBJECT(struct_obj, type) +/* Delete structure object */ +#define s2j_delete_struct_obj(struct_obj) \ + S2J_DELETE_STRUCT_OBJECT(struct_obj) + /* Get basic type element for structure object */ #define s2j_struct_get_basic_element(to_struct, from_json, type, element) \ S2J_STRUCT_GET_BASIC_ELEMENT(to_struct, from_json, type, element) diff --git a/easyflash/plugins/types/struct2json/inc/s2jdef.h b/easyflash/plugins/types/struct2json/inc/s2jdef.h index 0d8a180..be445ac 100644 --- a/easyflash/plugins/types/struct2json/inc/s2jdef.h +++ b/easyflash/plugins/types/struct2json/inc/s2jdef.h @@ -90,6 +90,9 @@ typedef struct { #define S2J_CREATE_JSON_OBJECT(json_obj) \ cJSON *json_obj = cJSON_CreateObject(); +#define S2J_DELETE_JSON_OBJECT(json_obj) \ + cJSON_Delete(json_obj); + #define S2J_JSON_SET_BASIC_ELEMENT(to_json, from_struct, type, _element) \ S2J_JSON_SET_##type##_ELEMENT(to_json, from_struct, _element) @@ -116,6 +119,9 @@ typedef struct { type *struct_obj = s2jHook.malloc_fn(sizeof(type)); \ if (struct_obj) memset(struct_obj, 0, sizeof(type)); +#define S2J_DELETE_STRUCT_OBJECT(struct_obj) \ + s2jHook.free_fn(struct_obj); + #define S2J_STRUCT_GET_BASIC_ELEMENT(to_struct, from_json, type, _element) \ S2J_STRUCT_GET_##type##_ELEMENT(to_struct, from_json, _element) diff --git a/easyflash/plugins/types/struct2json/readme.md b/easyflash/plugins/types/struct2json/readme.md index 3287b4a..dea28be 100644 --- a/easyflash/plugins/types/struct2json/readme.md +++ b/easyflash/plugins/types/struct2json/readme.md @@ -1,34 +1,34 @@ -# C结构体与 JSON 快速互转库 +# C缁撴瀯浣撲笌 JSON 蹇熶簰杞簱 --- ## struct2json -[struct2json](https://github.com/armink/struct2json) 是一个开源的C结构体与 JSON 快速互转库,它可以快速实现 **结构体对象** 与 **JSON 对象** 之间序列化及反序列化要求。快速、简洁的 API 设计,大大降低直接使用 JSON 解析库来实现此类功能的代码复杂度。 +[struct2json](https://github.com/armink/struct2json) 鏄竴涓紑婧愮殑C缁撴瀯浣撲笌 JSON 蹇熶簰杞簱锛屽畠鍙互蹇熷疄鐜 **缁撴瀯浣撳璞** 涓 **JSON 瀵硅薄** 涔嬮棿搴忓垪鍖栧強鍙嶅簭鍒楀寲瑕佹眰銆傚揩閫熴佺畝娲佺殑 API 璁捐锛屽ぇ澶ч檷浣庣洿鎺ヤ娇鐢 JSON 瑙f瀽搴撴潵瀹炵幇姝ょ被鍔熻兘鐨勪唬鐮佸鏉傚害銆 -## 起源 +## 璧锋簮 -把面向对象设计应用到C语言中,是当下很流行的设计思想。由于C语言中没有类,所以一般使用结构体 `struct` 充当类,那么结构体变量就是对象。有了对象之后,很多时候需要考虑对象的序列化及反序列化问题。C语言不像很多高级语言拥有反射等机制,使得对象序列化及反序列化被原生的支持。 +鎶婇潰鍚戝璞¤璁″簲鐢ㄥ埌C璇█涓紝鏄綋涓嬪緢娴佽鐨勮璁℃濇兂銆傜敱浜嶤璇█涓病鏈夌被锛屾墍浠ヤ竴鑸娇鐢ㄧ粨鏋勪綋 `struct` 鍏呭綋绫伙紝閭d箞缁撴瀯浣撳彉閲忓氨鏄璞°傛湁浜嗗璞′箣鍚庯紝寰堝鏃跺欓渶瑕佽冭檻瀵硅薄鐨勫簭鍒楀寲鍙婂弽搴忓垪鍖栭棶棰樸侰璇█涓嶅儚寰堝楂樼骇璇█鎷ユ湁鍙嶅皠绛夋満鍒讹紝浣垮緱瀵硅薄搴忓垪鍖栧強鍙嶅簭鍒楀寲琚師鐢熺殑鏀寔銆 -对于C语言来说,序列化为 JSON 字符串是个不错的选择,所以就得使用 [cJSON](https://github.com/kbranigan/cJSON) 这类 JSON 解析库,但是使用后的代码冗余且逻辑性差,所以萌生对cJSON库进行二次封装,实现一个 struct 与 JSON 之间快速互转的库。 struct2json 就诞生于此。下面是 struct2json 主要使用场景: +瀵逛簬C璇█鏉ヨ锛屽簭鍒楀寲涓 JSON 瀛楃涓叉槸涓笉閿欑殑閫夋嫨锛屾墍浠ュ氨寰椾娇鐢 [cJSON](https://github.com/kbranigan/cJSON) 杩欑被 JSON 瑙f瀽搴擄紝浣嗘槸浣跨敤鍚庣殑浠g爜鍐椾綑涓旈昏緫鎬у樊锛屾墍浠ヨ悓鐢熷cJSON搴撹繘琛屼簩娆″皝瑁咃紝瀹炵幇涓涓 struct 涓 JSON 涔嬮棿蹇熶簰杞殑搴撱 struct2json 灏辫癁鐢熶簬姝ゃ備笅闈㈡槸 struct2json 涓昏浣跨敤鍦烘櫙锛 -- **持久化** :结构体对象序列化为 JSON 对象后,可直接保存至文件、Flash,实现对结构体对象的掉电存储; -- **通信** :高级语言对JSON支持的很友好,例如: Javascript、Groovy 就对 JSON 具有原生的支持,所以 JSON 也可作为C语言与其他语言软件之间的通信协议格式及对象传递格式; -- **可视化** :序列化为 JSON 后的对象,可以更加直观的展示到控制台或者 UI 上,可用于产品调试、产品二次开发等场景; +- **鎸佷箙鍖** 锛氱粨鏋勪綋瀵硅薄搴忓垪鍖栦负 JSON 瀵硅薄鍚庯紝鍙洿鎺ヤ繚瀛樿嚦鏂囦欢銆丗lash锛屽疄鐜板缁撴瀯浣撳璞$殑鎺夌數瀛樺偍锛 +- **閫氫俊** 锛氶珮绾ц瑷瀵笿SON鏀寔鐨勫緢鍙嬪ソ锛屼緥濡傦細 Javascript銆丟roovy 灏卞 JSON 鍏锋湁鍘熺敓鐨勬敮鎸侊紝鎵浠 JSON 涔熷彲浣滀负C璇█涓庡叾浠栬瑷杞欢涔嬮棿鐨勯氫俊鍗忚鏍煎紡鍙婂璞′紶閫掓牸寮忥紱 +- **鍙鍖** 锛氬簭鍒楀寲涓 JSON 鍚庣殑瀵硅薄锛屽彲浠ユ洿鍔犵洿瑙傜殑灞曠ず鍒版帶鍒跺彴鎴栬 UI 涓婏紝鍙敤浜庝骇鍝佽皟璇曘佷骇鍝佷簩娆″紑鍙戠瓑鍦烘櫙锛 -## 如何使用 +## 濡備綍浣跨敤 -### 声明结构体 +### 澹版槑缁撴瀯浣 -如下声明了两个结构体,结构体 `Hometown` 是结构体 `Student` 的子结构体 +濡備笅澹版槑浜嗕袱涓粨鏋勪綋锛岀粨鏋勪綋 `Hometown` 鏄粨鏋勪綋 `Student` 鐨勫瓙缁撴瀯浣 ```C -/* 籍贯 */ +/* 绫嶈疮 */ typedef struct { char name[16]; } Hometown; -/* 学生 */ +/* 瀛︾敓 */ typedef struct { uint8_t id; uint8_t score[8]; @@ -38,189 +38,24 @@ typedef struct { } Student; ``` -### 将结构体对象序列化为 JSON 对象 +### 灏嗙粨鏋勪綋瀵硅薄搴忓垪鍖栦负 JSON 瀵硅薄 -|未使用([源文件](https://github.com/armink/struct2json/blob/master/docs/zh/assets/not_use_struct2json.c))|使用后([源文件](https://github.com/armink/struct2json/blob/master/docs/zh/assets/used_struct2json.c))| +|浣跨敤鍓嶏紙[婧愭枃浠禲(https://github.com/armink/struct2json/blob/master/docs/zh/assets/not_use_struct2json.c)锛墊浣跨敤鍚庯紙[婧愭枃浠禲(https://github.com/armink/struct2json/blob/master/docs/zh/assets/used_struct2json.c)锛墊 |:-----:|:-----:| -|![结构体转JSON-使用前](https://git.oschina.net/Armink/struct2json/raw/master/docs/zh/images/not_use_struct2json.png)| ![结构体转JSON-使用后](https://git.oschina.net/Armink/struct2json/raw/master/docs/zh/images/used_struct2json.png)| +|![缁撴瀯浣撹浆JSON-浣跨敤鍓峕(https://git.oschina.net/Armink/struct2json/raw/master/docs/zh/images/not_use_struct2json.png)| ![缁撴瀯浣撹浆JSON-浣跨敤鍚嶿(https://git.oschina.net/Armink/struct2json/raw/master/docs/zh/images/used_struct2json.png)| -### 将 JSON 对象反序列化为结构体对象 +### 灏 JSON 瀵硅薄鍙嶅簭鍒楀寲涓虹粨鏋勪綋瀵硅薄 -|未使用([源文件](https://github.com/armink/struct2json/blob/master/docs/zh/assets/not_use_struct2json_for_json.c))|使用后([源文件](https://github.com/armink/struct2json/blob/master/docs/zh/assets/used_struct2json_for_json.c))| +|浣跨敤鍓嶏紙[婧愭枃浠禲(https://github.com/armink/struct2json/blob/master/docs/zh/assets/not_use_struct2json_for_json.c)锛墊浣跨敤鍚庯紙[婧愭枃浠禲(https://github.com/armink/struct2json/blob/master/docs/zh/assets/used_struct2json_for_json.c)锛墊 |:-----:|:-----:| -|![JSON转结构体-使用前](https://git.oschina.net/Armink/struct2json/raw/master/docs/zh/images/not_use_struct2json_for_json.png)| ![JSON转结构体-使用后](https://git.oschina.net/Armink/struct2json/raw/master/docs/zh/images/used_struct2json_for_json.png)| +|![JSON杞粨鏋勪綋-浣跨敤鍓峕(https://git.oschina.net/Armink/struct2json/raw/master/docs/zh/images/not_use_struct2json_for_json.png)| ![JSON杞粨鏋勪綋-浣跨敤鍚嶿(https://git.oschina.net/Armink/struct2json/raw/master/docs/zh/images/used_struct2json_for_json.png)| -欢迎大家 **fork and pull request**([Github](https://github.com/armink/struct2json)|[OSChina](http://git.oschina.net/armink/struct2json)|[Coding](https://coding.net/u/armink/p/struct2json/git)) 。如果觉得这个开源项目很赞,可以点击[项目主页](https://github.com/armink/struct2json) 右上角的**Star**,同时把它推荐给更多有需要的朋友。 +娆㈣繋澶у **fork and pull request**([Github](https://github.com/armink/struct2json)|[OSChina](http://git.oschina.net/armink/struct2json)|[Coding](https://coding.net/u/armink/p/struct2json/git)) 銆傚鏋滆寰楄繖涓紑婧愰」鐩緢璧烇紝鍙互鐐瑰嚮[椤圭洰涓婚〉](https://github.com/armink/struct2json) 鍙充笂瑙掔殑**Star**锛屽悓鏃舵妸瀹冩帹鑽愮粰鏇村鏈夐渶瑕佺殑鏈嬪弸銆 -## 文档 +## 鏂囨。 -具体内容参考[`\docs\zh\`](https://github.com/armink/struct2json/tree/master/docs/zh)下的文件。务必保证在 **阅读文档** 后再使用。 +鍏蜂綋鍐呭鍙傝僛`\docs\zh\`](https://github.com/armink/struct2json/tree/master/docs/zh)涓嬬殑鏂囦欢銆傚姟蹇呬繚璇佸湪 **闃呰鏂囨。** 鍚庡啀浣跨敤銆 -## 许可 +## 璁稿彲 MIT Copyright (c) armink.ztl@gmail.com - -```C -cJSON *struct_to_json(void* struct_obj) { - Student *struct_student = (Student *)struct_obj; - - /* 创建Student JSON对象 */ - s2j_create_json_obj(json_student); - - /* 序列化数据到Student JSON对象 */ - s2j_json_set_basic_element(json_student, struct_student, int, id); - s2j_json_set_basic_element(json_student, struct_student, double, weight); - s2j_json_set_array_element(json_student, struct_student, int, score, 8); - s2j_json_set_basic_element(json_student, struct_student, string, name); - - /* 序列化数据到Student.Hometown JSON对象 */ - s2j_json_set_struct_element(json_hometown, json_student, struct_hometown, struct_student, Hometown, hometown); - s2j_json_set_basic_element(json_hometown, struct_hometown, string, name); - - /* 返回Student JSON对象指针 */ - return json_student; -} - - - - - - - - - - - -``` -```C -cJSON *struct_to_json(void* struct_obj) { - Student *struct_student = (Student *) struct_obj; - cJSON *score, *score_element; - size_t index = 0; - - /* 创建Student JSON对象 */ - cJSON *json_student = cJSON_CreateObject(); - - /* 序列化数据到Student JSON对象 */ - cJSON_AddNumberToObject(json_student, "id", struct_student->id); - cJSON_AddNumberToObject(json_student, "weight", struct_student->weight); - score = cJSON_CreateArray(); - if (score) { - while (index < 8) { - score_element = cJSON_CreateNumber(struct_student->score[index++]); - cJSON_AddItemToArray(score, score_element); - } - cJSON_AddItemToObject(json_student, "score", score); - } - cJSON_AddStringToObject(json_student, "name", struct_student->name); - - /* 序列化数据到Student.Hometown JSON对象 */ - Hometown *hometown_struct = &(struct_student->hometown); - cJSON *hometown_json = cJSON_CreateObject(); - cJSON_AddItemToObject(json_student, "hometown", hometown_json); - cJSON_AddStringToObject(hometown_json, "name", hometown_struct->name); - - /* 返回Student JSON对象指针 */ - return json_student; -} - -``` - - -```C -void *json_to_struct(cJSON* json_obj) { - /* 创建Student结构体对象 */ - s2j_create_struct_obj(struct_student, Student); - - /* 反序列化数据到Student结构体对象 */ - s2j_struct_get_basic_element(struct_student, json_obj, int, id); - s2j_struct_get_array_element(struct_student, json_obj, int, score); - s2j_struct_get_basic_element(struct_student, json_obj, string, name); - s2j_struct_get_basic_element(struct_student, json_obj, double, weight); - - /* 反序列化数据到Student.Hometown结构体对象 */ - s2j_struct_get_struct_element(struct_hometown, struct_student, json_hometown, json_obj, Hometown, hometown); - s2j_struct_get_basic_element(struct_hometown, json_hometown, string, name); - - /* 返回Student结构体对象指针 */ - return struct_student; -} - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -``` - -``` -void *json_to_struct(cJSON* json_obj) { - cJSON *json_temp, *score, *score_element; - Student *struct_student; - size_t index = 0, score_size = 0; - - /* 创建Student结构体对象 */ - struct_student = s2j_malloc(sizeof(Student)); - if (struct_student) { - memset(struct_student, 0, sizeof(Student)); - } - - /* 反序列化数据到Student结构体对象 */ - json_temp = cJSON_GetObjectItem(json_obj, "id"); - if (json_temp) { - struct_student->id = json_temp->valueint; - } - score = cJSON_GetObjectItem(json_obj, "score") - if (score) { - score_size = cJSON_GetArraySize(score); - while (index < score_size) { - score_element = cJSON_GetArrayItem(score, index); - if (score_element) { - struct_student->score[index++] = score_element->valueint; - } - } - } - json_temp = cJSON_GetObjectItem(json_obj, "name"); - if (json_temp) { - strcpy(struct_student->name, json_temp->valuestring); - } - json_temp = cJSON_GetObjectItem(json_obj, "weight"); - if (json_temp) { - struct_student->weight = json_temp->valuedouble; - } - - /* 反序列化数据到Student.Hometown结构体对象 */ - Hometown *struct_hometown = &(struct_student->hometown); - cJSON *json_hometown = cJSON_GetObjectItem(json_obj, "hometown"); - json_temp = cJSON_GetObjectItem(json_hometown, "name"); - if (json_temp) { - strcpy(struct_hometown->name, json_temp->valuestring); - } - - /* 返回Student结构体对象指针 */ - return struct_student; -} -``` \ No newline at end of file diff --git a/easyflash/plugins/types/struct2json/src/s2j.c b/easyflash/plugins/types/struct2json/src/s2j.c index 87246a3..32ff16e 100644 --- a/easyflash/plugins/types/struct2json/src/s2j.c +++ b/easyflash/plugins/types/struct2json/src/s2j.c @@ -26,7 +26,7 @@ * Created on: 2015-10-14 */ -#include "..\inc\s2j.h" +#include #include S2jHook s2jHook = {