Monado OpenXR Runtime
u_json.c File Reference

Tiny JSON wrapper around cJSON. More...

#include "util/u_json.h"
#include "util/u_logging.h"
#include <assert.h>
#include <stdio.h>
#include "cjson/cJSON.c"
Include dependency graph for u_json.c:

Functions

static const cJSON * get (const cJSON *json, const char *f)
 Less typing. More...
 
const cJSON * u_json_get (const cJSON *json, const char *f)
 Get a JSON object by string from a JSON object. More...
 
bool u_json_get_string_into_array (const cJSON *json, char *out_str, size_t max_size)
 Parse a string from a JSON object into a char array. More...
 
bool u_json_get_bool (const cJSON *json, bool *out_bool)
 Parse an bool from a JSON object. More...
 
bool u_json_get_int (const cJSON *json, int *out_int)
 Parse an int from a JSON object. More...
 
bool u_json_get_double (const cJSON *json, double *out_double)
 Parse a double from a JSON object. More...
 
bool u_json_get_float (const cJSON *json, float *out_float)
 Parse a float from a JSON object. More...
 
bool u_json_get_vec3 (const cJSON *json, struct xrt_vec3 *out_vec3)
 Parse an xrt_vec3 from a JSON object. More...
 
bool u_json_get_vec3_array (const cJSON *json, struct xrt_vec3 *out_vec3)
 Parse an xrt_vec3 from a JSON array. More...
 
bool u_json_get_vec3_f64_array (const cJSON *json, struct xrt_vec3_f64 *out_vec3)
 Parse an xrt_vec3_f64 from a JSON array. More...
 
bool u_json_get_quat (const cJSON *json, struct xrt_quat *out_quat)
 Parse a quaternion from a JSON object. More...
 
bool u_json_get_pose (const cJSON *json, struct xrt_pose *out_pose)
 Parse a pose from a JSON object, composed of a vec3 named "position" and a quat named "orientation". More...
 
bool u_json_get_pose_permissive (const cJSON *json, struct xrt_pose *out_pose)
 Parse a pose from a JSON object, composed of a vec3 named "position", "translation", "location", "pos", or "loc" and a quat named "orientation". More...
 
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. More...
 
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. More...
 
size_t u_json_get_int_array (const cJSON *json_array, int *out_array, size_t max_size)
 Parse up to max_size int from a JSON array. More...
 
bool u_json_get_matrix_3x3 (const cJSON *json, struct xrt_matrix_3x3 *out_matrix)
 Parse a matrix_3x3 from a JSON object. More...
 

Detailed Description

Function Documentation

◆ get()

static const cJSON * get ( const cJSON *  json,
const char *  f 
)
inlinestatic

◆ u_json_get()

const cJSON * u_json_get ( const cJSON *  json,
const char *  f 
)

Get a JSON object by string from a JSON object.

Returns
The JSON object with the given name if successful, NULL if not.

References get().

Referenced by json_bool(), json_int(), json_rs2_format(), json_rs2_stream(), u_json_get_pose(), and u_json_get_pose_permissive().

◆ u_json_get_bool()

bool u_json_get_bool ( const cJSON *  json,
bool out_bool 
)

Parse an bool from a JSON object.

Returns
true if successful, false if not.

Referenced by json_bool().

◆ u_json_get_double()

bool u_json_get_double ( const cJSON *  json,
double *  out_double 
)

Parse a double from a JSON object.

Returns
true if successful, false if not.

Referenced by u_json_get_double_array(), and u_json_get_float().

◆ u_json_get_double_array()

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.

Returns
the number of elements set.

References u_json_get_double(), and U_LOG_W.

◆ u_json_get_float()

bool u_json_get_float ( const cJSON *  json,
float *  out_float 
)

Parse a float from a JSON object.

Returns
true if successful, false if not.

References u_json_get_double().

Referenced by u_json_get_float_array(), u_json_get_quat(), and u_json_get_vec3().

◆ u_json_get_float_array()

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.

Returns
the number of elements set.

References u_json_get_float(), and U_LOG_W.

◆ u_json_get_int()

bool u_json_get_int ( const cJSON *  json,
int *  out_int 
)

Parse an int from a JSON object.

Returns
true if successful, false if not.

Referenced by json_int(), json_rs2_format(), json_rs2_stream(), and u_json_get_int_array().

◆ u_json_get_int_array()

size_t u_json_get_int_array ( const cJSON *  json_array,
int *  out_array,
size_t  max_size 
)

Parse up to max_size int from a JSON array.

Returns
the number of elements set.

References u_json_get_int(), and U_LOG_W.

◆ u_json_get_matrix_3x3()

bool u_json_get_matrix_3x3 ( const cJSON *  json,
struct xrt_matrix_3x3 out_matrix 
)

Parse a matrix_3x3 from a JSON object.

Returns
true if successful, false if not.

◆ u_json_get_pose()

bool u_json_get_pose ( const cJSON *  json,
struct xrt_pose out_pose 
)

Parse a pose from a JSON object, composed of a vec3 named "position" and a quat named "orientation".

Returns
true if successful, false if not.

References u_json_get(), u_json_get_quat(), and u_json_get_vec3().

◆ u_json_get_pose_permissive()

bool u_json_get_pose_permissive ( const cJSON *  json,
struct xrt_pose out_pose 
)

Parse a pose from a JSON object, composed of a vec3 named "position", "translation", "location", "pos", or "loc" and a quat named "orientation".

"rotation", or "rot"

Returns
true if successful, false if not.

References ARRAY_SIZE, u_json_get(), and u_json_get_vec3().

◆ u_json_get_quat()

bool u_json_get_quat ( const cJSON *  json,
struct xrt_quat out_quat 
)

Parse a quaternion from a JSON object.

Returns
true if successful, false if not.

References get(), and u_json_get_float().

Referenced by u_json_get_pose().

◆ u_json_get_string_into_array()

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.

Returns
true if successful, false if string does not fit in array or any other error.

References U_LOG_E.

◆ u_json_get_vec3()

bool u_json_get_vec3 ( const cJSON *  json,
struct xrt_vec3 out_vec3 
)

Parse an xrt_vec3 from a JSON object.

Returns
true if successful, false if not.

References get(), and u_json_get_float().

Referenced by u_json_get_pose(), and u_json_get_pose_permissive().

◆ u_json_get_vec3_array()

bool u_json_get_vec3_array ( const cJSON *  json,
struct xrt_vec3 out_vec3 
)

Parse an xrt_vec3 from a JSON array.

Returns
true if successful, false if not.

◆ u_json_get_vec3_f64_array()

bool u_json_get_vec3_f64_array ( const cJSON *  json,
struct xrt_vec3_f64 out_vec3 
)

Parse an xrt_vec3_f64 from a JSON array.

Returns
true if successful, false if not.