Virgil IoT KIT
|
JSON generator module. More...
Go to the source code of this file.
Data Structures | |
struct | json_str |
Macros | |
#define | MAX_JSON_STR_LEN 64 /* Maximum object or member name length */ |
#define | MAX_JSON_VAL_LEN 128 /* Maximum value name length */ |
#define | json_pop_object(jptr) json_close_object((jptr)) |
Close a composite object. More... | |
#define | json_set_val_str(jptr, name, val) json_set_object_value(jptr, name, val, 0, 0.0, 0, JSON_VAL_STR) |
Create a key with a string value. More... | |
#define | json_set_val_int(jptr, name, val) json_set_object_value(jptr, name, NULL, val, 0.0, 0, JSON_VAL_INT) |
Create a key with an integer value. More... | |
#define | json_set_val_uint(jptr, name, val) json_set_object_value(jptr, name, NULL, val, 0.0, 0, JSON_VAL_UINT) |
Create a key with an unsigned integer value. More... | |
#define | json_set_val_uint_64(jptr, name, val) json_set_object_value(jptr, name, NULL, val, 0.0, 0, JSON_VAL_UINT_64) |
Create a key with an unsigned 64 bit integer value. More... | |
#define | json_set_val_float(jptr, name, val) json_set_object_value(jptr, name, NULL, 0, val, 2, JSON_VAL_FLOAT) |
Create a key with a float value. More... | |
#define | json_set_val_float_precision(jptr, name, val, precision) json_set_object_value(jptr, name, NULL, 0, val, precision, JSON_VAL_FLOAT) |
Create a key with a float value and decimal precision. More... | |
#define | json_set_val_bool(jptr, name, val) |
Create a key with an boolean value. More... | |
#define | json_set_val_null(jptr, name) (json_set_object_value(jptr, name, NULL, 0, 0.0, 0, JSON_VAL_NULL)) |
Create a key with a null value. More... | |
#define | json_set_array_str(jptr, val) json_set_array_value(jptr, val, 0, 0.0, JSON_VAL_STR) |
Create a string array element. More... | |
#define | json_set_array_int(jptr, val) json_set_array_value(jptr, NULL, val, 0.0, JSON_VAL_INT) |
Create an integer array element. More... | |
#define | json_set_array_float(jptr, val) json_set_array_value(jptr, NULL, 0, val, JSON_VAL_FLOAT) |
Create a float array element. More... | |
#define | json_set_array_bool(jptr, val) |
Create an boolean array element. More... | |
Enumerations | |
enum | json_data_types { JSON_VAL_STR, JSON_VAL_INT, JSON_VAL_UINT, JSON_VAL_UINT_64, JSON_VAL_FLOAT, JSON_VAL_BOOL, JSON_VAL_NULL } |
Functions | |
void | json_str_init (struct json_str *jptr, char *buff, int len) |
Initialize the JSON generator. More... | |
void | json_str_init_no_clear (struct json_str *jptr, char *buff, int len) |
Initialize the JSON generator without clearing the buffer. More... | |
void | json_str_finish (struct json_str *jptr) |
int | json_start_object (struct json_str *jptr) |
Start a new JSON object. More... | |
int | json_push_object (struct json_str *jptr, const char *name) |
Start a new composite object. More... | |
int | json_push_array_object (struct json_str *jptr, const char *name) |
Start a JSON array object. More... | |
int | json_pop_array_object (struct json_str *jptr) |
Close a JSON array object. More... | |
int | json_close_object (struct json_str *jptr) |
Close JSON object. More... | |
int | json_set_object_value (struct json_str *jptr, const char *name, const char *str, int64_t value, float val, short precision, json_data_types data) |
int | json_start_array (struct json_str *jptr) |
Start a JSON array. More... | |
int | json_close_array (struct json_str *jptr) |
Close a JSON array. More... | |
int | json_set_array_value (struct json_str *jptr, char *str, int value, float val, json_data_types data) |
JSON generator module.
This module is used to generate JSON strings
The JSON Generator can create various JSON strings. Here is an example:
will create the above mentioned JSON string.
Similarly arrays can be created as:
will create the JSON string
Similarly complex use-case like objects within arrays can be created as:
will create the JSON string
#define json_pop_object | ( | jptr | ) | json_close_object((jptr)) |
Close a composite object.
This closes a composite object. Any json_set() calls after this will create key-value pairs at the same depth as the composite object that was closed.
#define json_set_array_bool | ( | jptr, | |
val | |||
) |
Create an boolean array element.
This function creates an boolean array element in a previously started JSON array.
[in] | jptr | A pointer to the json_str handle returned by the json_str_init() call. |
[in] | val | The boolean value of the array element. |
#define json_set_array_float | ( | jptr, | |
val | |||
) | json_set_array_value(jptr, NULL, 0, val, JSON_VAL_FLOAT) |
Create a float array element.
This function creates a float array element in a previously started JSON array.
[in] | jptr | A pointer to the json_str handle returned by the json_str_init() call. |
[in] | val | A pointer to the float value of the array element. |
#define json_set_array_int | ( | jptr, | |
val | |||
) | json_set_array_value(jptr, NULL, val, 0.0, JSON_VAL_INT) |
Create an integer array element.
This function creates an integer array element in a previously started JSON array.
[in] | jptr | A pointer to the json_str handle returned by the json_str_init() call. |
[in] | val | The integer value of the array element. |
#define json_set_array_str | ( | jptr, | |
val | |||
) | json_set_array_value(jptr, val, 0, 0.0, JSON_VAL_STR) |
Create a string array element.
This function creates a string array element in a previously started JSON array.
[in] | jptr | A pointer to the json_str handle returned by the json_str_init() call. |
[in] | val | A pointer to the string value of the array element. |
#define json_set_val_bool | ( | jptr, | |
name, | |||
val | |||
) |
Create a key with an boolean value.
This function adds a key-value pair to the JSON text with an integer as the value.
[in] | jptr | A pointer to the json_str handle returned by the json_str_init() call. |
[in] | name | The name of the key |
[in] | val | The boolean value of the key. |
#define json_set_val_float | ( | jptr, | |
name, | |||
val | |||
) | json_set_object_value(jptr, name, NULL, 0, val, 2, JSON_VAL_FLOAT) |
Create a key with a float value.
This function adds a key-value pair to the JSON text with a float as the value.
[in] | jptr | A pointer to the json_str handle returned by the json_str_init() call. |
[in] | name | The name of the key |
[in] | val | The float value of the key |
#define json_set_val_float_precision | ( | jptr, | |
name, | |||
val, | |||
precision | |||
) | json_set_object_value(jptr, name, NULL, 0, val, precision, JSON_VAL_FLOAT) |
Create a key with a float value and decimal precision.
This function adds a key-value pair to the JSON text with a float as the value.
jptr | A pointer to the json_str handle returned by the json_str_init() call. | |
[in] | name | The name of the key |
[in] | val | The float value of the key |
[in] | precision | The number of precision digits for float |
#define json_set_val_int | ( | jptr, | |
name, | |||
val | |||
) | json_set_object_value(jptr, name, NULL, val, 0.0, 0, JSON_VAL_INT) |
Create a key with an integer value.
This function adds a key-value pair to the JSON text with an integer as the value.
[in] | jptr | A pointer to the json_str handle returned by the json_str_init() call. |
[in] | name | The name of the key |
[in] | val | The integer value of the key |
#define json_set_val_null | ( | jptr, | |
name | |||
) | (json_set_object_value(jptr, name, NULL, 0, 0.0, 0, JSON_VAL_NULL)) |
Create a key with a null value.
This function adds a key-value pair to the JSON text with null as the value.
[in] | jptr | A pointer to the json_str handle returned by the json_str_init() call. |
[in] | name | The name of the key |
#define json_set_val_str | ( | jptr, | |
name, | |||
val | |||
) | json_set_object_value(jptr, name, val, 0, 0.0, 0, JSON_VAL_STR) |
Create a key with a string value.
This function adds a key-value pair to the JSON text with string as the value.
[in] | jptr | A pointer to the json_str handle returned by the json_str_init() call. |
[in] | name | The name of the key |
[in] | val | The string value of the key |
#define json_set_val_uint | ( | jptr, | |
name, | |||
val | |||
) | json_set_object_value(jptr, name, NULL, val, 0.0, 0, JSON_VAL_UINT) |
Create a key with an unsigned integer value.
This function adds a key-value pair to the JSON text with an unsigned integer as the value.
[in] | jptr | A pointer to the json_str handle returned by the json_str_init() call. |
[in] | name | The name of the key |
[in] | val | The unsigned integer value of the key |
#define json_set_val_uint_64 | ( | jptr, | |
name, | |||
val | |||
) | json_set_object_value(jptr, name, NULL, val, 0.0, 0, JSON_VAL_UINT_64) |
Create a key with an unsigned 64 bit integer value.
This function adds a key-value pair to the JSON text with an unsigned 64 bit integer as the value.
[in] | jptr | A pointer to the json_str handle returned by the json_str_init() call. |
[in] | name | The name of the key |
[in] | val | The unsigned 64 bit integer value of the key |
#define MAX_JSON_STR_LEN 64 /* Maximum object or member name length */ |
#define MAX_JSON_VAL_LEN 128 /* Maximum value name length */ |
enum json_data_types |
int json_close_array | ( | struct json_str * | jptr | ) |
Close a JSON array.
This function closes a JSON array started by json_start_array().
[in] | jptr | Pointer to json_str object. |
int json_close_object | ( | struct json_str * | jptr | ) |
Close JSON object.
This function closes a JSON object that was started earlier.
[in] | jptr | Pointer to json_str object. |
int json_pop_array_object | ( | struct json_str * | jptr | ) |
Close a JSON array object.
This closes a previously pushed JSON object.
[in] | jptr | Pointer to json_str object. |
int json_push_array_object | ( | struct json_str * | jptr, |
const char * | name | ||
) |
Start a JSON array object.
This function creates an element that has an array as its value.
[in] | jptr | A pointer to the json_str handle returned by the json_str_init() call. |
[in] | name | The name of the array object |
int json_push_object | ( | struct json_str * | jptr, |
const char * | name | ||
) |
Start a new composite object.
This function starts a new composite object which may have its own set of key-value pairs. Any json_set() calls performed after this will create key-value pairs within this composite object.
[in] | jptr | A pointer to the json_str handle returned by the json_str_init() call. |
[in] | name | The name of the composite object that should be created. |
int json_set_array_value | ( | struct json_str * | jptr, |
char * | str, | ||
int | value, | ||
float | val, | ||
json_data_types | data | ||
) |
int json_set_object_value | ( | struct json_str * | jptr, |
const char * | name, | ||
const char * | str, | ||
int64_t | value, | ||
float | val, | ||
short | precision, | ||
json_data_types | data | ||
) |
int json_start_array | ( | struct json_str * | jptr | ) |
Start a JSON array.
This function starts a JSON array. This is different than the json_push_arraj_object(), in that json_push_array_object() creates an key-value pair with an array as the value, while this simply starts an array object.
[in] | jptr | Pointer to json_str object. |
int json_start_object | ( | struct json_str * | jptr | ) |
Start a new JSON object.
This function starts a new JSON object
[in] | jptr | Pointer to json_str object. |
void json_str_finish | ( | struct json_str * | jptr | ) |
void json_str_init | ( | struct json_str * | jptr, |
char * | buff, | ||
int | len | ||
) |
Initialize the JSON generator.
This function initializes the JSON generator. It also clears the buffer passed to it internally.
void json_str_init_no_clear | ( | struct json_str * | jptr, |
char * | buff, | ||
int | len | ||
) |
Initialize the JSON generator without clearing the buffer.
This function initializes the JSON generator without clearing the received buffer. This can be useful for cases where the same buffer is being used for creating the JSON as well as for any intermediate computations (like encoding). In order to NULL terminate the JSON string, the json_str_finish() API needs to be called at the end, after the JSON is ready.