Skip to content

Commit

Permalink
drop "foreach" APIs
Browse files Browse the repository at this point in the history
These require knowledge over implementation details. Instead, json-c
already contained a clean iterator class (json_object_iterator.[ch]).
They are functionally equivalent.

closes rsyslog#46
  • Loading branch information
rgerhards committed Apr 7, 2016
1 parent 0a975e7 commit 8106d79
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 20 deletions.
11 changes: 8 additions & 3 deletions tests/test1.c
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,15 @@ int main(int __attribute__((unused)) argc, char __attribute__((unused)) **argv)

/*fjson_object_object_add(my_object, "arr", my_array);*/
printf("my_object=\n");
fjson_object_object_foreach(my_object, key, val)
{
printf("\t%s: %s\n", key, fjson_object_to_json_string(val));
struct fjson_object_iterator it = fjson_object_iter_begin(my_object);
struct fjson_object_iterator itEnd = fjson_object_iter_end(my_object);
while (!fjson_object_iter_equal(&it, &itEnd)) {
printf("\t%s: %s\n",
fjson_object_iter_peek_name(&it),
fjson_object_to_json_string(fjson_object_iter_peek_value(&it)));
fjson_object_iter_next(&it);
}

printf("my_object.to_string()=%s\n", fjson_object_to_json_string(my_object));

fjson_object_put(my_string);
Expand Down
48 changes: 31 additions & 17 deletions tests/testReplaceExisting.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

int main(int __attribute__((unused)) argc, char __attribute__((unused)) **argv)
{
struct fjson_object_iterator it;
struct fjson_object_iterator itEnd;
const char *key;
MC_SET_DEBUG(1);

/*
Expand All @@ -23,25 +26,32 @@ int main(int __attribute__((unused)) argc, char __attribute__((unused)) **argv)
printf("==== delete-in-loop test starting ====\n");

int orig_count = 0;
fjson_object_object_foreach(my_object, key0, val0)
{
printf("Key at index %d is [%s]", orig_count, key0);
if (strcmp(key0, "deleteme") == 0)
{
fjson_object_object_del(my_object, key0);
itEnd = fjson_object_iter_end(my_object);
it = fjson_object_iter_begin(my_object);
while (!fjson_object_iter_equal(&it, &itEnd)) {
key = fjson_object_iter_peek_name(&it);
printf("Key at index %d is [%s]", orig_count, key);
/* need to advance now, as del invalidates "it" */
fjson_object_iter_next(&it);
if (strcmp(key, "deleteme") == 0) {
fjson_object_object_del(my_object, key);
printf(" (deleted)\n");
}
else
} else {
printf(" (kept)\n");
}
orig_count++;
}

printf("==== replace-value first loop starting ====\n");

const char *original_key = NULL;
orig_count = 0;
fjson_object_object_foreach(my_object, key, val)
{
itEnd = fjson_object_iter_end(my_object);
it = fjson_object_iter_begin(my_object);
while (!fjson_object_iter_equal(&it, &itEnd)) {
key = fjson_object_iter_peek_name(&it);
/* need to advance now, as modify invalidates "it" */
fjson_object_iter_next(&it);
printf("Key at index %d is [%s]\n", orig_count, key);
orig_count++;
if (strcmp(key, "foo2") != 0)
Expand All @@ -55,15 +65,19 @@ int main(int __attribute__((unused)) argc, char __attribute__((unused)) **argv)

int new_count = 0;
int retval = 0;
fjson_object_object_foreach(my_object, key2, val2)
{
printf("Key at index %d is [%s]\n", new_count, key2);
itEnd = fjson_object_iter_end(my_object);
it = fjson_object_iter_begin(my_object);
while (!fjson_object_iter_equal(&it, &itEnd)) {
key = fjson_object_iter_peek_name(&it);
/* need to advance now, as modify invalidates "it" */
fjson_object_iter_next(&it);
printf("Key at index %d is [%s]\n", new_count, key);
new_count++;
if (strcmp(key2, "foo2") != 0)
if (strcmp(key, "foo2") != 0)
continue;
printf("pointer for key [%s] does %smatch\n", key2,
(key2 == original_key) ? "" : "NOT ");
if (key2 != original_key)
printf("pointer for key [%s] does %smatch\n", key,
(key == original_key) ? "" : "NOT ");
if (key != original_key)
retval = 1;
}
if (new_count != orig_count)
Expand Down

0 comments on commit 8106d79

Please sign in to comment.