The open source OpenXR runtime
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

u/json: Add a lot more helper functions

authored by

Nova and committed by
Jakob Bornecrantz
d48022d5 4cfd4c46

+248 -12
+185 -12
src/xrt/auxiliary/util/u_json.c
··· 9 9 */ 10 10 11 11 #include "u_json.h" 12 + #include <assert.h> 12 13 14 + // This includes the c file completely. 13 15 #include "cjson/cJSON.c" 14 16 17 + 18 + /*! 19 + * Less typing. 20 + */ 21 + static inline const cJSON * 22 + get(const cJSON *json, const char *f) 23 + { 24 + return cJSON_GetObjectItemCaseSensitive(json, f); 25 + } 26 + 27 + bool 28 + u_json_get_string_into_array(const cJSON *json, char *out_str, size_t max_size) 29 + { 30 + assert(out_str != NULL); 31 + 32 + if (!json) { 33 + return false; 34 + } 35 + if (!cJSON_IsString(json)) { 36 + return false; 37 + } 38 + 39 + int ret = snprintf(out_str, max_size, "%s", json->valuestring); 40 + if (ret < 0) { 41 + return false; 42 + } else if ((size_t)ret < max_size) { 43 + return true; 44 + } else { 45 + return false; 46 + } 47 + } 48 + 49 + bool 50 + u_json_get_int(const cJSON *json, int *out_int) 51 + { 52 + assert(out_int != NULL); 53 + 54 + if (!json) { 55 + return false; 56 + } 57 + if (!cJSON_IsNumber(json)) { 58 + return false; 59 + } 60 + 61 + *out_int = json->valueint; 62 + 63 + return true; 64 + } 65 + 66 + bool 67 + u_json_get_double(const cJSON *json, double *out_double) 68 + { 69 + assert(out_double != NULL); 70 + 71 + if (!json) { 72 + return false; 73 + } 74 + if (!cJSON_IsNumber(json)) { 75 + return false; 76 + } 77 + *out_double = json->valuedouble; 78 + return true; 79 + } 80 + 81 + bool 82 + u_json_get_float(const cJSON *json, float *out_float) 83 + { 84 + assert(out_float != NULL); 85 + 86 + double d = 0; 87 + if (!u_json_get_double(json, &d)) { 88 + return false; 89 + } 90 + 91 + *out_float = (float)d; 92 + return true; 93 + } 94 + 95 + bool 96 + u_json_get_vec3(const cJSON *json, struct xrt_vec3 *out_vec3) 97 + { 98 + assert(out_vec3 != NULL); 99 + 100 + if (!json) { 101 + return false; 102 + } 103 + if (!cJSON_IsObject(json)) { 104 + return false; 105 + } 106 + 107 + struct xrt_vec3 ret; 108 + if (!u_json_get_float(get(json, "x"), &ret.x)) { 109 + return false; 110 + } 111 + if (!u_json_get_float(get(json, "y"), &ret.y)) { 112 + return false; 113 + } 114 + if (!u_json_get_float(get(json, "z"), &ret.z)) { 115 + return false; 116 + } 117 + 118 + *out_vec3 = ret; 119 + 120 + return true; 121 + } 122 + 123 + bool 124 + u_json_get_quat(const cJSON *json, struct xrt_quat *out_quat) 125 + { 126 + assert(out_quat != NULL); 127 + 128 + if (!json) { 129 + return false; 130 + } 131 + if (!cJSON_IsObject(json)) { 132 + return false; 133 + } 134 + 135 + struct xrt_quat ret; 136 + if (!u_json_get_float(get(json, "w"), &ret.w)) { 137 + return false; 138 + } 139 + if (!u_json_get_float(get(json, "x"), &ret.x)) { 140 + return false; 141 + } 142 + if (!u_json_get_float(get(json, "y"), &ret.y)) { 143 + return false; 144 + } 145 + if (!u_json_get_float(get(json, "z"), &ret.z)) { 146 + return false; 147 + } 148 + 149 + *out_quat = ret; 150 + 151 + return true; 152 + } 153 + 15 154 size_t 16 - u_json_get_double_array(const cJSON *json_array, 17 - double *out_array, 18 - size_t max_size) 155 + u_json_get_float_array(const cJSON *json_array, 156 + float *out_array, 157 + size_t max_size) 19 158 { 159 + assert(out_array != NULL); 160 + 20 161 if (!json_array) { 21 162 return 0; 22 163 } 23 - if (!out_array) { 164 + if (!cJSON_IsArray(json_array)) { 24 165 return 0; 25 166 } 26 - if (!cJSON_IsArray(json_array)) { 167 + 168 + size_t i = 0; 169 + const cJSON *elt; 170 + cJSON_ArrayForEach(elt, json_array) 171 + { 172 + if (i >= max_size) { 173 + break; 174 + } 175 + 176 + if (!u_json_get_float(elt, &out_array[i])) { 177 + fprintf(stderr, 178 + "warning: u_json_get_float_array got a " 179 + "non-number in a numeric array"); 180 + return i; 181 + } 182 + 183 + i++; 184 + } 185 + 186 + return i; 187 + } 188 + 189 + size_t 190 + u_json_get_double_array(const cJSON *json_array, 191 + double *out_array, 192 + size_t max_size) 193 + { 194 + assert(out_array != NULL); 195 + 196 + if (!json_array) { 27 197 return 0; 28 198 } 29 - if (max_size == 0) { 199 + if (!cJSON_IsArray(json_array)) { 30 200 return 0; 31 201 } 202 + 32 203 size_t i = 0; 33 204 const cJSON *elt; 34 205 cJSON_ArrayForEach(elt, json_array) 35 206 { 36 - if (!cJSON_IsNumber(elt)) { 207 + if (i >= max_size) { 208 + break; 209 + } 210 + 211 + if (!u_json_get_double(elt, &out_array[i])) { 37 212 fprintf(stderr, 38 213 "warning: u_json_get_double_array got a " 39 214 "non-number in a numeric array"); 40 215 return i; 41 216 } 42 - out_array[i] = elt->valuedouble; 43 - ++i; 44 - if (i == max_size) { 45 - break; 46 - } 217 + 218 + i++; 47 219 } 220 + 48 221 return i; 49 222 }
+63
src/xrt/auxiliary/util/u_json.h
··· 11 11 #pragma once 12 12 13 13 #include "xrt/xrt_compiler.h" 14 + #include "xrt/xrt_defines.h" 14 15 15 16 #include "cjson/cJSON.h" 16 17 ··· 19 20 extern "C" { 20 21 #endif // __cplusplus 21 22 23 + 24 + /*! 25 + * @brief Parse a string from a JSON object into a char array. 26 + * 27 + * @return true if successful, false if string does not fit in 28 + * array or any other error. 29 + */ 30 + bool 31 + u_json_get_string_into_array(const cJSON *json, char *out, size_t max_size); 32 + 33 + /*! 34 + * @brief Parse an int from a JSON object. 35 + * 36 + * @return true if successful, false if not. 37 + */ 38 + bool 39 + u_json_get_int(const cJSON *json, int *out_int); 40 + 41 + /*! 42 + * @brief Parse a float from a JSON object. 43 + * 44 + * @return true if successful, false if not. 45 + */ 46 + bool 47 + u_json_get_float(const cJSON *json, float *out_float); 48 + 49 + /*! 50 + * @brief Parse a double from a JSON object. 51 + * 52 + * @return true if successful, false if not. 53 + */ 54 + bool 55 + u_json_get_double(const cJSON *json, double *out_double); 56 + 57 + /*! 58 + * @brief Parse a vec3 from a JSON object. 59 + * 60 + * @return true if successful, false if not. 61 + */ 62 + bool 63 + u_json_get_vec3(const cJSON *json, struct xrt_vec3 *out_vec3); 64 + 65 + /*! 66 + * @brief Parse a quaternion from a JSON object. 67 + * 68 + * @return true if successful, false if not. 69 + */ 70 + bool 71 + u_json_get_quat(const cJSON *json, struct xrt_quat *out_quat); 72 + 73 + /*! 74 + * @brief Parse up to max_size floats from a JSON array. 75 + * 76 + * @return the number of elements set. 77 + */ 78 + size_t 79 + u_json_get_float_array(const cJSON *json_array, 80 + float *out_array, 81 + size_t max_size); 82 + 22 83 /*! 23 84 * @brief Parse up to max_size doubles from a JSON array. 24 85 * ··· 28 89 u_json_get_double_array(const cJSON *json_array, 29 90 double *out_array, 30 91 size_t max_size); 92 + 93 + 31 94 #ifdef __cplusplus 32 95 } // extern "C" 33 96 #endif // __cplusplus