Skip to content

Commit

Permalink
datajson: handle case with multiple format
Browse files Browse the repository at this point in the history
In some cases, the JSON array will contain different values type
and the wanted key may be absent from some items.

This patch fixes this use case by ignoring the data and just printing
a message if ever the no item have been found.
  • Loading branch information
regit committed Dec 23, 2024
1 parent 3c3fff0 commit add5abd
Showing 1 changed file with 11 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/datajson.c
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ static int DatajsonLoadString(Dataset *set, char *json_key, char *array_key)
fclose(fp);
} else {
json_t *json;
bool found = false;

if (ParseJsonFile(set->load, &json, array_key) == -1)
return -1;
Expand All @@ -339,10 +340,13 @@ static int DatajsonLoadString(Dataset *set, char *json_key, char *array_key)
json_t *key = GetSubObjectByKey(value, json_key);

if (key == NULL) {
FatalErrorOnInit("Can't find expected key '%s' in object", json_key);
/* ignore error as it can be a working mode where some entries
are not in the same format */
continue;
}

found = true;

const char *val = json_string_value(key);

DataJsonType json = { .value = NULL, .len = 0 };
Expand All @@ -358,6 +362,12 @@ static int DatajsonLoadString(Dataset *set, char *json_key, char *array_key)
}
}
json_decref(json);

if (found == false) {
FatalErrorOnInit(
"No valid entries for key '%s' found in the file '%s'", json_key, set->load);
return -1;
}
}
THashConsolidateMemcap(set->hash);

Expand Down

0 comments on commit add5abd

Please sign in to comment.