json-c 理解记录

1. json-c 理解记录

1.1. 编译及说明

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

  • 编译源码及文档
mkdir build
cd build
cmake ..
make
make doc

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);
// 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.3.2. 拷贝

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

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.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.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.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.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.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.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.3.6. 排序

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

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值