From e5c9735d937d7795bb88f693a2d5670d2d72da77 Mon Sep 17 00:00:00 2001 From: ChiragH25 <83455681+ChiragH25@users.noreply.github.com> Date: Wed, 17 Apr 2024 21:03:15 +0800 Subject: [PATCH] removed type checking to attempt to resolve Visual Studio Errors --- doc/apiref.rst | 7 ++++ src/jansson.def | 1 + src/jansson.h | 2 +- src/value.c | 12 +++---- test/suites/api/test_get_path.c | 59 ++++++++++++++++----------------- 5 files changed, 44 insertions(+), 37 deletions(-) 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..4445b514 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,16 +117,14 @@ json_t *json_object_get_path(json_t* object, size_t depth, ...){ } continue; } - json_type object_t = va_arg(path, json_type); object = json_object_get(object, va_arg(path, const char*)); if(!object){ return NULL; } - if(object->type != object_t){ - return NULL; - } } + va_end(path); + return object; } diff --git a/test/suites/api/test_get_path.c b/test/suites/api/test_get_path.c index b5515d48..90f67006 100644 --- a/test/suites/api/test_get_path.c +++ b/test/suites/api/test_get_path.c @@ -30,106 +30,105 @@ static void run_tests() { "nest 3" ); - // printf("%s\n", json_dumps(json,0)); - - json_t* value = json_object_get_path(json, 1, JSON_OBJECT, "A"); + json_t* value = json_object_get_path(json, 1, "A"); if(value) fail("should not have found object"); - value = json_object_get_path(json, 1, JSON_ARRAY, "A"); + value = json_object_get_path(json, 1, "A"); if(value) fail("should not have found array"); - value = json_object_get_path(json, 1, JSON_STRING, "A"); + value = json_object_get_path(json, 1, "A"); if(value) fail("should not have found string"); - value = json_object_get_path(json, 1, JSON_INTEGER, "A"); + value = json_object_get_path(json, 1, "A"); if(value) fail("should not have found integer"); - value = json_object_get_path(json, 1, JSON_TRUE, "A"); + value = json_object_get_path(json, 1, "A"); if(value) fail("should not have found true"); - value = json_object_get_path(json, 1, JSON_FALSE, "A"); + value = json_object_get_path(json, 1, "A"); if(value) fail("should not have found false"); - value = json_object_get_path(json, 1, JSON_NULL, "A"); + value = json_object_get_path(json, 1, "A"); if(value) fail("should not have found null"); - value = json_object_get_path(json, 1, JSON_OBJECT, "base"); + value = json_object_get_path(json, 1, "base"); if(!value) fail("did not find object"); - value = json_object_get_path(json, 2, JSON_OBJECT, "base", JSON_ARRAY, "array"); + value = json_object_get_path(json, 2, "base", "array"); if(!value) fail("did not find array"); - value = json_object_get_path(json, 3, JSON_OBJECT, "base", JSON_ARRAY, "array", (size_t) 0); + value = json_object_get_path(json, 3, "base", "array", (size_t) 0); if(!value) fail("did not find array element"); if(value->type != JSON_OBJECT) fail("did not find object in array"); - value = json_object_get_path(json, 3, JSON_OBJECT, "base", JSON_ARRAY, "array", (size_t) 1); + value = json_object_get_path(json, 3, "base", "array", (size_t) 1); if(!value) fail("did not find array element"); if(value->type != JSON_ARRAY) fail("did not find array in array"); - value = json_object_get_path(json, 3, JSON_OBJECT, "base", JSON_ARRAY, "array", (size_t) 2); + value = json_object_get_path(json, 3, "base", "array", (size_t) 2); if(!value) fail("did not find array element"); if(value->type != JSON_STRING) fail("did not find string in array"); - value = json_object_get_path(json, 3, JSON_OBJECT, "base", JSON_ARRAY, "array", (size_t) 3); + value = json_object_get_path(json, 3, "base", "array", (size_t) 3); if(!value) fail("did not find array element"); if(value->type != JSON_INTEGER) fail("did not find integer in array"); - value = json_object_get_path(json, 3, JSON_OBJECT, "base", JSON_ARRAY, "array", (size_t) 4); + value = json_object_get_path(json, 3, "base", "array", (size_t) 4); if(!value) fail("did not find array element"); if(value->type != JSON_REAL) fail("did not find real in array"); - value = json_object_get_path(json, 3, JSON_OBJECT, "base", JSON_ARRAY, "array", (size_t) 5); + value = json_object_get_path(json, 3, "base", "array", (size_t) 5); if(!value) fail("did not find array element"); if(value->type != JSON_TRUE) fail("did not find real in array"); - value = json_object_get_path(json, 3, JSON_OBJECT, "base", JSON_ARRAY, "array", (size_t) 6); + value = json_object_get_path(json, 3, "base", "array", (size_t) 6); if(!value) fail("did not find array element"); if(value->type != JSON_FALSE) fail("did not find real in array"); - value = json_object_get_path(json, 3, JSON_OBJECT, "base", JSON_ARRAY, "array", (size_t) 7); + value = json_object_get_path(json, 3, "base", "array", (size_t) 7); if(!value) fail("did not find array element"); if(value->type != JSON_NULL) fail("did not find real in array"); - value = json_object_get_path(json, 3, JSON_OBJECT, "base", JSON_ARRAY, "array", (size_t) 8); + value = json_object_get_path(json, 3, "base", "array", (size_t) 8); if(value) fail("should not have found array element"); - value = json_object_get_path(json, 2, JSON_OBJECT, "base", JSON_STRING, "string2"); + value = json_object_get_path(json, 2, "base", "string2"); if(!value) fail("did not find string in object"); if(value->type != JSON_STRING) fail("did not find string in object"); if(strcmp(json_string_value(value), "string3")) fail("did not get appropriate string value"); - value = json_object_get_path(json, 2, JSON_OBJECT, "base", JSON_INTEGER, "integer"); + value = json_object_get_path(json, 2, "base", "integer"); if(!value) fail("did not find integer in object"); if(value->type != JSON_INTEGER) fail("did not find integer in object"); if(json_integer_value(value) != 3) fail("did not get appropriate integer value"); - value = json_object_get_path(json, 2, JSON_OBJECT, "base", JSON_REAL, "real"); + value = json_object_get_path(json, 2, "base", "real"); if(!value) fail("did not find real in object"); if(value->type != JSON_REAL) fail("did not find real in object"); if(json_real_value(value)!= 4.4) fail("did not get appropriate real value"); - value = json_object_get_path(json, 2, JSON_OBJECT, "base", JSON_TRUE, "true"); + value = json_object_get_path(json, 2, "base", "true"); if(!value) fail("did not find true in object"); if(value->type != JSON_TRUE) fail("did not find true in object"); if(!json_is_true(value)) fail("did not get appropriate true value"); - value = json_object_get_path(json, 2, JSON_OBJECT, "base", JSON_FALSE, "false"); + value = json_object_get_path(json, 2, "base", "false"); if(!value) fail("did not find false in object"); if(value->type != JSON_FALSE) fail("did not find false in object"); if(!json_is_false(value)) fail("did not get appropriate false value"); - value = json_object_get_path(json, 2, JSON_OBJECT, "base", JSON_NULL, "null"); + value = json_object_get_path(json, 2, "base", "null"); if(!value) fail("did not find null in object"); if(value->type != JSON_NULL) fail("did not find null in object"); - value = json_object_get_path(json, 2, JSON_OBJECT, "base", JSON_NULL, "null", JSON_OBJECT, "nest 1", JSON_OBJECT, "nest 2", JSON_OBJECT, "nest 3"); + value = json_object_get_path(json, 2, "base", "null", "nest 1", "nest 2", "nest 3"); if(!value) fail("did not ignore garbage arguments"); - value = json_object_get_path(json, 4, JSON_OBJECT, "base", JSON_OBJECT, "nest 1", JSON_OBJECT, "nest 2", JSON_OBJECT, "nest 3"); + value = json_object_get_path(json, 3, "base", "nest 1", "nest 2"); + if(!value) fail("did not find nested object"); + + value = json_object_get_path(json, 4, "base", "nest 1", "nest 2", "nest 3"); if(!value) fail("did not find nested object"); - - json_decref(json); json = json_pack( "[sss]",