diff --git a/doc/apiref.rst b/doc/apiref.rst index 4bfb6879..9b075d32 100644 --- a/doc/apiref.rst +++ b/doc/apiref.rst @@ -648,6 +648,13 @@ allowed in object keys. Get a value corresponding to *key* from *object*. Returns *NULL* if *key* is not found and on error. +.. function:: json_t *json_object_get_path(const json_t* object, size_t depth, ...) + + .. refcounting:: borrow + + Get a value corresponding to the path from *object*. Returns *NULL* if + the object is not found and on error. + .. function:: json_t *json_object_getn(const json_t *object, const char *key, size_t key_len) .. refcounting:: borrow diff --git a/src/jansson.def b/src/jansson.def index 5c76c2f6..edf2dea9 100644 --- a/src/jansson.def +++ b/src/jansson.def @@ -35,6 +35,7 @@ EXPORTS json_object_size json_object_get json_object_getn + json_object_get_path json_object_set_new json_object_setn_new json_object_set_new_nocheck diff --git a/src/jansson.h b/src/jansson.h index a2ac2eea..bdde4329 100644 --- a/src/jansson.h +++ b/src/jansson.h @@ -188,7 +188,7 @@ void json_object_seed(size_t seed); size_t json_object_size(const json_t *object); json_t *json_object_get(const json_t *object, const char *key) JANSSON_ATTRS((warn_unused_result)); -json_t *json_object_get_path(const json_t* root, size_t depth, ...); +json_t *json_object_get_path(const json_t* object, size_t depth, ...); json_t *json_object_getn(const json_t *object, const char *key, size_t key_len) JANSSON_ATTRS((warn_unused_result)); int json_object_set_new(json_t *object, const char *key, json_t *value); diff --git a/src/value.c b/src/value.c index bd0cb4d3..0acaa0a0 100644 --- a/src/value.c +++ b/src/value.c @@ -105,9 +105,11 @@ json_t *json_object_get(const json_t *json, const char *key) { json_t *json_object_get_path(json_t* object, size_t depth, ...){ va_list path; + size_t i = 0; + va_start(path, depth); - for (size_t i = 0; i < depth; i++) - { + + for (i = 0; i < depth; i++){ if(object->type == JSON_ARRAY){ object = json_array_get(object, va_arg(path, size_t)); if(!object){ @@ -115,7 +117,7 @@ json_t *json_object_get_path(json_t* object, size_t depth, ...){ } continue; } - json_type object_t = va_arg(path, json_type); + json_type object = va_arg(path, json_type); object = json_object_get(object, va_arg(path, const char*)); if(!object){ return NULL; @@ -125,6 +127,8 @@ json_t *json_object_get_path(json_t* object, size_t depth, ...){ } } + va_end(path); + return object; }