Add support for iterating over objects
This commit is contained in:
parent
17a69c2d66
commit
a2adf6ec98
@ -64,6 +64,10 @@ static inline void json_decref(json_t *json)
|
||||
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_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);
|
||||
|
||||
unsigned int json_array_size(const json_t *array);
|
||||
json_t *json_array_get(const json_t *array, unsigned int index);
|
||||
|
44
src/value.c
44
src/value.c
@ -104,6 +104,17 @@ json_t *json_object_get(const json_t *json, const char *key)
|
||||
return hashtable_get(object->hashtable, key);
|
||||
}
|
||||
|
||||
int json_object_set(json_t *json, const char *key, json_t *value)
|
||||
{
|
||||
json_object_t *object;
|
||||
|
||||
if(!json_is_object(json))
|
||||
return -1;
|
||||
|
||||
object = json_to_object(json);
|
||||
return hashtable_set(object->hashtable, strdup(key), json_incref(value));
|
||||
}
|
||||
|
||||
int json_object_del(json_t *json, const char *key)
|
||||
{
|
||||
json_object_t *object;
|
||||
@ -115,15 +126,42 @@ int json_object_del(json_t *json, const char *key)
|
||||
return hashtable_del(object->hashtable, key);
|
||||
}
|
||||
|
||||
int json_object_set(json_t *json, const char *key, json_t *value)
|
||||
void *json_object_iter(json_t *json)
|
||||
{
|
||||
json_object_t *object;
|
||||
|
||||
if(!json_is_object(json))
|
||||
return -1;
|
||||
return NULL;
|
||||
|
||||
object = json_to_object(json);
|
||||
return hashtable_set(object->hashtable, strdup(key), json_incref(value));
|
||||
return hashtable_iter(object->hashtable);
|
||||
}
|
||||
|
||||
void *json_object_iter_next(json_t *json, void *iter)
|
||||
{
|
||||
json_object_t *object;
|
||||
|
||||
if(!json_is_object(json) || iter == NULL)
|
||||
return NULL;
|
||||
|
||||
object = json_to_object(json);
|
||||
return hashtable_iter_next(object->hashtable, iter);
|
||||
}
|
||||
|
||||
const char *json_object_iter_key(void *iter)
|
||||
{
|
||||
if(!iter)
|
||||
return NULL;
|
||||
|
||||
return (const char *)hashtable_iter_key(iter);
|
||||
}
|
||||
|
||||
json_t *json_object_iter_value(void *iter)
|
||||
{
|
||||
if(!iter)
|
||||
return NULL;
|
||||
|
||||
return (json_t *)hashtable_iter_value(iter);
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user