cJSON-シンプルで強力なJSON解析ライブラリを使用

4320 ワード

C++でJSONを扱うライブラリがたくさんあり、JSOnCPP、json-parser、Jansson...多すぎます
しかし、私たちはプログラム開発をして、1つのライブラリがどんなに強力な機能を持っているかは必ずしも必要ありませんが、このライブラリは必ず私たちの要求を満たすことができて、同時に、開発は簡単で、私たちの開発の負担と学習の負担を増加しないほうがいいです.
私はmenthol言語のJSONモジュール処理を開発した時、簡単で使いやすいJSON処理ライブラリ-CJSONを発見しました.
公式の紹介を見て
Welcome to cJSON. cJSON aims to be the dumbest possible parser that you can get your job done with. It’s a single file of C, and a single header file.
JSON is described best here: http://www.json.org/It’s like XML, but fat-free. You use it to move data around, store things, or just generally represent your program’s state.
As a library, cJSON exists to take away as much legwork as it can, but not get in your way. As a point of pragmatism (i.e. ignoring the truth), I’m going to say that you can use it in one of two modes: Auto and Manual. Let’s have a quick run-through.
I lifted some JSON from this page: http://www.json.org/fatfree.html That page inspired me to write cJSON, which is a parser that tries to share the same philosophy as JSON itself. Simple, dumb, out of the way.
上記のように、その使用は比較的簡単で、2つのファイルcJSONだけです.c,cJSON.h、開発するときは、あなたのプロジェクトに導入して一緒にコンパイルすればいいです.
まずcjsonのいくつかの基本的なタイプを理解します:cJSON_False,cJSON_True,cJSON_NULL,cJSON_Number,cJSON_String,cJSON_Array,cJSON_Object
1.Cjsonを使用してjsonを分析する場合は、まずcJSON_を使用するParse、JSON構造に戻ります
cJSON *json = cJSON_Parse(string);

Cjson構造は以下の通りである
typedef struct cJSON
{
    struct cJSON *next;
    struct cJSON *prev;
    struct cJSON *child;
    int type;
    char *valuestring;
    /* writing to valueint is DEPRECATED, use cJSON_SetNumberValue instead */
    int valueint;
    double valuedouble;
    char *string;
} cJSON;

2.cJSONで_GetObjectItemは、あなたが望むkeyに対応するvalueを取得します.
cJSON *json = cJSON_Parse(string);
v = cJSON_GetObjectItemCaseSensitive(json, "key");

3.type属性で返されるデータ型を判断し、異なるデータ型に応じて異なる値をとる
v->type

typeがcJSONならNumber、v->valuedoubleで数値を取りますtypeがcJSON_であればString、v->valuestringで数値をとる
4.arrayならcJSON_が必要GetArrayItem(json,index)は、cJSON*タイプを取得し、ステップ3でデータを取得します.
5.cJSON_GetArraySizeは配列のサイズを取得できます
6.解析完了後にcJSONを呼び出す必要があるDelete削除、最初のステップで戻ったポインタ
cJSON_Delete(json)

公式の例を見てみましょう
JSON例
{
    "name": "Awesome 4K",
    "resolutions": [
        {
            "width": 1280,
            "height": 720
        },
        {
            "width": 1920,
            "height": 1080
        },
        {
            "width": 3840,
            "height": 2160
        }
    ]
}

Cjson解析プログラム
int supports_full_hd(const char * const monitor)
{
    const cJSON *resolution = NULL;
    const cJSON *resolutions = NULL;
    const cJSON *name = NULL;
    int status = 0;
    cJSON *monitor_json = cJSON_Parse(monitor);
    if (monitor_json == NULL)
    {
        const char *error_ptr = cJSON_GetErrorPtr();
        if (error_ptr != NULL)
        {
            fprintf(stderr, "Error before: %s
", error_ptr); } status = 0; goto end; } name = cJSON_GetObjectItemCaseSensitive(monitor_json, "name"); if (cJSON_IsString(name) && (name->valuestring != NULL)) { printf("Checking monitor \"%s\"
", name->valuestring); } resolutions = cJSON_GetObjectItemCaseSensitive(monitor_json, "resolutions"); cJSON_ArrayForEach(resolution, resolutions) { cJSON *width = cJSON_GetObjectItemCaseSensitive(resolution, "width"); cJSON *height = cJSON_GetObjectItemCaseSensitive(resolution, "height"); if (!cJSON_IsNumber(width) || !cJSON_IsNumber(height)) { status = 0; goto end; } if ((width->valuedouble == 1920) && (height->valuedouble == 1080)) { status = 1; goto end; } } end: cJSON_Delete(monitor_json); return status; }

全体的に、Cjsonは簡単で、簡便で、学習コストがなく、JSONを分析する良いツールと言える.
Cjsonの具体的な使用例をもっと知りたいなら、私が開発したmenthol言語のJSONモジュール実装を見ることができます.
この文書は、http://www.ltplayer.com/blog/2019/01/09/2019.01.09/