Transformation between JSON and struct. (类似Java Gson,C语言实现将json和struct的快捷转换。)
Go to file
孙传宝 eddf45f4c6 add language label 2020-03-14 00:23:41 +08:00
3rd cson commit 2019-12-24 23:13:45 -08:00
adapter Simplify source file structure 2020-02-26 14:38:22 +08:00
demo add free pointer api 2020-03-12 21:37:45 +08:00
inc fix name of var 2020-03-12 21:51:38 +08:00
src fix name of var 2020-03-12 21:51:38 +08:00
LICENSE cson commit 2019-12-24 23:13:45 -08:00
Makefile cson commit 2019-12-24 23:13:45 -08:00
README.md add language label 2020-03-14 00:22:32 +08:00
README_zh.md add language label 2020-03-14 00:23:41 +08:00

README.md

简体中文 | English

cson

Transformation between json string and struct.

The transformation between string to JSON objects (e.g. json_t in Jansson) is implemented by a third-party library (Jansson or cJSON. Refer to Dependence). What cson really implements is the transformation between JSON object and structure.

Compilation

Compile static library

$ git clone https://github.com/sunchb/cson.git
$ cd cson
$ make

Compile demo

$ cd demo
$ make
$ ./test

How to use

  1. Define the data structure for JSON.
  2. Define the property description table for data structure.
  3. Call cson api to decode or encode.

Dependence

https://github.com/akheron/jansson.git https://github.com/DaveGamble/cJSON.git

Demo

Here is a JSON with various data types. eg. Interger, String, Real, Boolean, Object, and Array. We're going to decode and encode it by cson.

{
    "name":"jay zhou",
    "creater":"dahuaxia",
    "songNum":2,
    "songList":[
        {
            "songName":"qilixiang",
            "signerName":"jay zhou",
            "albumName":"qilixiang",
            "url":"www.kugou.com",
            "duration":200,
            "paid":false,
            "price":6.6600000000000001,
            "lyricNum":2,
            "lyric":[
                {
                    "time":1,
                    "text":"Sparrow outside the window"
                },
                {
                    "time":10,
                    "text":"Multi mouth on the pole"
                }
            ]
        },
        {
            "songName":"dongfengpo",
            "signerName":"jay zhou",
            "albumName":"dongfengpo",
            "url":"music.qq.com",
            "duration":180,
            "paid":true,
            "price":0.88,
            "lyricNum":2,
            "lyric":[
                {
                    "time":10,
                    "text":"A sad parting, standing alone in the window"
                },
                {
                    "time":20,
                    "text":"I'm behind the door pretending you're not gone"
                }
            ]
        }
    ],
    "extData":{
        "a":999,
        "b":1.05
    }
}

1. Define the data structure for JSON.

Even if you don't use cson, you usually need to do like this.

Attention

  • String must be declared as char*.
  • Array must be declared as pointer.
  • Declare additional properties to hold the array size for every array property.
typedef struct {
    int         time;
    char*       text;         /* String must be declared as char* */
} Lyric;

typedef struct {
    char*       songName;
    char*       signerName;
    char*       albumName;
    char*       url;
    int         duration;
    int         paid;
    double      price;
    size_t      lyricNum;     /* Declare additional properties to hold the array size */
    Lyric*      lyric;        /* Array must be declared as pointer */
} SongInfo;

typedef struct {
    int         a;
    double      b;
} ExtData;

typedef struct {
    char*       name;
    char*       creater;
    size_t      songNum;
    SongInfo*   songList;
    ExtData     extData;
} PlayList;

2. Define the property description table for data structure.

Use following definitions to help you describe the structure.

  • _property_int(type, field)
  • _property_real(type, field)
  • _property_bool(type, field)
  • _property_string(type, field)
  • _property_obj(type, field, tbl)
  • _property_array_object(type, field, tbl, arrayType, countfild) # I know it's a little cumbersome, I'm trying to simplify it.
  • _property_array_int(type, field, arrayType, countfild)
  • _property_array_string(type, field, arrayType, countfild)
  • _property_array_real(type, field, arrayType, countfild)
  • _property_array_bool(type, field, arrayType, countfild)

Args notes:

  • type: type of data structure
  • field: property name
  • tbl: description table of the property type. use when object or object array
  • arrayType: type of the array (Used to calculate size when dynamically get array memory)
  • countfild: property to save array size
/* description for Lyric */
reflect_item_t lyric_ref_tbl[] = {
    _property_int(Lyric, time),
    _property_string(Lyric, text),
    _property_end()
};

/* description for SongInfo */
reflect_item_t song_ref_tbl[] = {
    _property_string(SongInfo, songName),
    _property_string(SongInfo, signerName),
    _property_string(SongInfo, albumName),
    _property_string(SongInfo, url),
    _property_int(SongInfo, duration),
    _property_bool(SongInfo, paid),
    _property_real(SongInfo, price),
    _property_int(SongInfo, lyricNum),          
    _property_array(SongInfo, lyric, lyric_ref_tbl, Lyric, lyricNum),   /* Lyric: type of array; lyricNum: property to save array size */
    _property_end()
};

/* description for ExtData */
reflect_item_t ext_data_ref_tbl[] = {
    _property_int(ExtData, a),
    _property_real(ExtData, b),
    _property_end()
};

/* description for PlayList */
reflect_item_t play_list_ref_tbl[] = {
    _property_string(PlayList, name),
    _property_string(PlayList, creater),
    _property_int(PlayList, songNum),
    _property_array(PlayList, songList, song_ref_tbl, SongInfo, songNum),
    _property_obj(PlayList, extData, ext_data_ref_tbl),
    _property_end()
};

3. Call cson api to decode or encode.

PlayList playList;

/* Decode */
csonJsonStr2Struct(jStr, &playList, play_list_ref_tbl);

/* Encode */
char* jstrOutput;
csonStruct2JsonStr(&jstrOutput, &playList, play_list_ref_tbl);

Restrict

  • Multidimensional arrays are not supported.
  • Cjson is used by default. If you want to use jannson or other json lib, please modify $(JSON_LIB) in makefile.