diff options
Diffstat (limited to 'src/util/support/json.c')
-rw-r--r-- | src/util/support/json.c | 19 |
1 files changed, 16 insertions, 3 deletions
diff --git a/src/util/support/json.c b/src/util/support/json.c index b2dd1333e..b99fabd49 100644 --- a/src/util/support/json.c +++ b/src/util/support/json.c @@ -429,15 +429,28 @@ int k5_json_object_set(k5_json_object obj, const char *key, k5_json_value val) { struct entry *ent, *ptr; - size_t new_alloc; + size_t new_alloc, i; ent = object_search(obj, key); - if (ent) { + if (ent != NULL) { k5_json_release(ent->value); - ent->value = k5_json_retain(val); + if (val == NULL) { + /* Remove this key. */ + free(ent->key); + for (i = ent - obj->entries; i < obj->len - 1; i++) + obj->entries[i] = obj->entries[i + 1]; + obj->len--; + } else { + /* Overwrite this key's value with the new one. */ + ent->value = k5_json_retain(val); + } return 0; } + /* If didn't find a key the caller asked to remove, do nothing. */ + if (val == NULL) + return 0; + if (obj->len >= obj->allocated) { /* Increase the number of slots by 50% (16 slots minimum). */ new_alloc = obj->len + 1 + (obj->len >> 1); |