• json-c 理解记录


    1. json-c 理解记录

    1.1. 编译及说明

    一个 C 库,多用于嵌入式系统

    • 编译源码及文档
    mkdir build
    cd build
    cmake ..
    make
    make doc
    
    • 1
    • 2
    • 3
    • 4
    • 5

    1.2. 特色

    • json-c 默认不支持多线程,可以通过编译参数 DENABLE_THREADING 开启,但是开启后仅限于 json_object_get 和 json_object_put 函数。据说会慢3倍

    1.3. 使用

    1.3.1. 创建,读写文件

    创建的 json_object 都需要通过 json_object_put 释放

    struct json_object *    json_object_new_object (void);  // key:val
    struct json_object *    json_object_new_array (void);
    struct json_object *    json_object_new_boolean (json_bool b);
    struct json_object *    json_object_new_int (int32_t i);
    struct json_object *    json_object_new_int64 (int64_t i);
    struct json_object *    json_object_new_double (double d);
    struct json_object *    json_object_new_double_s (double d, const char *ds);
    struct json_object *    json_object_new_string (const char *s);
    struct json_object *    json_object_new_string_len (const char *s, int len);
    // 字符串到json对象,与之对应的是 json_object_to_json_string 函数
    struct json_object *    json_tokener_parse(const char *str);
    // read from file
    struct json_object*     json_object_from_file(const char *filename);
    struct json_object*     json_object_from_fd(int fd);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    // write to file
    int                     json_object_to_file(const char *filename, struct json_object *obj);
    int                     json_object_to_file_ext(const char *filename, struct json_object *obj, int flags);
    int                     json_object_to_fd(int fd, struct json_object *obj, int flags);
    
    • 1
    • 2
    • 3
    • 4

    1.3.2. 拷贝

    // 引用拷贝,引用计数+1
    json_object * json_object_get (struct json_object *obj);
    // 引用释放,引用计数-1,引用计数为0释放内存,返回值为1
    int json_object_put (struct json_object *obj);
    
    • 1
    • 2
    • 3
    • 4

    1.3.3. 增改

    1.3.3.1. 字典增加元素
    // 字典增加元素
    int json_object_object_add (struct json_object *obj, const char *key, struct json_object *val);
    // 扩展版本,支持一些选项,用于内存优化,比如 opts=JSON_C_OBJECT_ADD_KEY_IS_NEW 或 JSON_C_OBJECT_KEY_IS_CONSTANT
    int json_object_object_add_ex (struct json_object *obj, const char *const key, struct json_object *const val, const unsigned opts);
    
    • 1
    • 2
    • 3
    • 4
    1.3.3.2. 数组增加修改元素
    // 数组增加元素
    int json_object_array_add (struct json_object *obj, struct json_object *val);
    // 替换数组中元素
    int json_object_array_put_idx (struct json_object *obj, size_t idx, struct json_object *val);
    
    • 1
    • 2
    • 3
    • 4

    1.3.4. 字典数组删除元素

    void json_object_object_del (struct json_object *obj, const char *key);
    int json_object_array_del_idx(struct json_object *obj, size_t idx, size_t count);
    
    • 1
    • 2

    1.3.5. 查

    1.3.5.1. 类型
    // 类型操作
    enum json_type json_object_get_type (const struct json_object *obj);
    int json_object_is_type(const struct json_object *obj, enum json_type type);
    extern const char *json_type_to_name(enum json_type o_type);
    
    • 1
    • 2
    • 3
    • 4
    1.3.5.2. 数组
    // 数组操作
    struct array_list * json_object_get_array (const struct json_object *obj);
    size_t json_object_array_length (const struct json_object *obj);
    struct json_object * json_object_array_get_idx (const struct json_object *obj, size_t idx);
    
    • 1
    • 2
    • 3
    • 4
    1.3.5.3. 字典
    // 字典操作
    int json_object_object_length (const struct json_object *obj);
    // 不建议使用,推介使用 json_object_object_get_ex 代替
    struct json_object* json_object_object_get(const struct json_object* obj, const char *key);
    // 获取同时,判断是否存在,其他也有类似函数,可以查找。这里无需通过 json_object_put 释放
    json_bool json_object_object_get_ex(const struct json_object* obj, const char *key, struct json_object **value);
    // 格式化成字符串输出
    const char * json_object_to_json_string (struct json_object *obj);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    1.3.5.4. 其他标准类型
    // 其他操作
    json_bool json_object_get_boolean (const struct json_object *obj);
    int32_t json_object_get_int (const struct json_object *obj);
    int64_t json_object_get_int64 (const struct json_object *obj);
    double json_object_get_double (const struct json_object *obj);
    const char * json_object_get_string (struct json_object *obj);
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    1.3.6. 排序

    // 数组排序,自己写比较函数
    void json_object_array_sort (struct json_object *jso, int(*sort_fn)(const void *, const void *));
    
    • 1
    • 2

    1.3.7. other

    const char *json_util_get_last_err(void);
    extern int json_parse_int64(const char *buf, int64_t *retval);
    extern int json_parse_double(const char *buf, double *retval);
    
    • 1
    • 2
    • 3

    1.4. 参考地址

  • 相关阅读:
    SpringBoot读取Yml中基本数据类型、List、Map、数组数据
    【毕业设计】深度学习手势识别 - yolo python opencv cnn 机器视觉
    RabbitMq中的warren模式和shovel模式
    七日算法先导(七)——字符串
    pdf怎么转换成jpg图片?收藏这几种方法
    MySQL备份与恢复
    MongoDB入门与实战-第一章-介绍
    docker Harbor的概述部署
    ffmpeg5及以上-s和像素格式转换 画屏问题
    发表博客的模板
  • 原文地址:https://blog.csdn.net/knowledgebao/article/details/126650825