diff --git a/doc/changes/aux/mr.266.2.md b/doc/changes/aux/mr.266.2.md new file mode 100644 index 000000000..e6482e939 --- /dev/null +++ b/doc/changes/aux/mr.266.2.md @@ -0,0 +1 @@ +u/json: Add bool getter function. diff --git a/src/xrt/auxiliary/util/u_json.c b/src/xrt/auxiliary/util/u_json.c index 3778520b3..035b29cd8 100644 --- a/src/xrt/auxiliary/util/u_json.c +++ b/src/xrt/auxiliary/util/u_json.c @@ -46,6 +46,23 @@ u_json_get_string_into_array(const cJSON *json, char *out_str, size_t max_size) } } +bool +u_json_get_bool(const cJSON *json, bool *out_bool) +{ + assert(out_bool != NULL); + + if (!json) { + return false; + } + if (!cJSON_IsBool(json)) { + return false; + } + + *out_bool = json->valueint; + + return true; +} + bool u_json_get_int(const cJSON *json, int *out_int) { diff --git a/src/xrt/auxiliary/util/u_json.h b/src/xrt/auxiliary/util/u_json.h index c63633be2..939585ac3 100644 --- a/src/xrt/auxiliary/util/u_json.h +++ b/src/xrt/auxiliary/util/u_json.h @@ -30,6 +30,14 @@ extern "C" { bool u_json_get_string_into_array(const cJSON *json, char *out, size_t max_size); +/*! + * @brief Parse an bool from a JSON object. + * + * @return true if successful, false if not. + */ +bool +u_json_get_bool(const cJSON *json, bool *out_bool); + /*! * @brief Parse an int from a JSON object. *