Giter Site home page Giter Site logo

yuxuebao / struct2json Goto Github PK

View Code? Open in Web Editor NEW

This project forked from armink/struct2json

4.0 1.0 1.0 969 KB

A fast convert library between the JSON and C&C++ structure. Implement structure serialization and deserialization for C&C++. V2.x | C&C++ 结构体与 JSON 快速互转库,快速实现 C 结构体的序列化及反序列化。V2.x

License: MIT License

C 100.00%

struct2json's Introduction

C结构体与 JSON 快速互转库


struct2json

struct2json 是一个开源的C结构体与 JSON 快速互转库,它可以快速实现 结构体对象JSON 对象 之间序列化及反序列化要求。快速、简洁的 API 设计,大大降低直接使用 JSON 解析库来实现此类功能的代码复杂度。

起源

把面向对象设计应用到C语言中,是当下很流行的设计**。由于C语言中没有类,所以一般使用结构体 struct 充当类,那么结构体变量就是对象。有了对象之后,很多时候需要考虑对象的序列化及反序列化问题。C语言不像很多高级语言拥有反射等机制,使得对象序列化及反序列化被原生的支持。

对于C语言来说,序列化为 JSON 字符串是个不错的选择,所以就得使用 cJSON 这类 JSON 解析库,但是使用后的代码冗余且逻辑性差,所以萌生对cJSON库进行二次封装,实现一个 struct 与 JSON 之间快速互转的库。 struct2json 就诞生于此。下面是 struct2json 主要使用场景:

  • 持久化 :结构体对象序列化为 JSON 对象后,可直接保存至文件、Flash,实现对结构体对象的掉电存储;
  • 通信 :高级语言对JSON支持的很友好,例如: Javascript、Groovy 就对 JSON 具有原生的支持,所以 JSON 也可作为C语言与其他语言软件之间的通信协议格式及对象传递格式;
  • 可视化 :序列化为 JSON 后的对象,可以更加直观的展示到控制台或者 UI 上,可用于产品调试、产品二次开发等场景;

如何使用

声明结构体

如下声明了两个结构体,结构体 Hometown 是结构体 Student 的子结构体

/* 籍贯 */
typedef struct {
    char name[16];
} Hometown;

/* 学生 */
typedef struct {
    uint8_t id;
    uint8_t score[8];
    char name[10];
    double weight;
    Hometown hometown;
} Student;

将结构体对象序列化为 JSON 对象

使用前(源文件 使用后(源文件
结构体转JSON-使用前 结构体转JSON-使用后

将 JSON 对象反序列化为结构体对象

使用前(源文件 使用后(源文件
JSON转结构体-使用前 JSON转结构体-使用后

V2.0版本新增功能【yuxuebao】

1) 更新cJSON库至1.7.12版本,并扩充实现,支持int64 (long long)类型数据。PS:cJSON原来int64类型以double方式处理,如果超过16位会有精度损失。

2) 扩展struct2json功能,增加支持结构体内包含结构体成员,支持包含数组成员。

3) 增加struct2json 结构体与JSON转换代码自动生成的Python脚本,支持从头文件中提取结构体定义,并根据结构体定义自动生成结构体与JSON互转代码,并提供相关示例。

V2.0 使用说明:

1) 提取结构体定义:

	将头文件(eg:mc_usr_def.h)放在demo\inc目录下;
	执行generate_struct_defination.py,生成struct_defination.txt;

2) 生成结构体与JSON互转代码:

	执行generate_s2j_code.py,根据结构体定义自动生成结构体与JSON互转代码:my_struct_2_json.c,my_struct_2_json.h;
	该脚本支持的参数类型有 基本类型 和 结构体类型,enum和指针按int处理,不支持union和位域;
	支持的数组类型:支持基本类型一维数组,结构体一维数组,字符二维数组(字符串数组)

3) 测试结构体与JSON转换:

	cd demo
	编译测试代码,gcc ../cJSON/cJSON.c ../struct2json/src/*.c ./*.c -I ../cJSON/ -I ../struct2json/inc/ -lm -DDEBUGS2J  -g -o tests2j
	测试 ./tests2j 
	查看output输出和生成的JSON样例文件struct_defination.json;
	预期输出:*:strcmp:0     *:strcmp:0

*:json_cmp:1

欢迎大家 fork and pull request(Github|OSChina|Coding) 。如果觉得这个开源项目很赞,可以点击项目主页 右上角的Star,同时把它推荐给更多有需要的朋友。

文档

具体内容参考\docs\zh\下的文件。务必保证在 阅读文档 后再使用。

许可

  • Armink (original author)
  • Yu Xuebao (current maintainer)
  • and the other contributors (CONTRIBUTORS.md)

MIT Copyright (c) [email protected]

struct2json's People

Contributors

yuxuebao avatar armink avatar jokidream avatar libradoge avatar zbcfscc avatar

Stargazers

 avatar  avatar  avatar Hongbin Mao avatar

Watchers

James Cloos avatar

Forkers

wxrock

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.