u/u_json: Implement u_json_get_int_array()

This commit is contained in:
Jan Schmidt 2022-05-22 00:47:34 +10:00 committed by Jakob Bornecrantz
parent ce13de9484
commit f8a486884b
2 changed files with 41 additions and 0 deletions

View file

@ -328,6 +328,39 @@ u_json_get_double_array(const cJSON *json_array, double *out_array, size_t max_s
return i;
}
size_t
u_json_get_int_array(const cJSON *json_array, int *out_array, size_t max_size)
{
assert(out_array != NULL);
if (!json_array) {
return 0;
}
if (!cJSON_IsArray(json_array)) {
return 0;
}
size_t i = 0;
const cJSON *elt;
cJSON_ArrayForEach(elt, json_array)
{
if (i >= max_size) {
break;
}
if (!u_json_get_int(elt, &out_array[i])) {
U_LOG_W(
"u_json_get_int got a non-number in a "
"numeric array");
return i;
}
i++;
}
return i;
}
bool
u_json_get_matrix_3x3(const cJSON *json, struct xrt_matrix_3x3 *out_matrix)
{

View file

@ -117,6 +117,14 @@ u_json_get_float_array(const cJSON *json_array, float *out_array, size_t max_siz
size_t
u_json_get_double_array(const cJSON *json_array, double *out_array, size_t max_size);
/*!
* @brief Parse up to max_size int from a JSON array.
*
* @return the number of elements set.
*/
size_t
u_json_get_int_array(const cJSON *json_array, int *out_array, size_t max_size);
/*!
* @brief Parse a matrix_3x3 from a JSON object.
*