Libjansson: Difference between revisions
Jump to navigation
Jump to search
| (2 intermediate revisions by the same user not shown) | |||
| Line 114: | Line 114: | ||
} | } | ||
</source> | </source> | ||
== json_object == | |||
=== json_object_foreach === | |||
<pre> | |||
json_object_foreach(object, key, value) | |||
</pre> | |||
json_object 에 포함된 모든 key 와 value 항목들을 확인하고자 할 때 유용하다. | |||
<pre> | |||
/* obj is a JSON object */ | |||
const char *key; | |||
json_t *value; | |||
json_object_foreach(obj, key, value) { | |||
/* block of code that uses key and value */ | |||
} | |||
</pre> | |||
=== json_object_foreach_safe === | |||
기본적인 내용은 json_object_foreach 와 같으나, json_object_del() 을 수행해도 안전한 함수이다. libjansson-2.8 버전부터 지원한다. | |||
<pre> | |||
json_object_foreach_safe(object, tmp, key, value) | |||
Like json_object_foreach(), but it’s safe to call json_object_del(object, key) during iteration. | |||
You need to pass an extra void * parameter tmp that is used for temporary storage. | |||
New in version 2.8. | |||
The following functions implement an iteration protocol for objects, allowing to iterate through all key-value pairs in an object. | |||
The items are not returned in any particular order, as this would require sorting due to the internal hashtable implementation. | |||
</pre> | |||
=== json_object_update === | |||
<pre> | |||
int json_object_update(json_t *object, json_t *other) | |||
</pre> | |||
<nowiki>*object 항목의 key와 value 를 *other 항목의 key 와 value 항목의 값으로 업데이트 한다. 덮어쓰기가 되며, 성공시 0, 에러 발생시 -1을 리턴한다.</nowiki> | |||
== References == | == References == | ||
Latest revision as of 09:49, 24 November 2015
Overview
Json library Jansson 정보 정리.
API manual
json_array
json_array_foreach
json_array_foreach() 함수는 json array 에 있는 모든 요소들을 하나씩 살펴볼 때 사용하면 유용하다. 하지만, 루프를 도는 도중, 입력된 array의, 항목이 삭제가 되는 등의 변화가 발생하게 되면 삭제와 동시에 내부 정렬이 다시 초기화 되면서 의도치 않는 결과가 나오게 된다.
따라서, json_array_foreach() 함수의 사용도중에 항목을 삭제하기 위해서는 많은 주의가 요구된다.
- Example(loop 를 돌며 array 내용 지우기 예제)
<source lang=c>
- define _GNU_SOURCE
- include <stdio.h>
- include <stdlib.h>
- include <string.h>
- include <jansson.h>
int get_array_idx(const json_t* j_arr, const char* value) {
int ret;
json_t* j_tmp;
int idx;
const char* tmp;
json_array_foreach(j_arr, idx, j_tmp)
{
if(j_tmp == NULL)
{
printf("Err.\n");
}
tmp = json_string_value(j_tmp);
ret = strcmp(tmp, value);
if(ret == 0)
{
break;
}
}
ret = json_array_size(j_arr);
if(idx >= ret)
{
return -1;
}
return idx;
}
int main(int argc, char** argv)
{
json_t* j_arr;
json_t* j_err;
json_t* j_tmp;
char* tmp;
const char* tmp_value;
int idx;
int i;
int ret;
j_arr = json_array();
for(i = 0; i < 10; i++)
{
asprintf(&tmp, "%d", i);
json_array_append_new(j_arr, json_string(tmp));
free(tmp);
}
tmp = json_dumps(j_arr, JSON_ENCODE_ANY);
printf("result. tmp[%s]\n", tmp);
free(tmp);
// check errors.
j_err = json_array();
json_array_foreach(j_arr, idx, j_tmp)
{
if(j_tmp == NULL)
{
printf("Err.\n");
}
if((idx == 3) || (idx == 5))
{
json_array_append(j_err, j_tmp);
}
}
json_array_foreach(j_err, idx, j_tmp)
{
tmp_value = json_string_value(j_tmp);
ret = get_array_idx(j_arr, tmp_value);
if(ret == -1)
{
continue;
}
json_array_remove(j_arr, ret);
}
tmp = json_dumps(j_arr, JSON_ENCODE_ANY);
printf("result. tmp[%s]\n", tmp);
free(tmp);
json_decref(j_arr);
json_decref(j_err);
return 0;
} </source>
json_object
json_object_foreach
json_object_foreach(object, key, value)
json_object 에 포함된 모든 key 와 value 항목들을 확인하고자 할 때 유용하다.
/* obj is a JSON object */
const char *key;
json_t *value;
json_object_foreach(obj, key, value) {
/* block of code that uses key and value */
}
json_object_foreach_safe
기본적인 내용은 json_object_foreach 와 같으나, json_object_del() 을 수행해도 안전한 함수이다. libjansson-2.8 버전부터 지원한다.
json_object_foreach_safe(object, tmp, key, value)
Like json_object_foreach(), but it’s safe to call json_object_del(object, key) during iteration.
You need to pass an extra void * parameter tmp that is used for temporary storage.
New in version 2.8.
The following functions implement an iteration protocol for objects, allowing to iterate through all key-value pairs in an object.
The items are not returned in any particular order, as this would require sorting due to the internal hashtable implementation.
json_object_update
int json_object_update(json_t *object, json_t *other)
*object 항목의 key와 value 를 *other 항목의 key 와 value 항목의 값으로 업데이트 한다. 덮어쓰기가 되며, 성공시 0, 에러 발생시 -1을 리턴한다.
References
<references />