struct2json - C structures and fast conversion between JSON library V1.0 released

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/zhutianlong/article/details/49861223
[Disclaimer: Copyright, welcome to reprint, not for commercial use. Contact mail: [email protected]]

struct2json
struct2json C is an open source structure and the rapid conversion between JSON library, it can be quickly realized between the sequence of the objects and structures and JSON object deserialization requirements. Fast, simple API design, greatly reduce the direct use of JSON parsing libraries to implement code complexity of such functions.

The origin of
the object-oriented design applied to the C language, is the current popular design. Because the C language does not have classes, so the general use structure struct for the class, then the structure variable is the object. Once you have an object, often you need to consider the serialization and de-serialization of the object in question. Unlike many high-level language C language has a reflection mechanism, making the object serialization and de-serialization is native support.

For the C language, as JSON string sequence is a good choice, you must use such cJSON JSON parsing library, but the use of redundant code logic and poor, so that the initiation of the second package cJSON library, to achieve struct a quick system conversion between JSON and library. struct2json was born here. The following are the main struct2json to use scenarios:

Persistence : structure serialize objects into JSON objects back, can be saved directly to a file, Flash, memory power-down structure of the object;
communication : high-level language support for JSON very friendly, for example: Javascript, Groovy on the right JSON have native support, it may be passed JSON format as a communication protocol between the C language and the target language with other software format;
visualization: the sequence of objects into JSON, can be more intuitive to the console or display the UI, available in product testing, product development and other secondary scene;
how to
declare the structure
the following statement on the sub-structure two structures, the structure is a structure Student's Hometown

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

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

The structure of the target sequence as JSON objects

Not used ( source files ) (Using source files )
Forwarding structure before use JSON- Transfected using the structure JSON-

The JSON object structure deserialize objects

Not used ( source files ) (Using source files )
Turn structure of JSON - before use JSON turn structure - after use

 

Welcome to pull Request and fork ( Github | OSChina | Coding ) . If you feel that this open source project is like, you can click on the project's home page top right corner of Star, while more to recommend it to a friend in need.

Document
details Reference \ docs \ zh \ files under. Be sure to read the documentation before using to ensure.

License
MIT Copyright (c) [email protected]

 

Guess you like

Origin www.cnblogs.com/txqdm/p/11577528.html