diff options
author | Greg Hudson <ghudson@mit.edu> | 2013-07-16 18:09:55 -0400 |
---|---|---|
committer | Greg Hudson <ghudson@mit.edu> | 2013-07-16 18:09:55 -0400 |
commit | ea29df4d93b1b7b384c15f39a4ee20be3e0991ac (patch) | |
tree | 25837e2647b89b5f12f5712f1daa113400db9f58 /src/util | |
parent | 57d0b4b300e43722ae9f080fbf132edeb3834323 (diff) | |
download | krb5-ea29df4d93b1b7b384c15f39a4ee20be3e0991ac.tar.gz krb5-ea29df4d93b1b7b384c15f39a4ee20be3e0991ac.tar.xz krb5-ea29df4d93b1b7b384c15f39a4ee20be3e0991ac.zip |
Clarify and improve k5_json_object_set
Document that k5_json_object_set can be used to overwrite an existing
key, and make it possible to remove a key by setting it to NULL.
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/support/json.c | 19 | ||||
-rw-r--r-- | src/util/support/t_json.c | 9 |
2 files changed, 25 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); diff --git a/src/util/support/t_json.c b/src/util/support/t_json.c index 040a85a91..1f229247b 100644 --- a/src/util/support/t_json.c +++ b/src/util/support/t_json.c @@ -165,6 +165,15 @@ test_object(void) if (strcmp(k5_json_string_utf8(s), "hejsan") != 0) err("Retrieving key2 from object failed"); + check(k5_json_object_get(object, "key3") == NULL, + "object nonexistent key"); + + k5_json_object_set(object, "key1", NULL); + check(k5_json_object_get(object, "key1") == NULL, + "object removed key"); + check(k5_json_object_get(object, "key2") != NULL, + "object remaining key"); + k5_json_release(v1); k5_json_release(v2); k5_json_release(object); |