{"id":13430277,"url":"https://github.com/armink/struct2json","last_synced_at":"2025-04-12T23:39:58.976Z","repository":{"id":41451528,"uuid":"44855008","full_name":"armink/struct2json","owner":"armink","description":"A fast convert library between the JSON and C structure. Implement structure serialization and deserialization for C. | C 结构体与 JSON 快速互转库，快速实现 C 结构体的序列化及反序列化","archived":false,"fork":false,"pushed_at":"2022-01-07T10:36:47.000Z","size":815,"stargazers_count":693,"open_issues_count":11,"forks_count":301,"subscribers_count":44,"default_branch":"master","last_synced_at":"2025-04-12T23:39:54.342Z","etag":null,"topics":["c","json","structure"],"latest_commit_sha":null,"homepage":"","language":"C","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/armink.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2015-10-24T06:26:27.000Z","updated_at":"2025-03-31T03:47:12.000Z","dependencies_parsed_at":"2022-08-01T00:37:57.859Z","dependency_job_id":null,"html_url":"https://github.com/armink/struct2json","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/armink%2Fstruct2json","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/armink%2Fstruct2json/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/armink%2Fstruct2json/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/armink%2Fstruct2json/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/armink","download_url":"https://codeload.github.com/armink/struct2json/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248647254,"owners_count":21139081,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["c","json","structure"],"created_at":"2024-07-31T02:00:51.733Z","updated_at":"2025-04-12T23:39:58.956Z","avatar_url":"https://github.com/armink.png","language":"C","readme":"# C结构体与 JSON 快速互转库\n\n---\n\n## struct2json\n\n[struct2json](https://github.com/armink/struct2json) 是一个开源的C结构体与 JSON 快速互转库，它可以快速实现 **结构体对象** 与 **JSON 对象** 之间序列化及反序列化要求。快速、简洁的 API 设计，大大降低直接使用 JSON 解析库来实现此类功能的代码复杂度。\n\n## 起源\n\n把面向对象设计应用到C语言中，是当下很流行的设计思想。由于C语言中没有类，所以一般使用结构体 `struct` 充当类，那么结构体变量就是对象。有了对象之后，很多时候需要考虑对象的序列化及反序列化问题。C语言不像很多高级语言拥有反射等机制，使得对象序列化及反序列化被原生的支持。\n\n对于C语言来说，序列化为 JSON 字符串是个不错的选择，所以就得使用 [cJSON](https://github.com/kbranigan/cJSON) 这类 JSON 解析库，但是使用后的代码冗余且逻辑性差，所以萌生对cJSON库进行二次封装，实现一个 struct 与 JSON 之间快速互转的库。 struct2json 就诞生于此。下面是 struct2json 主要使用场景：\n\n- **持久化** ：结构体对象序列化为 JSON 对象后，可直接保存至文件、Flash，实现对结构体对象的掉电存储；\n- **通信** ：高级语言对JSON支持的很友好，例如： Javascript、Groovy 就对 JSON 具有原生的支持，所以 JSON 也可作为C语言与其他语言软件之间的通信协议格式及对象传递格式；\n- **可视化** ：序列化为 JSON 后的对象，可以更加直观的展示到控制台或者 UI 上，可用于产品调试、产品二次开发等场景；\n\n## 如何使用\n\n### 声明结构体\n\n如下声明了两个结构体，结构体 `Hometown` 是结构体 `Student` 的子结构体\n\n```C\n/* 籍贯 */\ntypedef struct {\n    char name[16];\n} Hometown;\n\n/* 学生 */\ntypedef struct {\n    uint8_t id;\n    uint8_t score[8];\n    char name[10];\n    double weight;\n    Hometown hometown;\n} Student;\n```\n\n### 将结构体对象序列化为 JSON 对象\n\n|使用前（[源文件](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)）|\n|:-----:|:-----:|\n|![结构体转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)|\n\n### 将 JSON 对象反序列化为结构体对象\n\n|使用前（[源文件](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)）|\n|:-----:|:-----:|\n|![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)|\n\n### V2.0版本新增功能【yuxuebao】\n#### 1) 更新cJSON库至1.7.12版本，并扩充实现，支持int64 (long long)类型数据。PS：cJSON原来int64类型以double方式处理，如果超过16位会有精度损失。\n#### 2) 扩展struct2json功能，增加支持结构体内包含结构体成员，支持包含数组成员。\n#### 3) 增加struct2json 结构体与JSON转换代码自动生成的Python脚本，支持从头文件中提取结构体定义，并根据结构体定义自动生成结构体与JSON互转代码，并提供相关示例。\n\n### V2.0 使用说明：\n#### 1) 提取结构体定义:\n\t\t将头文件（eg:mc_usr_def.h）放在demo\\inc目录下；\n\t\t执行generate_struct_defination.py，生成struct_defination.txt；\n#### 2) 生成结构体与JSON互转代码:\n\t\t执行generate_s2j_code.py，根据结构体定义自动生成结构体与JSON互转代码：my_struct_2_json.c,my_struct_2_json.h；\n\t\t该脚本支持的参数类型有 基本类型 和 结构体类型，enum和指针按int处理，不支持union和位域；\n\t\t支持的数组类型:支持基本类型一维数组，结构体一维数组，字符二维数组（字符串数组）\n#### 3) 测试结构体与JSON转换:\n\t\tcd demo\n\t\t编译测试代码，gcc ../cJSON/cJSON.c ../struct2json/src/*.c ./*.c -I ../cJSON/ -I ../struct2json/inc/ -lm -DDEBUGS2J  -g -o tests2j\n\t\t测试 ./tests2j \n\t\t查看output输出和生成的JSON样例文件struct_defination.json；\n\t\t预期输出：*:strcmp:0     *:strcmp:0\r*:json_cmp:1\n\n欢迎大家 **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**，同时把它推荐给更多有需要的朋友。\n\n## 文档\n\n具体内容参考[`\\docs\\zh\\`](https://github.com/armink/struct2json/tree/master/docs/zh)下的文件。务必保证在 **阅读文档** 后再使用。\n\n## 许可\n- Armink (original author)\n- Yu Xuebao (current maintainer)\n- and the other contributors (CONTRIBUTORS.md)\n\nMIT Copyright (c) armink.ztl@gmail.com\n","funding_links":[],"categories":["C"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farmink%2Fstruct2json","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farmink%2Fstruct2json","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farmink%2Fstruct2json/lists"}