• 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. 参考地址

  • 相关阅读:
    uniapp前端对数据的处理
    Ansible自动化
    aj-report页面嵌入其他项目
    linux安装MySql
    全网最全JAVA面试八股文,终于整理完了
    数字签名与数字信封
    【一江水 一家人】 盘龙区打造铸牢中华民族共同体意识盘龙江示范带
    DASCTF X GFCTF 2022十月挑战赛web
    C++lambda表达式
    WordPress主题开发( 八)之—— 模板循环详细用法
  • 原文地址:https://blog.csdn.net/knowledgebao/article/details/126650825