Monado OpenXR Runtime
u_json.h
Go to the documentation of this file.
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 Ryan Pavlik <ryan.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"
17 
18 
19 #ifdef __cplusplus
20 extern "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  */
28 const cJSON *
29 u_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  */
37 bool
38 u_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  */
45 bool
46 u_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  */
53 bool
54 u_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  */
61 bool
62 u_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  */
69 bool
70 u_json_get_double(const cJSON *json, double *out_double);
71 
72 /*!
73  * @brief Parse a vec3 from a JSON object.
74  *
75  * @return true if successful, false if not.
76  */
77 bool
78 u_json_get_vec3(const cJSON *json, struct xrt_vec3 *out_vec3);
79 
80 /*!
81  * @brief Parse a quaternion from a JSON object.
82  *
83  * @return true if successful, false if not.
84  */
85 bool
86 u_json_get_quat(const cJSON *json, struct xrt_quat *out_quat);
87 
88 /*!
89  * @brief Parse up to max_size floats from a JSON array.
90  *
91  * @return the number of elements set.
92  */
93 size_t
94 u_json_get_float_array(const cJSON *json_array,
95  float *out_array,
96  size_t max_size);
97 
98 /*!
99  * @brief Parse up to max_size doubles from a JSON array.
100  *
101  * @return the number of elements set.
102  */
103 size_t
104 u_json_get_double_array(const cJSON *json_array,
105  double *out_array,
106  size_t max_size);
107 
108 
109 #ifdef __cplusplus
110 } // extern "C"
111 #endif // __cplusplus
size_t u_json_get_float_array(const cJSON *json_array, float *out_array, size_t max_size)
Parse up to max_size floats from a JSON array.
Definition: u_json.c:181
A 3 element vector with single floats.
Definition: xrt_defines.h:133
bool u_json_get_quat(const cJSON *json, struct xrt_quat *out_quat)
Parse a quaternion from a JSON object.
Definition: u_json.c:150
bool u_json_get_float(const cJSON *json, float *out_float)
Parse a float from a JSON object.
Definition: u_json.c:108
const cJSON * u_json_get(const cJSON *json, const char *f)
Get a JSON object by string from a JSON object.
Definition: u_json.c:31
struct cJSON cJSON
Definition: xrt_prober.h:26
A quaternion with single floats.
Definition: xrt_defines.h:99
Common defines and enums for XRT.
bool u_json_get_bool(const cJSON *json, bool *out_bool)
Parse an bool from a JSON object.
Definition: u_json.c:59
bool u_json_get_int(const cJSON *json, int *out_int)
Parse an int from a JSON object.
Definition: u_json.c:76
size_t u_json_get_double_array(const cJSON *json_array, double *out_array, size_t max_size)
Parse up to max_size doubles from a JSON array.
Definition: u_json.c:216
bool u_json_get_vec3(const cJSON *json, struct xrt_vec3 *out_vec3)
Parse a vec3 from a JSON object.
Definition: u_json.c:122
bool u_json_get_string_into_array(const cJSON *json, char *out, size_t max_size)
Parse a string from a JSON object into a char array.
Definition: u_json.c:37
Header holding common defines.
bool u_json_get_double(const cJSON *json, double *out_double)
Parse a double from a JSON object.
Definition: u_json.c:93