diff options
author | Rainer Gerhards <rgerhards@adiscon.com> | 2015-09-23 09:08:49 +0200 |
---|---|---|
committer | Rainer Gerhards <rgerhards@adiscon.com> | 2015-09-23 09:43:00 +0200 |
commit | 2d549662be832da838aa063da2efa78ee3b99668 (patch) | |
tree | 9d3f7a78723a512c21844060ac62d80a6ea7c114 /json_object.c | |
parent | d8e44dc6855c5065050b1bcb26e2708a63934797 (diff) | |
download | json-c-2d549662be832da838aa063da2efa78ee3b99668.tar.gz |
add json_object_object_add_ex() API
This provides more control over some detail aspects, many
of which are performance related.
Diffstat (limited to 'json_object.c')
-rw-r--r-- | json_object.c | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/json_object.c b/json_object.c index e06c7ac..55f7469 100644 --- a/json_object.c +++ b/json_object.c @@ -355,7 +355,8 @@ static int json_object_object_to_json_string(struct json_object* jso, static void json_object_lh_entry_free(struct lh_entry *ent) { - free(ent->k); + if (!ent->k_is_constant) + free(ent->k); json_object_put((struct json_object*)ent->v); } @@ -396,6 +397,31 @@ struct lh_table* json_object_get_object(struct json_object *jso) } } +void json_object_object_add_ex(struct json_object* jso, + const char *const key, + struct json_object *const val, + const unsigned opts) +{ + // 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; + const unsigned long hash = lh_get_hash(jso->o.c_object, (void*)key); + existing_entry = (opts & JSON_C_OBJECT_ADD_KEY_IS_NEW) ? NULL : + lh_table_lookup_entry_w_hash(jso->o.c_object, (void*)key, hash); + if (!existing_entry) + { + void *const k = (opts & JSON_C_OBJECT_KEY_IS_CONSTANT) ? + (void*)key : strdup(key); + lh_table_insert_w_hash(jso->o.c_object, k, val, hash, opts); + return; + } + existing_value = (void *)existing_entry->v; + if (existing_value) + json_object_put(existing_value); + existing_entry->v = val; +} + void json_object_object_add(struct json_object* jso, const char *key, struct json_object *val) { @@ -407,7 +433,7 @@ void json_object_object_add(struct json_object* jso, const char *key, existing_entry = lh_table_lookup_entry_w_hash(jso->o.c_object, (void*)key, hash); if (!existing_entry) { - lh_table_insert_w_hash(jso->o.c_object, strdup(key), val, hash); + lh_table_insert_w_hash(jso->o.c_object, strdup(key), val, hash, 0); return; } existing_value = (void *)existing_entry->v; |