jansson的使用
https://jansson.readthedocs.org/en/2.5/gettingstarted.html
https://github.com/akheron/jansson/blob/master/win32/vs2010/jansson.vcxproj
./configure
make
make check
make install
/* Create the JSON integer 42 */
json_pack("i", 42); /* Create the JSON array ["foo", "bar", true] */
json_pack("[ssb]", "foo", "bar", 1);
- json_t *json_pack(const char *fmt, ...)
- Return value: New reference.
Build a new JSON value according to the format string fmt. For each format character (except for {}[]n), one argument is consumed and used to build the corresponding value. Returns NULL on error.
- json_t *json_pack_ex(json_error_t *error, size_t flags, const char *fmt, ...)¶
- json_t *json_vpack_ex(json_error_t *error, size_t flags, const char *fmt, va_list ap)
- Return value: New reference.
Like json_pack(), but an in the case of an error, an error message is written to error, if it’s not NULL. The flags parameter is currently unused and should be set to 0.
/* Build an empty JSON object */
json_pack("{}"); /* Build the JSON object {"foo": 42, "bar": 7} */
json_pack("{sisi}", "foo", 42, "bar", 7); /* Like above, ':', ',' and whitespace are ignored */
json_pack("{s:i, s:i}", "foo", 42, "bar", 7); /* Build the JSON array [[1, 2], {"cool": true}] */
json_pack("[[i,i],{s:b]]", 1, 2, "cool", 1);
- enum json_type
-
The type of a JSON value. The following members are defined:
JSON_OBJECT JSON_ARRAY JSON_STRING JSON_INTEGER JSON_REAL JSON_TRUE JSON_FALSE JSON_NULL These correspond to JSON object, array, string, number, boolean and null. A number is represented by either a value of the type JSON_INTEGER or of the type JSON_REAL. A true boolean value is represented by a value of the type JSON_TRUE and false by a value of the type JSON_FALSE.
- int json_typeof(const json_t *json)
-
Return the type of the JSON value (a json_type cast to int). json MUST NOT be NULL. This function is actually implemented as a macro for speed.
- json_is_object(const json_t *json)
- json_is_array(const json_t *json)
- json_is_string(const json_t *json)
- json_is_integer(const json_t *json)
- json_is_real(const json_t *json)
- json_is_true(const json_t *json)
- json_is_false(const json_t *json)
- json_is_null(const json_t *json)
-
These functions (actually macros) return true (non-zero) for values of the given type, and false (zero) for values of other types and for NULL.
- json_is_number(const json_t *json)
-
Returns true for values of types JSON_INTEGER and JSON_REAL, and false for other types and for NULL.
- json_is_boolean(const json_t *json)
-
Returns true for types JSON_TRUE and JSON_FALSE, and false for values of other types and for NULL.
- int json_equal(json_t *value1, json_t *value2)
-
Returns 1 if value1 and value2 are equal, as defined above. Returns 0 if they are inequal or one or both of the pointers are NULL.
static void test_array_foreach()
{
size_t index;
json_t *array1, *array2, *value;
const char *pcValue=NULL;array1 = json_pack("[sisisi]", "foo", 1, "bar", 2, "baz", 3);
array2 = json_array();json_array_foreach(array1, index, value) {
pcValue=json_string_value(value);
if(NULL!=pcValue)
{
printf("array1[%d]=%s\n",index,pcValue);
}
pcValue=NULL;
json_array_append(array2, value);
}
if(!json_equal(array1, array2))
fail("json_array_foreach failed to iterate all elements");json_decref(array1);
json_decref(array2);
}输出:
array1[0]=foo
array1[2]=bar
array1[4]=baz- int json_unpack(json_t *root, const char *fmt, ...)
-
Validate and unpack the JSON value root according to the format string fmt. Returns 0 on success and -1 on failure.
- int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...)
- int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap)
-
Validate and unpack the JSON value root according to the format string fmt. If an error occurs and error is not NULL, write error information to error. flags can be used to control the behaviour of the unpacker, see below for the flags. Returns 0 on success and -1 on failure.
The following unpacking flags are available:
- JSON_STRICT
- Enable the extra validation step checking that all object and array items are unpacked. This is equivalent to appending the format character ! to the end of every array and object in the format string.
- JSON_VALIDATE_ONLY
- Don’t extract any data, just validate the JSON value against the given format string. Note that object keys must still be specified after the format string.
-
/* root is the JSON integer 42 */
int myint;
json_unpack(root, "i", &myint);
assert(myint == 42); /* root is the JSON object {"foo": "bar", "quux": true} */
const char *str;
int boolean;
json_unpack(root, "{s:s, s:b}", "foo", &str, "quux", &boolean);
assert(strcmp(str, "bar") == 0 && boolean == 1); /* root is the JSON array [[1, 2], {"baz": null} */
json_error_t error;
json_unpack_ex(root, &error, JSON_VALIDATE_ONLY, "[[i,i], {s:n}]", "baz");
/* returns 0 for validation success, nothing is extracted */ /* root is the JSON array [1, 2, 3, 4, 5] */
int myint1, myint2;
json_unpack(root, "[ii!]", &myint1, &myint2);
/* returns -1 for failed validation */ /* root is an empty JSON object */
int myint = 0, myint2 = 0;
json_unpack(root, "{s?i, s?[ii]}",
"foo", &myint1,
"bar", &myint2, &myint3);
/* myint1, myint2 or myint3 is no touched as "foo" and "bar" don't exist */ static void test_deep_copy_object(void)
{
const char *json_object_text =
"{\"foo\": \"bar\", \"a\": 1, \"b\": 3.141592, \"c\": [1,2,3,4]}"; json_t *object, *copy;
void *iter; object = json_loads(json_object_text, 0, NULL);
if(!object)
fail("unable to parse an object"); copy = json_deep_copy(object);
if(!copy)
fail("unable to deep copy an object");
if(copy == object)
fail("deep copying an object doesn't copy");
if(!json_equal(copy, object))
fail("deep copying an object produces an inequal copy"); iter = json_object_iter(object);
while(iter)
{
const char *key;
json_t *value1, *value2; key = json_object_iter_key(iter);
value1 = json_object_iter_value(iter);
value2 = json_object_get(copy, key); if(value1 == value2)
fail("deep copying an object doesn't copy its items"); iter = json_object_iter_next(object, iter);
} json_decref(object);
json_decref(copy);
}- int json_unpack(json_t *root, const char *fmt, ...)
-
Validate and unpack the JSON value root according to the format string fmt. Returns 0 on success and -1 on failure.
- int json_unpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, ...)
- int json_vunpack_ex(json_t *root, json_error_t *error, size_t flags, const char *fmt, va_list ap)
-
Validate and unpack the JSON value root according to the format string fmt. If an error occurs and error is not NULL, write error information to error. flags can be used to control the behaviour of the unpacker, see below for the flags. Returns 0 on success and -1 on failure.
- JSON_STRICT
- Enable the extra validation step checking that all object and array items are unpacked. This is equivalent to appending the format character ! to the end of every array and object in the format string.
- JSON_VALIDATE_ONLY
- Don’t extract any data, just validate the JSON value against the given format string. Note that object keys must still be specified after the format string.
/* root is the JSON integer 42 */
int myint;
json_unpack(root, "i", &myint);
assert(myint == 42); /* root is the JSON object {"foo": "bar", "quux": true} */
const char *str;
int boolean;
json_unpack(root, "{s:s, s:b}", "foo", &str, "quux", &boolean);
assert(strcmp(str, "bar") == 0 && boolean == 1); /* root is the JSON array [[1, 2], {"baz": null} */
json_error_t error;
json_unpack_ex(root, &error, JSON_VALIDATE_ONLY, "[[i,i], {s:n}]", "baz");
/* returns 0 for validation success, nothing is extracted */ /* root is the JSON array [1, 2, 3, 4, 5] */
int myint1, myint2;
json_unpack(root, "[ii!]", &myint1, &myint2);
/* returns -1 for failed validation */ /* root is an empty JSON object */
int myint = 0, myint2 = 0;
json_unpack(root, "{s?i, s?[ii]}",
"foo", &myint1,
"bar", &myint2, &myint3);
/* myint1, myint2 or myint3 is no touched as "foo" and "bar" don't exist *//* string */
j = json_string("foo");
rv = json_unpack(j, "s", &s);
if(rv || strcmp(s, "foo"))
fail("json_unpack string failed");
json_decref(j); /* empty object */
j = json_object();
if(json_unpack(j, "{}"))
fail("json_unpack empty object failed");
json_decref(j); /* empty list */
j = json_array();
if(json_unpack(j, "[]"))
fail("json_unpack empty list failed");
json_decref(j); /* non-incref'd object */
j = json_object();
rv = json_unpack(j, "o", &j2);
if(j2 != j || j->refcount != 1)
fail("json_unpack object failed");
json_decref(j); /* incref'd object */
j = json_object();
rv = json_unpack(j, "O", &j2);
if(j2 != j || j->refcount != 2)
fail("json_unpack object failed");
json_decref(j);
json_decref(j); /* simple object */
j = json_pack("{s:i}", "foo", 42);
rv = json_unpack(j, "{s:i}", "foo", &i1);
if(rv || i1 != 42)
fail("json_unpack simple object failed");
json_decref(j); /* simple array */
j = json_pack("[iii]", 1, 2, 3);
rv = json_unpack(j, "[i,i,i]", &i1, &i2, &i3);
if(rv || i1 != 1 || i2 != 2 || i3 != 3)
fail("json_unpack simple array failed");
json_decref(j);By default, Jansson uses malloc() and free() for memory allocation. These functions can be overridden if custom behavior is needed.
- json_malloc_t
-
A typedef for a function pointer with malloc()‘s signature:
typedef void *(*json_malloc_t)(size_t);
- json_free_t
-
A typedef for a function pointer with free()‘s signature:
typedef void (*json_free_t)(void *);
- void json_set_alloc_funcs(json_malloc_t malloc_fn, json_free_t free_fn)
-
Use malloc_fn instead of malloc() and free_fn instead of free(). This function has to be called before any other Jansson’s API functions to ensure that all memory operations use the same functions.
Examples:
Use the Boehm’s conservative garbage collector for memory operations:
json_set_alloc_funcs(GC_malloc, GC_free);
- int json_object_update(json_t *object, json_t *other)¶
-
Update object with the key-value pairs from other, overwriting existing keys. Returns 0 on success or -1 on error.
- int json_object_update_existing(json_t *object, json_t *other)
-
Like json_object_update(), but only the values of existing keys are updated. No new keys are created. Returns 0 on success or -1 on error.
New in version 2.3.
- int json_object_update_missing(json_t *object, json_t *other)
-
Like json_object_update(), but only new keys are created. The value of any existing key is not changed. Returns 0 on success or -1 on error.
jansson的使用的更多相关文章
- 安装jansson库【JSON库C语言版】
本次操作在Ubuntu 14.04下进行,其他的系统大同小异,安装软件时请根据系统版本进行调整. 1.下载jansson源码: git clone https://github.com/akheron ...
- linux 下jansson安装和使用
1.安装jansson ./configure make make install 2.生成帮助文档 cd doc make html 编译安装doc时提示 spinx-build not a com ...
- JSON 下 -- jansson 示例
JSON 下 —— jansson 示例 参考网址: jansson 库的下载: http://www.digip.org/jansson/ 安装jansson 步骤: http://blog.csd ...
- Jansson库的使用简介
一.Jansson的安装: 二.jansson相关的API: https://jansson.readthedocs.io/en/latest/apiref.html#c.json_t string ...
- 在Ubuntu X64上编译Hadoop
在之前的文章中介绍了如何直接在Ubuntu中安装Hadoop.但是对于64位的Ubuntu来说,官方给出的Hadoop包是32位的,运行时会得到警告: WARN util.NativeCodeLoad ...
- 值得推荐的C/C++框架和库
值得推荐的C/C++框架和库 [本文系外部转贴,原文地址:http://coolshell.info/c/c++/2014/12/13/c-open-project.htm]留作存档 下次造轮子前先看 ...
- [转]C/C++ 程序员必须收藏的资源大全
from: https://github.com/jobbole/awesome-cpp-cn C++ 资源大全中文版 我想很多程序员应该记得 GitHub 上有一个 Awesome – XXX 系列 ...
- [转载]C/C++框架和库
C/C++框架和库 装载自:http://blog.csdn.net/xiaoxiaoyeyaya/article/details/42541419 值得学习的C语言开源项目 Webbench Web ...
- kafka 搭建与使用
消息量非超级多不建议使用,可以使用redis或Beanstalkd 使用简单 Beanstalkd 客户端建议用:composer require pda/pheanstalk 如果无JAVA JDK ...
随机推荐
- BZOJ3473 字符串 广义后缀自动机
今天主攻了下SAM 好多东西以前都没理解到 对于这道题 我们建一个自动机存所有串 每个穿last从1开始 对于自动机上每个点额外记一个cnt 表示能匹配到这个点的不同串个数 建完对每个串在自动机上匹配 ...
- Dijkstra_Liu博客100篇祭
创建博客,有两年三个月了.今天,写了100篇随笔了,又正值我的15岁生日,还是值得纪念一下. 两年过去了,我从学习:队列.栈.模拟.背包慢慢地变成了:Tarjan.线段树.树剖. 我也从一个初一的天真 ...
- LogStash日志分析系统
简介 通常日志管理是逐渐崩溃的——当日志对于人们最重要的时候,也就是出现问题的时候,这个渐进的过程就开始了.日志管理一般会经历一下3个阶段: 初级管理员将通过一些传统工具(如cat.tail.sed. ...
- Polly简介 — 2. 弹性策略
和故障处理策略不同的是,弹性策略并不是针对委托执行过程中的异常进行处理,而是改变委托本身的行为,因此弹性策略并没有故障定义这一过程,它的处理流程为: 定义策略 应用策略 Polly对弹性策略也做了不少 ...
- VisualSVN Server 修改用户密码
VisualSVN Server是非常方便好用的SVN服务器端软件,但有个问题,你在服务器端创建了用户名密码后,用户无法自己修改密码,据说VisualSVN的客户端可以修改用户密码,但客户端是收费软件 ...
- MVC把表格导出到Excel
有关Model: namespace MvcApplication1.Models { public class Coach { public int Id { get; set; } public ...
- [翻译] EnterTheMatrix
Enter The Matrix https://github.com/mpospese/EnterTheMatrix The sample application to accompany my c ...
- dwr3实现消息精确推送详细步骤
最近项目中需要用到推送消息,找了很久终于找到一篇不错的文章,方便以后查看就转载了,也分享给大家,希望能帮到有需要的人. 第一.在项目中引入dwr.jar,然后在web.xml中进行配置,配置如下: & ...
- vijos p1729 Knights
描述 在一个N*N的正方形棋盘上,放置了一些骑士.我们将棋盘的行用1开始的N个自然数标记,将列用'A'开始的N个大写英文字母标记.举个例子来说,一个标准的8*8的国际象棋棋盘的行标记为1..8,列标记 ...
- Android数字选择器-NumberPicker
数字选择器NumberPicker是Android3.0之后引入的一个控件,比较常用,比如说手机常用的闹钟,可以选择小时和分钟,如果你需要兼容3.0之前版本,GitHub上有开源的项目,具体的下载地址 ...