没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
Jansson API 参 考 ( 仅 供 参 考 )
一、准备
所有的函数功能都在 jansson.h 里面,所以:
请包涵:
#incluede <jansson.h>
所有常数前缀都是用 JSON_开头(描述 library 版本信息由 JANSSON_引导)
其他的标示符有 json_开始。
类型的后缀是_t。
二、库的版本(略)
三、参数表示
由于 JSON 规格书中定义的数据类型:object, array, string, number, boolean, and
null.
并且 JSON 中的数据类型正在不断的扩充,所以哦,我们把所有的类型都放在一个 C type
里面,而且这个结构体包含所有 json 类型
Json_t
Value Representation
Json 类型:object,array,string,number,boolean, andnull.
json_t 包含所有 json 数据类型
如果发生错误,除非另有说明,所有的 API 函数返回一个错误值。
根据函数的原型,Error Value 是 NULL 或-1。无效的参数或输入无效的错误是
显而易见的来源。内存分配和 I / O 操作也可能会导致错误。
TYPE
使用以下功能查询和测试一个 JSON 值类型:
enumjson_type
JSON_OBJECT
JSON_ARRAY
JSON_STRING
JSON_INTEGER
JSON_REAL
JSON_TRUE
JSON_FALSE
JSON_NULL
这些对应的 JSON 对象,数组,字符串,数字,布尔和 null。其中 number 代表了的不同的 json
type,JSON_INTEGER 或 JSON_REAL 的任何一个值。一个真正的布尔值,表示由一个值的类
型 JSON_TRUE 和假值类型 JSON_FALSE。
intjson_typeof(constjson_t*json)
返回 JSON 值的类型(的 json_type 转换为 int)。JSON
不能为 null。这个功能实际上是作为宏观调控的速度实现。
json_is_object(constjson_t*json)
json_is_array(constjson_t*json)
json_is_string(constjson_t*json)
json_is_integer(constjson_t*json)
json_is_real(constjson_t*json)
json_is_true(constjson_t*json)
json_is_false(constjson_t*json)
json_is_null(constjson_t*json)
该函数功能是非零类型返回 true,零或空返回 false
json_is_number(constjson_t*json)
为¡JSON_INTEGER 或者 JSON_REAL 返回 true,false 则是其他类型或
者 NULL
json_is_boolean(constjson_t*json)
Returns true for typesJSON_TRUEandJSON_FALSE, and false for
values of other types and forNULL.
Reference Count(引用计数)
引用计数是用来跟踪某个值是否仍然是在使用或不使用。
当创建一个值,它的引用计数设置为 1。如果参考值保持(如值存储供
以后使用的地方),其引用计数递增,并且不再需要该值时,引用计数
递减。
当引用计数下降到零,没有留下的引用,可以被摧毁的价值。
以下函数用于处理的引用计数。
json_t*json_incref(json_t*json)
如果它不是非 NULL,增加 JSON 的引用计数。最终返回 JSON。
voidjson_decref(json_t*json)
递减 JSON 引用计数。只要一旦调用以 json_decref()后,引用计数下
降到零时,该值被破坏,不能再被使用。
函数创建新的 JSON 并把引用计数值设置为 1。这些函数被调用返回一
个新的引用。其他函数返回的 JSON 值(现有的),通常不会增加引用
计数。这些函数被调用,归还借用的参考。因此,如果用户将持有参考
返回借来的参考值,他必须调用 json_incref()。值一旦不再需要,应该
被称为 json_decref()释放引用。
通常情况下,所有函数的接受一个 JSON 作为参数值将管理引用,即增
加和减小所需要的引用计数作。但是,某些函数窃取引用,也就是说,
如果用户被调用 json_decref()后调用函数的参数,它们有相同的结果。
这些函数是后缀_new 或者有_new_的某处他们的名字。
例如,下面的代码创建了一个新的 JSON 数组并追加一个整数:
json_t *array, *integer;
array = json_array();
integer = json_integer(42);
json_array_append(array, integer);
json_decref(integer);
注意如何调用者的整数的值通过调用 json_decref()释放参考。通过使
用 参 考 盗 取 函 数 json_array_append_new ( ) 而 不 是
json_array_append(),代码变得简单得多:
json_t *array = json_array();
json_array_append_new(array, json_integer(42));
在 这 种 情 况 下 , 用 户 不 具 有 明 确 地 释 放 为 整 数 值 , , 作 为
json_array_append_new()窃取时的参考值附加到数组。
在下面的章节中明确记载是否函数会返回一个新的或借来的参考,或窃
取其参数的引用。
Circular References¶
A circular reference is created when an object or an array is, directly or
indirectly, inserted inside itself. The direct case is simple:
json_t *obj = json_object();
json_object_set(obj, "foo", obj);
Jansson will refuse to do this, andjson_object_set()(and all the other
such functions for objects and arrays) will return with an error status. The
indirect case is the dangerous one:
json_t *arr1 = json_array(), *arr2 = json_array();
json_array_append(arr1, arr2); json_array_append(arr2, arr1);
In this example, the arrayarr2is contained in the arrayarr1, and vice versa.
Jansson cannot check for this kind of indirect circular references without a
performance hit, so it’s up to the user to avoid them.
If a circular reference is created, the memory consumed by the values cannot
be freed byjson_decref(). The reference counts never drops to zero
because the values are keeping the references to each other. Moreover,
trying to encode the values with any of the encoding functions will fail. The
encoder detects circular references and returns an error status.
True, False and Null
实现的这三个值是单例,所以返回的指针调用这些函数之间也不会改变。
json_t*json_true(void)
Return value: New reference.
Returns the JSON true value.
json_t*json_false(void)
Return value: New reference.
Returns the JSON false value.
json_t*json_boolean(val)
Return value: New reference.
Returns JSON false ifvalis zero, and JSON true otherwise. This is a
macro, and equivalent toval.?.json_true().:.json_false().
New in version 2.4.
json_t*json_null(void)
Return value: New reference.
Returns the JSON null value.
String¶
Jansson uses UTF-8 as the character encoding. All JSON strings must be
valid UTF-8 (or ASCII, as it’s a subset of UTF-8). Normal null terminated C
strings are used, so JSON strings may not contain embedded null characters.
All other Unicode codepoints U+0001 through U+10FFFF are allowed.
json_t*json_string(const char*value)
Return value: New reference.
Returns a new JSON string, orNULLon error.valuemust be a valid
UTF-8 encoded Unicode string.
json_t*json_string_nocheck(const char*value)
Return value: New reference.
Likejson_string(), but doesn’t check thatvalueis valid UTF-8. Use this
function only if you are certain that this really is the case (e.g. you have
already checked it by other means).
const char *json_string_value(constjson_t*string)
Returns the associated value ofstringas a null terminated UTF-8
encoded string, orNULLifstringis not a JSON string.
The retuned value is read-only and must not be modified or freed by
the user. It is valid as long asstringexists, i.e. as long as its reference
count has not dropped to zero.
剩余26页未读,继续阅读
资源评论
- tianwaifeishi172013-10-21Jansson开发文档,不错
- Zaki_huang2018-04-15还可以,能用的
- longfeiq2013-11-23Jansson开发文档,没有预想的好
朔望两弦
- 粉丝: 12
- 资源: 9
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- AIS2024 valid
- 最入门的爬虫代码 python.docx
- 爬虫零基础入门-爬取天气预报.pdf
- 最通俗易懂的 MongoDB 非结构化文档存储数据库教程.zip
- 以mongodb为数据库的订单物流小项目.zip
- 腾讯云-mongodb数据库, 项目部署.zip
- 腾讯 APIJSON 的 MongoDB 数据库插件.zip
- 理解非关系型数据库和关系型数据库的区别.zip
- 操作简单的Mongodb网页web管理工具,基于Spring Boot2.0支持mongodb集群.zip
- tms-mongodb-web,提供访问mongodb数据的REST API和可灵活扩展的mongodb web 客户端.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功