aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorPetri Lehtinen <petri@digip.org>2009-10-11 20:42:43 +0300
committerPetri Lehtinen <petri@digip.org>2009-10-11 20:44:01 +0300
commit1e00cd58a514a61e829e639f1e40dac94a334561 (patch)
tree030dc7824942dd289627d77b5f5891f6a0e73ba9 /src
parent40bb7bf4378e09570f3921cd76cdce6acc873259 (diff)
downloadjansson-1e00cd58a514a61e829e639f1e40dac94a334561.zip
jansson-1e00cd58a514a61e829e639f1e40dac94a334561.tar.gz
jansson-1e00cd58a514a61e829e639f1e40dac94a334561.tar.bz2
Extend object API
Added functions: json_object_size json_object_clear json_object_update
Diffstat (limited to 'src')
-rw-r--r--src/hashtable.c47
-rw-r--r--src/hashtable.h9
-rw-r--r--src/jansson.h3
-rw-r--r--src/value.c48
4 files changed, 94 insertions, 13 deletions
diff --git a/src/hashtable.c b/src/hashtable.c
index afb3304..05dc167 100644
--- a/src/hashtable.c
+++ b/src/hashtable.c
@@ -133,6 +133,23 @@ static int hashtable_do_del(hashtable_t *hashtable,
return 0;
}
+static void hashtable_do_clear(hashtable_t *hashtable)
+{
+ list_t *list, *next;
+ pair_t *pair;
+
+ for(list = hashtable->list.next; list != &hashtable->list; list = next)
+ {
+ next = list->next;
+ pair = list_to_pair(list);
+ if(hashtable->free_key)
+ hashtable->free_key(pair->key);
+ if(hashtable->free_value)
+ hashtable->free_value(pair->value);
+ free(pair);
+ }
+}
+
static int hashtable_do_rehash(hashtable_t *hashtable)
{
list_t *list, *next;
@@ -220,19 +237,7 @@ int hashtable_init(hashtable_t *hashtable,
void hashtable_close(hashtable_t *hashtable)
{
- list_t *list, *next;
- pair_t *pair;
- for(list = hashtable->list.next; list != &hashtable->list; list = next)
- {
- next = list->next;
- pair = list_to_pair(list);
- if(hashtable->free_key)
- hashtable->free_key(pair->key);
- if(hashtable->free_value)
- hashtable->free_value(pair->value);
- free(pair);
- }
-
+ hashtable_do_clear(hashtable);
free(hashtable->buckets);
}
@@ -292,6 +297,22 @@ int hashtable_del(hashtable_t *hashtable, const void *key)
return hashtable_do_del(hashtable, key, hash);
}
+void hashtable_clear(hashtable_t *hashtable)
+{
+ unsigned int i;
+
+ hashtable_do_clear(hashtable);
+
+ for(i = 0; i < num_buckets(hashtable); i++)
+ {
+ hashtable->buckets[i].first = hashtable->buckets[i].last =
+ &hashtable->list;
+ }
+
+ list_init(&hashtable->list);
+ hashtable->size = 0;
+}
+
void *hashtable_iter(hashtable_t *hashtable)
{
return hashtable_iter_next(hashtable, &hashtable->list);
diff --git a/src/hashtable.h b/src/hashtable.h
index 76666a4..d01b7e7 100644
--- a/src/hashtable.h
+++ b/src/hashtable.h
@@ -135,6 +135,15 @@ void *hashtable_get(hashtable_t *hashtable, const void *key);
int hashtable_del(hashtable_t *hashtable, const void *key);
/**
+ * hashtable_clear - Clear hashtable
+ *
+ * @hashtable: The hashtable object
+ *
+ * Removes all items from the hashtable.
+ */
+void hashtable_clear(hashtable_t *hashtable);
+
+/**
* hashtable_iter - Iterate over hashtable
*
* @hashtable: The hashtable object
diff --git a/src/jansson.h b/src/jansson.h
index b7a75b6..6751998 100644
--- a/src/jansson.h
+++ b/src/jansson.h
@@ -71,9 +71,12 @@ static inline void json_decref(json_t *json)
/* getters, setters, manipulation */
+unsigned int json_object_size(const json_t *object);
json_t *json_object_get(const json_t *object, const char *key);
int json_object_set_new(json_t *object, const char *key, json_t *value);
int json_object_del(json_t *object, const char *key);
+int json_object_clear(json_t *object);
+int json_object_update(json_t *object, json_t *other);
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);
diff --git a/src/value.c b/src/value.c
index d8e9e9a..b06cb4f 100644
--- a/src/value.c
+++ b/src/value.c
@@ -107,6 +107,17 @@ static void json_delete_object(json_object_t *object)
free(object);
}
+unsigned int json_object_size(const json_t *json)
+{
+ json_object_t *object;
+
+ if(!json_is_object(json))
+ return -1;
+
+ object = json_to_object(json);
+ return object->hashtable.size;
+}
+
json_t *json_object_get(const json_t *json, const char *key)
{
json_object_t *object;
@@ -168,6 +179,43 @@ int json_object_del(json_t *json, const char *key)
return hashtable_del(&object->hashtable, key);
}
+int json_object_clear(json_t *json)
+{
+ json_object_t *object;
+
+ if(!json_is_object(json))
+ return -1;
+
+ object = json_to_object(json);
+ hashtable_clear(&object->hashtable);
+
+ return 0;
+}
+
+int json_object_update(json_t *object, json_t *other)
+{
+ void *iter;
+
+ if(!json_is_object(object) || !json_is_object(other))
+ return -1;
+
+ iter = json_object_iter(other);
+ while(iter) {
+ const char *key;
+ json_t *value;
+
+ key = json_object_iter_key(iter);
+ value = json_object_iter_value(iter);
+
+ if(json_object_set(object, key, value))
+ return -1;
+
+ iter = json_object_iter_next(other, iter);
+ }
+
+ return 0;
+}
+
void *json_object_iter(json_t *json)
{
json_object_t *object;