diff options
-rw-r--r-- | include/apr_json.h | 25 | ||||
-rw-r--r-- | json/apr_json.c | 36 |
2 files changed, 61 insertions, 0 deletions
diff --git a/include/apr_json.h b/include/apr_json.h index ca0dfa020..3b2575399 100644 --- a/include/apr_json.h +++ b/include/apr_json.h @@ -278,6 +278,31 @@ APR_DECLARE(apr_json_kv_t *) __attribute__((nonnull(1, 2))); /** + * Get the first value associated with an object. + * + * If the value is an object, this function returns the first key value pair. + * @param obj The JSON object. + * @return Returns the first value, or NULL if not an object, or the object is + * empty. + */ +APR_DECLARE(apr_json_kv_t *) apr_json_object_first(apr_json_value_t *obj) + __attribute__((nonnull(1)));; + +/** + * Get the next value associated with an object. + * + * This function returns the next key value pair, or NULL if no more pairs + * are present. + * @param obj The JSON object. + * @param kv The previous JSON key value pair. + * @return Returns the next key value pair, or NULL if not an object, or + * the object is empty. + */ +APR_DECLARE(apr_json_kv_t *) apr_json_object_next(apr_json_value_t *obj, + apr_json_kv_t *kv) + __attribute__((nonnull(1, 2)));; + +/** * Decode utf8-encoded JSON string into apr_json_value_t. * @param retval the result * @param injson utf8-encoded JSON string. diff --git a/json/apr_json.c b/json/apr_json.c index 5fb7b5f31..8a4c91d60 100644 --- a/json/apr_json.c +++ b/json/apr_json.c @@ -169,6 +169,42 @@ apr_json_kv_t *apr_json_object_get(apr_json_value_t *object, const char *key, return apr_hash_get(object->value.object->hash, key, klen); } +apr_json_kv_t *apr_json_object_first(apr_json_value_t *obj) +{ + apr_json_kv_t *kv; + + if (obj->type != APR_JSON_OBJECT) { + return NULL; + } + + kv = APR_RING_FIRST(&(obj->value.object)->list); + + if (kv != APR_RING_SENTINEL(&(obj->value.object)->list, apr_json_kv_t, link)) { + return kv; + } + else { + return NULL; + } +} + +apr_json_kv_t *apr_json_object_next(apr_json_value_t *obj, apr_json_kv_t *kv) +{ + apr_json_kv_t *next; + + if (obj->type != APR_JSON_OBJECT) { + return NULL; + } + + next = APR_RING_NEXT((kv), link); + + if (next != APR_RING_SENTINEL(&(obj->value.object)->list, apr_json_kv_t, link)) { + return next; + } + else { + return NULL; + } +} + apr_json_value_t *apr_json_overlay(apr_pool_t *p, apr_json_value_t *overlay, apr_json_value_t *base, int flags) |