最好用的C语言JSON解析器

本文介绍开源C语言库Melon(https://github.com/Water-Melon/Melon)的JSON解析器。

相信很多读者都听说过甚至使用过cJSON开源库。那么本文就拿cJSON与Melon的JSON组件进行对比。

下面我们就来一起看一看。

编码Encode

假设我们要构建如下JSON:

{
    "name": "Awesome 4K",
    "resolutions": [
        {
            "width": 1280,
            "height": 720
        },
        {
            "width": 1920,
            "height": 1080
        },
        {
            "width": 3840,
            "height": 2160
        }
    ]
}

那么,我们先看一下cJSON的构建代码:

#include 
#include 

//NOTE: Returns a heap allocated string, you are required to free it after use.
char *create_monitor_with_helpers(void)
{
    const unsigned int resolution_numbers[3][2] = {
        {1280, 720},
        {1920, 1080},
        {3840, 2160}
    };
    char *string = NULL;
    cJSON *resolutions = NULL;
    size_t index = 0;

    cJSON *monitor = cJSON_CreateObject();

    if (cJSON_AddStringToObject(monitor, "name", "Awesome 4K") == NULL)
    {
        goto end;
    }

    resolutions = cJSON_AddArrayToObject(monitor, "resolutions");
    if (resolutions == NULL)
    {
        goto end;
    }

    for (index = 0; index < (sizeof(resolution_numbers) / (2 * sizeof(int))); ++index)
    {
        cJSON *resolution = cJSON_CreateObject();

        if (cJSON_AddNumberToObject(resolution, "width", resolution_numbers[index][0]) == NULL)
        {
            goto end;
        }

        if (cJSON_AddNumberToObject(resolution, "height", resolution_numbers[index][1]) == NULL)
        {
            goto end;
        }

        cJSON_AddItemToArray(resolutions, resolution);
    }

    string = cJSON_Print(monitor);
    if (string == NULL)
    {
        fprintf(stderr, "Failed to print monitor.
");
    }

end:
    cJSON_Delete(monitor);
    return string;
}

int main(void)
{
    char *p;
    p = create_monitor_with_helpers();
    printf("%s
", p);
    return 0;
}

下面,我们一起看下使用Melon的JSON组件的代码:

#include 
#include "mln_json.h"
#include "mln_log.h"

static mln_string_t *generate(void)
{
    mln_json_t j;
    mln_string_t *ret;

    mln_json_init(&j);

    mln_json_generate(&j, "{s:s,s:[{s:d,s:d},{s:d,s:d},{s:d,s:d}]}", 
        "name", "Awesome 4K", "resolutions", "width", 1280, "height", 720, 
        "width", 1920, "height", 1080, "width", 3840, "height", 2160);
    ret = mln_json_encode(&j);

    mln_json_destroy(&j);

    return ret;
}

int main(void)
{
    mln_string_t *p;
    p = generate();
    mln_log(none, "%S
", p);
    return 0;
}

解码Decode

假设我们有如下JSON:

{
    "name": "Awesome 4K",
    "resolutions": [
        {
            "width": 1280,
            "height": 720
        }
    ]
}

下面一起来看下解码,还是先上cJSON的代码:

#include 
#include 

/* return 1 if the monitor supports full hd, 0 otherwise */
int supports_full_hd(const char * const monitor)
{
    const cJSON *resolution = NULL;
    const cJSON *resolutions = NULL;
    cJSON *monitor_json = cJSON_Parse(monitor);
    if (monitor_json == NULL)
        return -1;

    resolutions = cJSON_GetObjectItemCaseSensitive(monitor_json, "resolutions");
    cJSON_ArrayForEach(resolution, resolutions)
    {
        cJSON *width = cJSON_GetObjectItemCaseSensitive(resolution, "width");
        return width->valuedouble;
    }

    cJSON_Delete(monitor_json);
    return -1;
}

int main(void)
{
    char p[] = "{"name":"Awesome 4K","resolutions":[{"width":1280,"height":720}]}";
    int i = supports_full_hd(p);
    printf("%d
", i);
    return 0;
}

接下来是Melon的JSON组件的解码代码:

#include 
#include "mln_json.h"
#include "mln_log.h"

static int handler(mln_json_t *j, void *data)
{
    return (int)mln_json_number_data_get(j);
}

static int parse(mln_string_t *p)
{
    mln_json_t j;
    mln_string_t exp = mln_string("resolutions.0.width");
    mln_json_decode(p, &j);
    return mln_json_parse(&j, &exp, handler, NULL);
}

int main(void)
{
    mln_string_t p = mln_string("{"name":"Awesome 4K","resolutions":[{"width":1280,"height":720}]}");
    int i = parse(&p);
    mln_log(none, "%d
", i);
    return 0;
}

结语

Melon的JSON组件主要提供了如下四个函数来便于使用者构建和解析JSON:

Melon的JSON组件提供了易于阅读和使用的函数接口,更易于开发者对项目的维护。

欢迎大家来试用开源C语言库Melon。

Github:https://github.com/Water-Melon/Melon

展开阅读全文

页面更新:2024-03-13

标签:语言   结点   结语   表达式   字符串   函数   组件   本文   结构   代码

1 2 3 4 5

上滑加载更多 ↓
推荐阅读:
友情链接:
更多:

本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828  

© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号

Top