diff options
author | Petri Lehtinen <petri@digip.org> | 2009-09-03 17:56:00 +0300 |
---|---|---|
committer | Petri Lehtinen <petri@digip.org> | 2009-09-04 09:52:37 +0300 |
commit | 89d09813c376a1ba59a5fd015dd905e2778c7407 (patch) | |
tree | 1bce355601fc6fa68012340252367208fe5e94c2 /src | |
parent | 5ac491464256790b46ebffbf0e20e4c4f47a5bb2 (diff) | |
download | jansson-89d09813c376a1ba59a5fd015dd905e2778c7407.zip jansson-89d09813c376a1ba59a5fd015dd905e2778c7407.tar.gz jansson-89d09813c376a1ba59a5fd015dd905e2778c7407.tar.bz2 |
Add reference stealing functions for inserting values to objects and arrays
The non-stealing functions are now just simple wrappers around these.
Diffstat (limited to 'src')
-rw-r--r-- | src/jansson.h | 25 | ||||
-rw-r--r-- | src/value.c | 47 |
2 files changed, 60 insertions, 12 deletions
diff --git a/src/jansson.h b/src/jansson.h index 6efc6db..09af7bf 100644 --- a/src/jansson.h +++ b/src/jansson.h @@ -72,17 +72,36 @@ static inline void json_decref(json_t *json) /* getters, setters, manipulation */ json_t *json_object_get(const json_t *object, const char *key); -int json_object_set(json_t *object, const char *key, json_t *value); +int json_object_set_new(json_t *object, const char *key, json_t *value); int json_object_del(json_t *object, const char *key); void *json_object_iter(json_t *object); void *json_object_iter_next(json_t *object, void *iter); const char *json_object_iter_key(void *iter); json_t *json_object_iter_value(void *iter); +static inline +int json_object_set(json_t *object, const char *key, json_t *value) +{ + return json_object_set_new(object, key, json_incref(value)); +} + unsigned int json_array_size(const json_t *array); json_t *json_array_get(const json_t *array, unsigned int index); -int json_array_set(json_t *array, unsigned int index, json_t *value); -int json_array_append(json_t *array, json_t *value); +int json_array_set_new(json_t *array, unsigned int index, json_t *value); +int json_array_append_new(json_t *array, json_t *value); + +static inline +int json_array_set(json_t *array, unsigned int index, json_t *value) +{ + return json_array_set_new(array, index, json_incref(value)); +} + +static inline +int json_array_append(json_t *array, json_t *value) +{ + return json_array_append_new(array, json_incref(value)); +} + const char *json_string_value(const json_t *json); int json_integer_value(const json_t *json); diff --git a/src/value.c b/src/value.c index fda0b03..d83d575 100644 --- a/src/value.c +++ b/src/value.c @@ -118,23 +118,40 @@ json_t *json_object_get(const json_t *json, const char *key) return hashtable_get(&object->hashtable, key); } -int json_object_set_nocheck(json_t *json, const char *key, json_t *value) +int json_object_set_new_nocheck(json_t *json, const char *key, json_t *value) { json_object_t *object; if(!json_is_object(json)) + { + json_decref(value); return -1; - + } object = json_to_object(json); - return hashtable_set(&object->hashtable, strdup(key), json_incref(value)); + + if(hashtable_set(&object->hashtable, strdup(key), value)) + { + json_decref(value); + return -1; + } + + return 0; +} + +int json_object_set_nocheck(json_t *json, const char *key, json_t *value) +{ + return json_object_set_new_nocheck(json, key, json_incref(value)); } -int json_object_set(json_t *json, const char *key, json_t *value) +int json_object_set_new(json_t *json, const char *key, json_t *value) { if(!utf8_check_string(key, -1)) + { + json_decref(value); return -1; + } - return json_object_set_nocheck(json, key, value); + return json_object_set_new_nocheck(json, key, value); } int json_object_del(json_t *json, const char *key) @@ -235,37 +252,49 @@ json_t *json_array_get(const json_t *json, unsigned int index) return array->table[index]; } -int json_array_set(json_t *json, unsigned int index, json_t *value) +int json_array_set_new(json_t *json, unsigned int index, json_t *value) { json_array_t *array; if(!json_is_array(json)) + { + json_decref(value); return -1; + } array = json_to_array(json); if(index >= array->entries) + { + json_decref(value); return -1; + } json_decref(array->table[index]); - array->table[index] = json_incref(value); + array->table[index] = value; return 0; } -int json_array_append(json_t *json, json_t *value) +int json_array_append_new(json_t *json, json_t *value) { json_array_t *array; if(!json_is_array(json)) + { + json_decref(value); return -1; + } array = json_to_array(json); if(array->entries == array->size) { array->size = max(8, array->size * 2); array->table = realloc(array->table, array->size * sizeof(json_t *)); if(!array->table) + { + json_decref(value); return -1; + } } - array->table[array->entries] = json_incref(value); + array->table[array->entries] = value; array->entries++; return 0; |