Merge pull request #118 from dtgriscom/master

Added json_array_foreach(), parallelling json_object_foreach()
This commit is contained in:
Petri Lehtinen 2013-05-12 22:16:09 -07:00
commit ffb7ef4b75
4 changed files with 56 additions and 2 deletions

View File

@ -505,6 +505,35 @@ A JSON array is an ordered collection of other JSON values.
Appends all elements in *other_array* to the end of *array*. Appends all elements in *other_array* to the end of *array*.
Returns 0 on success and -1 on error. Returns 0 on success and -1 on error.
The following macro can be used to iterate through all elements
in an array.
.. function:: json_array_foreach(array, index, value)
Iterate over every element of ``array``, running the block
of code that follows each time with the proper values set to
variables ``index`` and ``value``, of types :type:`int` and
:type:`json_t *` respectively. Example::
/* array is a JSON array */
size_t index;
json_t *value;
json_array_foreach(array, index, value) {
/* block of code that uses index and value */
}
The items are returned in increasing index order.
This macro expands to an ordinary ``for`` statement upon
preprocessing, so its performance is equivalent to that of
hand-written code using the array access functions.
The main advantage of this macro is that it abstracts
away the complexity, and makes for shorter, more
concise code.
.. versionadded:: 2.5
Object Object
====== ======

View File

@ -148,6 +148,11 @@ int json_object_iter_set_new(json_t *object, void *iter, json_t *value);
key && (value = json_object_iter_value(json_object_key_to_iter(key))); \ key && (value = json_object_iter_value(json_object_key_to_iter(key))); \
key = json_object_iter_key(json_object_iter_next(object, json_object_key_to_iter(key)))) key = json_object_iter_key(json_object_iter_next(object, json_object_key_to_iter(key))))
#define json_array_foreach(array, index, value) \
for(index = 0; \
index < json_array_size(array) && (value = json_array_get(array, index)); \
index++)
static JSON_INLINE static JSON_INLINE
int json_object_set(json_t *object, const char *key, json_t *value) int json_object_set(json_t *object, const char *key, json_t *value)
{ {

View File

@ -400,6 +400,25 @@ static void test_circular()
json_decref(array1); json_decref(array1);
} }
static void test_array_foreach()
{
size_t index;
json_t *array1, *array2, *value;
array1 = json_pack("[sisisi]", "foo", 1, "bar", 2, "baz", 3);
array2 = json_array();
json_array_foreach(array1, index, value) {
json_array_append(array2, value);
}
if(!json_equal(array1, array2))
fail("json_array_foreach failed to iterate all elements");
json_decref(array1);
json_decref(array2);
}
static void run_tests() static void run_tests()
{ {
@ -409,4 +428,5 @@ static void run_tests()
test_clear(); test_clear();
test_extend(); test_extend();
test_circular(); test_circular();
test_array_foreach();
} }

View File

@ -479,7 +479,7 @@ static void test_preserve_order()
json_decref(object); json_decref(object);
} }
static void test_foreach() static void test_object_foreach()
{ {
const char *key; const char *key;
json_t *object1, *object2, *value; json_t *object1, *object2, *value;
@ -507,5 +507,5 @@ static void run_tests()
test_set_nocheck(); test_set_nocheck();
test_iterators(); test_iterators();
test_preserve_order(); test_preserve_order();
test_foreach(); test_object_foreach();
} }