The open source OpenXR runtime
at main 158 lines 3.7 kB view raw
1// Copyright 2019-2020, Collabora, Ltd. 2// SPDX-License-Identifier: BSL-1.0 3/*! 4 * @file 5 * @brief Tiny JSON wrapper around cJSON header. 6 * @author Jakob Bornecrantz <jakob@collabora.com> 7 * @author Rylie Pavlik <rylie.pavlik@collabora.com> 8 * @ingroup aux_util 9 */ 10 11#pragma once 12 13#include "xrt/xrt_compiler.h" 14#include "xrt/xrt_defines.h" 15 16#include <cjson/cJSON.h> // IWYU pragma: export 17 18 19#ifdef __cplusplus 20extern "C" { 21#endif // __cplusplus 22 23/*! 24 * @brief Get a JSON object by string from a JSON object. 25 * 26 * @return The JSON object with the given name if successful, NULL if not. 27 */ 28const cJSON * 29u_json_get(const cJSON *json, const char *f); 30 31/*! 32 * @brief Parse a string from a JSON object into a char array. 33 * 34 * @return true if successful, false if string does not fit in 35 * array or any other error. 36 */ 37bool 38u_json_get_string_into_array(const cJSON *json, char *out, size_t max_size); 39 40/*! 41 * @brief Parse an bool from a JSON object. 42 * 43 * @return true if successful, false if not. 44 */ 45bool 46u_json_get_bool(const cJSON *json, bool *out_bool); 47 48/*! 49 * @brief Parse an int from a JSON object. 50 * 51 * @return true if successful, false if not. 52 */ 53bool 54u_json_get_int(const cJSON *json, int *out_int); 55 56/*! 57 * @brief Parse a float from a JSON object. 58 * 59 * @return true if successful, false if not. 60 */ 61bool 62u_json_get_float(const cJSON *json, float *out_float); 63 64/*! 65 * @brief Parse a double from a JSON object. 66 * 67 * @return true if successful, false if not. 68 */ 69bool 70u_json_get_double(const cJSON *json, double *out_double); 71 72/*! 73 * @brief Parse an xrt_vec3 from a JSON object. 74 * 75 * @return true if successful, false if not. 76 */ 77bool 78u_json_get_vec3(const cJSON *json, struct xrt_vec3 *out_vec3); 79 80/*! 81 * @brief Parse an xrt_vec3 from a JSON array. 82 * 83 * @return true if successful, false if not. 84 */ 85bool 86u_json_get_vec3_array(const cJSON *json, struct xrt_vec3 *out_vec3); 87 88/*! 89 * @brief Parse an xrt_vec3_f64 from a JSON array. 90 * 91 * @return true if successful, false if not. 92 */ 93bool 94u_json_get_vec3_f64_array(const cJSON *json, struct xrt_vec3_f64 *out_vec3); 95 96/*! 97 * @brief Parse a quaternion from a JSON object. 98 * 99 * @return true if successful, false if not. 100 */ 101bool 102u_json_get_quat(const cJSON *json, struct xrt_quat *out_quat); 103 104/*! 105 * @brief Parse a pose from a JSON object, composed of a vec3 named "position" and a quat named "orientation". 106 * 107 * @return true if successful, false if not. 108 */ 109bool 110u_json_get_pose(const cJSON *json, struct xrt_pose *out_pose); 111 112 113/*! 114 * @brief Parse a pose from a JSON object, composed of 115 * a vec3 named "position", "translation", "location", "pos", or "loc" 116 * and a quat named "orientation". "rotation", or "rot" 117 * 118 * @return true if successful, false if not. 119 */ 120bool 121u_json_get_pose_permissive(const cJSON *json, struct xrt_pose *out_pose); 122 123/*! 124 * @brief Parse up to max_size floats from a JSON array. 125 * 126 * @return the number of elements set. 127 */ 128size_t 129u_json_get_float_array(const cJSON *json_array, float *out_array, size_t max_size); 130 131/*! 132 * @brief Parse up to max_size doubles from a JSON array. 133 * 134 * @return the number of elements set. 135 */ 136size_t 137u_json_get_double_array(const cJSON *json_array, double *out_array, size_t max_size); 138 139/*! 140 * @brief Parse up to max_size int from a JSON array. 141 * 142 * @return the number of elements set. 143 */ 144size_t 145u_json_get_int_array(const cJSON *json_array, int *out_array, size_t max_size); 146 147/*! 148 * @brief Parse a matrix_3x3 from a JSON object. 149 * 150 * @return true if successful, false if not. 151 */ 152bool 153u_json_get_matrix_3x3(const cJSON *json, struct xrt_matrix_3x3 *out_matrix); 154 155 156#ifdef __cplusplus 157} // extern "C" 158#endif // __cplusplus