GithubHelp home page GithubHelp logo

louisyoo / struct2json Goto Github PK

View Code? Open in Web Editor NEW

This project forked from armink/struct2json

0.0 2.0 0.0 261 KB

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

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转结构体-使用后

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

文档

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

许可

MIT Copyright (c) [email protected]

struct2json's People

Contributors

armink avatar zbcfscc avatar

Watchers

James Cloos avatar  avatar

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.