Rewrite json_object_object_add to replace just the value if the key already exists so keys remain valid.

This is particularly useful when replacing values in a loop, since it allows
 the key used by json_object_object_foreach to continue to be used.
This commit is contained in:
Eric Haszlakiewicz
2012-07-24 23:27:41 -05:00
parent 381f77c5bc
commit 6988f53fcb
5 changed files with 95 additions and 3 deletions

View File

@@ -306,8 +306,20 @@ struct lh_table* json_object_get_object(struct json_object *jso)
void json_object_object_add(struct json_object* jso, const char *key,
struct json_object *val)
{
lh_table_delete(jso->o.c_object, key);
lh_table_insert(jso->o.c_object, strdup(key), val);
// We lookup the entry and replace the value, rather than just deleting
// and re-adding it, so the existing key remains valid.
json_object *existing_value = NULL;
struct lh_entry *existing_entry;
existing_entry = lh_table_lookup_entry(jso->o.c_object, (void*)key);
if (!existing_entry)
{
lh_table_insert(jso->o.c_object, strdup(key), val);
return;
}
existing_value = (void *)existing_entry->v;
if (existing_value)
json_object_put(existing_value);
existing_entry->v = val;
}
struct json_object* json_object_object_get(struct json_object* jso, const char *key)