Skip to content

Commit

Permalink
removed type checking to attempt to resolve Visual Studio Errors
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiragH25 committed Apr 17, 2024
1 parent 14950bd commit e5c9735
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 37 deletions.
7 changes: 7 additions & 0 deletions doc/apiref.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions src/jansson.def
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/jansson.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 6 additions & 6 deletions src/value.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,26 +105,26 @@ 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){
return NULL;
}
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;
}

Expand Down
59 changes: 29 additions & 30 deletions test/suites/api/test_get_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]",
Expand Down

0 comments on commit e5c9735

Please sign in to comment.