diff options
author | ylavic <ylavic@13f79535-47bb-0310-9956-ffa450edef68> | 2018-08-31 12:25:34 +0000 |
---|---|---|
committer | ylavic <ylavic@13f79535-47bb-0310-9956-ffa450edef68> | 2018-08-31 12:25:34 +0000 |
commit | edd1cf8b16d9fc187aa05ebe2b78b21bcce8b88e (patch) | |
tree | dca3c693f5e09203325ec63e64dff4be4a225cf2 | |
parent | 88b8fa42f150c4357efd8431ce5cdabac85f613b (diff) | |
download | libapr-edd1cf8b16d9fc187aa05ebe2b78b21bcce8b88e.tar.gz |
apr_json: object keys are strings.
Make this clear in the API by requiring a usual string in apr_json_object_set()
and creating the apr_json_string_t from it.
It's also more user friendly as otherwise apr_json_string_create() is to be
used when setting an entry.
Also, axe the 'pool' arg from apr_json_array_add() since it's not needed.
git-svn-id: http://svn.apache.org/repos/asf/apr/apr/trunk@1839755 13f79535-47bb-0310-9956-ffa450edef68
-rw-r--r-- | include/apr_json.h | 23 | ||||
-rw-r--r-- | json/apr_json.c | 26 | ||||
-rw-r--r-- | json/apr_json_decode.c | 6 |
3 files changed, 26 insertions, 29 deletions
diff --git a/include/apr_json.h b/include/apr_json.h index 06ec58ebf..a41180eae 100644 --- a/include/apr_json.h +++ b/include/apr_json.h @@ -178,7 +178,7 @@ struct apr_json_object_t { * Use apr_json_array_create() to allocate. */ struct apr_json_array_t { - /** The key value pairs in the object are in this list */ + /** The values in the array are in this list */ APR_RING_HEAD(apr_json_array_list_t, apr_json_value_t) list; /** Array of JSON objects */ apr_array_header_t *array; @@ -271,8 +271,9 @@ APR_DECLARE(apr_json_value_t *) /** * Associate a value with a key in a JSON object. * @param obj The JSON object. - * @param key Pointer to the key string, including any whitespace - * required. + * @param key Pointer to the key. + * @param klen Length of the key, or APR_JSON_VALUE_STRING if NUL + * terminated. * @param val Value to associate with the key. * @param pool Pool to use. * @return APR_SUCCESS on success, APR_EINVAL if the key is @@ -280,20 +281,19 @@ APR_DECLARE(apr_json_value_t *) * @remark If the value is NULL the key value pair is deleted. */ APR_DECLARE(apr_status_t) apr_json_object_set(apr_json_value_t *obj, - apr_json_value_t *key, apr_json_value_t *val, - apr_pool_t *pool) __attribute__((nonnull(1, 4))); + const char *key, apr_ssize_t klen, apr_json_value_t *val, + apr_pool_t *pool) __attribute__((nonnull(1, 2, 5))); /** * Look up the value associated with a key in a JSON object. * @param obj The JSON object. * @param key Pointer to the key. * @param klen Length of the key, or APR_JSON_VALUE_STRING if NUL - * terminated. + * terminated. * @return Returns NULL if the key is not present. */ -APR_DECLARE(apr_json_kv_t *) - apr_json_object_get(apr_json_value_t *obj, const char *key, - apr_ssize_t klen) +APR_DECLARE(apr_json_kv_t *) apr_json_object_get(apr_json_value_t *obj, + const char *key, apr_ssize_t klen) __attribute__((nonnull(1, 2))); /** @@ -325,13 +325,12 @@ APR_DECLARE(apr_json_kv_t *) apr_json_object_next(apr_json_value_t *obj, * Add the value to the end of this array. * @param arr The JSON array. * @param val Value to add to the array. - * @param pool Pool to use. * @return APR_SUCCESS on success, APR_EINVAL if the array value is not * an APR_JSON_ARRAY. */ APR_DECLARE(apr_status_t) apr_json_array_add(apr_json_value_t *arr, - apr_json_value_t *val, apr_pool_t *pool) - __attribute__((nonnull(1, 2, 3))); + apr_json_value_t *val) + __attribute__((nonnull(1, 2))); /** * Look up the value associated with a key in a JSON object. diff --git a/json/apr_json.c b/json/apr_json.c index b8b55fc9a..d3bc8329d 100644 --- a/json/apr_json.c +++ b/json/apr_json.c @@ -129,25 +129,23 @@ apr_json_value_t *apr_json_null_create(apr_pool_t *pool) return json; } -apr_status_t apr_json_object_set(apr_json_value_t *object, apr_json_value_t *key, - apr_json_value_t *val, apr_pool_t *pool) +apr_status_t apr_json_object_set(apr_json_value_t *object, const char *key, + apr_ssize_t klen, apr_json_value_t *val, apr_pool_t *pool) { apr_json_kv_t *kv; apr_hash_t *hash; - if (object->type != APR_JSON_OBJECT || !key - || key->type != APR_JSON_STRING) { + if (object->type != APR_JSON_OBJECT) { return APR_EINVAL; } hash = object->value.object->hash; - kv = apr_hash_get(hash, key->value.string.p, key->value.string.len); + kv = apr_hash_get(hash, key, klen); if (!val) { if (kv) { - apr_hash_set(hash, key->value.string.p, key->value.string.len, - NULL); + apr_hash_set(hash, key, klen, NULL); APR_RING_REMOVE((kv), link); } return APR_SUCCESS; @@ -157,11 +155,10 @@ apr_status_t apr_json_object_set(apr_json_value_t *object, apr_json_value_t *key kv = apr_palloc(pool, sizeof(apr_json_kv_t)); APR_RING_ELEM_INIT(kv, link); APR_JSON_OBJECT_INSERT_TAIL(object->value.object, kv); - apr_hash_set(hash, key->value.string.p, key->value.string.len, - kv); + kv->k = apr_json_string_create(pool, key, klen); + apr_hash_set(hash, kv->k->value.string.p, kv->k->value.string.len, kv); } - kv->k = key; kv->v = val; return APR_SUCCESS; @@ -212,8 +209,7 @@ apr_json_kv_t *apr_json_object_next(apr_json_value_t *obj, apr_json_kv_t *kv) } } -apr_status_t apr_json_array_add(apr_json_value_t *arr, - apr_json_value_t *val, apr_pool_t *pool) +apr_status_t apr_json_array_add(apr_json_value_t *arr, apr_json_value_t *val) { apr_array_header_t *array; @@ -316,7 +312,8 @@ apr_json_value_t *apr_json_overlay(apr_pool_t *p, if (!apr_hash_get(overlay->value.object->hash, kv->k->value.string.p, kv->k->value.string.len)) { - apr_json_object_set(res, kv->k, kv->v, p); + apr_json_object_set(res, kv->k->value.string.p, + kv->k->value.string.len, kv->v, p); } else if (APR_JSON_FLAGS_STRICT & flags) { @@ -329,7 +326,8 @@ apr_json_value_t *apr_json_overlay(apr_pool_t *p, kv != APR_RING_SENTINEL(&(overlay->value.object)->list, apr_json_kv_t, link); kv = APR_RING_NEXT((kv), link)) { - apr_json_object_set(res, kv->k, kv->v, p); + apr_json_object_set(res, kv->k->value.string.p, + kv->k->value.string.len, kv->v, p); } diff --git a/json/apr_json_decode.c b/json/apr_json_decode.c index e8d44013b..d4b8b315d 100644 --- a/json/apr_json_decode.c +++ b/json/apr_json_decode.c @@ -390,8 +390,7 @@ static apr_status_t apr_json_decode_array(apr_json_scanner_t * self, return status; } - if (APR_SUCCESS - != (status = apr_json_array_add(array, element, self->pool))) { + if (APR_SUCCESS != (status = apr_json_array_add(array, element))) { return status; } @@ -501,7 +500,8 @@ static apr_status_t apr_json_decode_object(apr_json_scanner_t * self, if ((status = apr_json_decode_value(self, &value))) goto out; - apr_json_object_set(json, key, value, self->pool); + apr_json_object_set(json, key->value.string.p, key->value.string.len, + value, self->pool); if (self->p == self->e) { status = APR_EOF; |