/**
* Iterate through all keys and values of an object.
*
- * Adding or deleting keys to the object while iterating is NOT allowed.
+ * Adding keys to the object while iterating is NOT allowed.
*
- * Replacing an existing key with a new value IS allowed.
+ * Deleting an existing key, or replacing an existing key with a
+ * new value IS allowed.
*
* @param obj the json_object instance
* @param key the local name for the char* key variable defined in the body
# define json_object_object_foreach(obj,key,val) \
char *key; \
struct json_object *val; \
- for(struct lh_entry *entry = json_object_get_object(obj)->head; \
+ for(struct lh_entry *entry = json_object_get_object(obj)->head, *entry_next = NULL; \
({ if(entry) { \
key = (char*)entry->k; \
val = (struct json_object*)entry->v; \
+ entry_next = entry->next; \
} ; entry; }); \
- entry = entry->next )
+ entry = entry_next )
#else /* ANSI C or MSC */
char *key;\
struct json_object *val; \
struct lh_entry *entry; \
+ struct lh_entry *entry_next = NULL; \
for(entry = json_object_get_object(obj)->head; \
(entry ? ( \
key = (char*)entry->k, \
val = (struct json_object*)entry->v, \
+ entry_next = entry->next, \
entry) : 0); \
- entry = entry->next)
+ entry = entry_next)
#endif /* defined(__GNUC__) && !defined(__STRICT_ANSI__) */
json_object *my_object = json_object_new_object();
json_object_object_add(my_object, "foo1", json_object_new_string("bar1"));
json_object_object_add(my_object, "foo2", json_object_new_string("bar2"));
+ json_object_object_add(my_object, "deleteme", json_object_new_string("bar2"));
json_object_object_add(my_object, "foo3", json_object_new_string("bar3"));
- const char *original_key = NULL;
+
+ printf("==== delete-in-loop test starting ====\n");
+
int orig_count = 0;
+ json_object_object_foreach(my_object, key0, val0)
+ {
+ printf("Key at index %d is [%s]", orig_count, key0);
+ if (strcmp(key0, "deleteme") == 0)
+ {
+ json_object_object_del(my_object, key0);
+ printf(" (deleted)\n");
+ }
+ else
+ printf(" (kept)\n");
+ orig_count++;
+ }
+
+ printf("==== replace-value first loop starting ====\n");
+
+ const char *original_key = NULL;
+ orig_count = 0;
json_object_object_foreach(my_object, key, val)
{
printf("Key at index %d is [%s]\n", orig_count, key);
+==== delete-in-loop test starting ====
+Key at index 0 is [foo1] (kept)
+Key at index 1 is [foo2] (kept)
+Key at index 2 is [deleteme] (deleted)
+Key at index 3 is [foo3] (kept)
+==== replace-value first loop starting ====
Key at index 0 is [foo1]
Key at index 1 is [foo2]
replacing value for key [foo2]