Skip to content

Commit

Permalink
fixed root array issues
Browse files Browse the repository at this point in the history
  • Loading branch information
ChiragH25 committed Apr 17, 2024
1 parent 950d928 commit 14950bd
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 14 deletions.
16 changes: 7 additions & 9 deletions src/value.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,13 @@ json_t *json_object_get_path(json_t* object, size_t depth, ...){
va_start(path, depth);
for (size_t 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){
Expand All @@ -116,15 +123,6 @@ json_t *json_object_get_path(json_t* object, size_t depth, ...){
if(object->type != object_t){
return NULL;
}
if(object_t == JSON_ARRAY){
i++;
if(i<depth){
object = json_array_get(object, va_arg(path, size_t));
if(!object){
return NULL;
}
}
}
}

return object;
Expand Down
26 changes: 21 additions & 5 deletions test/suites/api/test_get_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,29 @@ static void run_tests() {
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");
if(!value) fail("did not ignore garbage arguments");

value = json_object_get_path(json, 2, JSON_OBJECT, "base", JSON_OBJECT, "nest 1", JSON_OBJECT, "nest 2", JSON_OBJECT, "nest 3");
value = json_object_get_path(json, 4, JSON_OBJECT, "base", JSON_OBJECT, "nest 1", JSON_OBJECT, "nest 2", JSON_OBJECT, "nest 3");
if(!value) fail("did not find nested object");

/*
"nest 1",
"nest 2",
"nest 3"*/
json_decref(json);

json = json_pack(
"[sss]",
"base",
"array",
"string 1"
);

value = json_object_get_path(json, 1, (size_t) 0);
if(!value) fail("did not find object in list");
if(strcmp(json_string_value(value), "base")) fail("did not get appropriate string value");

value = json_object_get_path(json, 1, (size_t) 1);
if(!value) fail("did not find object in list");
if(strcmp(json_string_value(value), "array")) fail("did not get appropriate string value");

value = json_object_get_path(json, 1, (size_t) 2);
if(!value) fail("did not find object in list");
if(strcmp(json_string_value(value), "string 1")) fail("did not get appropriate string value");

json_decref(json);
}

0 comments on commit 14950bd

Please sign in to comment.