Implement json_object_foreach()

Also change many places to use it internally to replace hand-crafted
iteration.

Closes #45, #46.
This commit is contained in:
Petri Lehtinen
2012-01-24 20:37:08 +02:00
parent a2381948bb
commit a307974731
6 changed files with 92 additions and 46 deletions

View File

@@ -44,6 +44,7 @@ json_object_iter_next
json_object_iter_key
json_object_iter_value
json_object_iter_set_new
json_object_key_to_iter
json_dumps
json_dumpf
json_dump_file

View File

@@ -437,6 +437,24 @@ static void test_preserve_order()
json_decref(object);
}
static void test_foreach()
{
const char *key;
json_t *object1, *object2, *value;
object1 = json_pack("{sisisi}", "foo", 1, "bar", 2, "baz", 3);
object2 = json_object();
json_object_foreach(object1, key, value)
json_object_set(object2, key, value);
if(!json_equal(object1, object2))
fail("json_object_foreach failed to iterate all key-value pairs");
json_decref(object1);
json_decref(object2);
}
static void run_tests()
{
test_misc();
@@ -446,4 +464,5 @@ static void run_tests()
test_set_nocheck();
test_iterators();
test_preserve_order();
test_foreach();
}