Virgil IoT KIT
Data Structures | Macros | Enumerations | Functions
json_generator.h File Reference

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)
 

Detailed Description

JSON generator module.

This module is used to generate JSON strings

Usage

The JSON Generator can create various JSON strings. Here is an example:

json_str_init(&jstr, buff, sizeof(buff));
// This initializes the json generator to generate string in the buffer buff
json_set_val_str(&jstr, "name", "John Travolta");
json_set_val_int(&jstr, "roll", 123);
json_push_object(&jstr, "address");
json_set_val_str(&jstr, "house", "Atlas");
json_set_val_str(&jstr, "street", "New Avenue");
json_set_val_int(&jstr, "pin", 39065);

will create the above mentioned JSON string.

Similarly arrays can be created as:

will create the JSON string

{ "roll": [ [ 10, 20, 30] ] }

Similarly complex use-case like objects within arrays can be created as:

json_str_init(&jstr, buff, sizeof(buff));
json_push_array_object(&jstr, "arr");
json_set_val_str(&jstr, "name", "John Travolta");
json_set_val_int(&jstr, "roll", 123);
json_set_val_str(&jstr, "name", "Foo Bar");
json_set_val_int(&jstr, "roll", 112);

will create the JSON string

{ "arr": [
{ "name": "John Travolta", "roll": 123 },
{ "name": "Foo Bar", "roll": 112 }
]
}

Macro Definition Documentation

◆ json_pop_object

#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.

◆ json_set_array_bool

#define json_set_array_bool (   jptr,
  val 
)
Value:
((val == true) ? (json_set_array_value(jptr, NULL, 1, 0.0, JSON_VAL_BOOL)) \
: (json_set_array_value(jptr, NULL, 0, 0.0, JSON_VAL_BOOL)))

Create an boolean array element.

This function creates an boolean array element in a previously started JSON array.

Parameters
[in]jptrA pointer to the json_str handle returned by the json_str_init() call.
[in]valThe boolean value of the array element.

◆ json_set_array_float

#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.

Parameters
[in]jptrA pointer to the json_str handle returned by the json_str_init() call.
[in]valA pointer to the float value of the array element.

◆ json_set_array_int

#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.

Parameters
[in]jptrA pointer to the json_str handle returned by the json_str_init() call.
[in]valThe integer value of the array element.

◆ json_set_array_str

#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.

Parameters
[in]jptrA pointer to the json_str handle returned by the json_str_init() call.
[in]valA pointer to the string value of the array element.

◆ json_set_val_bool

#define json_set_val_bool (   jptr,
  name,
  val 
)
Value:
((val == true) ? (json_set_object_value(jptr, name, NULL, 1, 0.0, 0, JSON_VAL_BOOL)) \
: (json_set_object_value(jptr, name, NULL, 0, 0.0, 0, JSON_VAL_BOOL)))

Create a key with an boolean value.

This function adds a key-value pair to the JSON text with an integer as the value.

Parameters
[in]jptrA pointer to the json_str handle returned by the json_str_init() call.
[in]nameThe name of the key
[in]valThe boolean value of the key.

◆ json_set_val_float

#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.

Parameters
[in]jptrA pointer to the json_str handle returned by the json_str_init() call.
[in]nameThe name of the key
[in]valThe float value of the key

◆ json_set_val_float_precision

#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.

Parameters
jptrA pointer to the json_str handle returned by the json_str_init() call.
[in]nameThe name of the key
[in]valThe float value of the key
[in]precisionThe number of precision digits for float

◆ json_set_val_int

#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.

Parameters
[in]jptrA pointer to the json_str handle returned by the json_str_init() call.
[in]nameThe name of the key
[in]valThe integer value of the key

◆ json_set_val_null

#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.

Parameters
[in]jptrA pointer to the json_str handle returned by the json_str_init() call.
[in]nameThe name of the key

◆ json_set_val_str

#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.

Parameters
[in]jptrA pointer to the json_str handle returned by the json_str_init() call.
[in]nameThe name of the key
[in]valThe string value of the key

◆ json_set_val_uint

#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.

Parameters
[in]jptrA pointer to the json_str handle returned by the json_str_init() call.
[in]nameThe name of the key
[in]valThe unsigned integer value of the key

◆ json_set_val_uint_64

#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.

Parameters
[in]jptrA pointer to the json_str handle returned by the json_str_init() call.
[in]nameThe name of the key
[in]valThe unsigned 64 bit integer value of the key

◆ MAX_JSON_STR_LEN

#define MAX_JSON_STR_LEN   64 /* Maximum object or member name length */

◆ MAX_JSON_VAL_LEN

#define MAX_JSON_VAL_LEN   128 /* Maximum value name length */

Enumeration Type Documentation

◆ json_data_types

Enumerator
JSON_VAL_STR 
JSON_VAL_INT 
JSON_VAL_UINT 
JSON_VAL_UINT_64 
JSON_VAL_FLOAT 
JSON_VAL_BOOL 
JSON_VAL_NULL 

Function Documentation

◆ json_close_array()

int json_close_array ( struct json_str jptr)

Close a JSON array.

This function closes a JSON array started by json_start_array().

Parameters
[in]jptrPointer to json_str object.
Returns
-WM_E_JSON_OBUF if buffer available is insufficient or a buffer overflow has already occurred.
WM_SUCCESS if operation successful.

◆ json_close_object()

int json_close_object ( struct json_str jptr)

Close JSON object.

This function closes a JSON object that was started earlier.

Parameters
[in]jptrPointer to json_str object.
Returns
-WM_E_JSON_OBUF if buffer available is insufficient or a buffer overflow has already occurred.
WM_SUCCESS if operation successful.

◆ json_pop_array_object()

int json_pop_array_object ( struct json_str jptr)

Close a JSON array object.

This closes a previously pushed JSON object.

Parameters
[in]jptrPointer to json_str object.
Returns
-WM_E_JSON_OBUF if buffer available is insufficient or a buffer overflow has already occurred.
WM_SUCCESS if operation successful.

◆ json_push_array_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.

Parameters
[in]jptrA pointer to the json_str handle returned by the json_str_init() call.
[in]nameThe name of the array object
Returns
-WM_E_JSON_OBUF if buffer available is insufficient or a buffer overflow has already occurred.
WM_SUCCESS if operation successful.

◆ json_push_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.

Parameters
[in]jptrA pointer to the json_str handle returned by the json_str_init() call.
[in]nameThe name of the composite object that should be created.
Returns
-WM_E_JSON_OBUF if buffer available is insufficient or a buffer overflow has already occurred.
WM_SUCCESS if operation successful.

◆ json_set_array_value()

int json_set_array_value ( struct json_str jptr,
char *  str,
int  value,
float  val,
json_data_types  data 
)

◆ json_set_object_value()

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 
)

◆ json_start_array()

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.

Parameters
[in]jptrPointer to json_str object.
Returns
-WM_E_JSON_OBUF if buffer available is insufficient or a buffer overflow has already occurred.
WM_SUCCESS if operation successful.

◆ json_start_object()

int json_start_object ( struct json_str jptr)

Start a new JSON object.

This function starts a new JSON object

Parameters
[in]jptrPointer to json_str object.
Returns
-WM_E_JSON_OBUF if buffer available is insufficient or a buffer overflow has already occurred.
WM_SUCCESS if operation successful.

◆ json_str_finish()

void json_str_finish ( struct json_str jptr)

◆ json_str_init()

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.

Parameters
[out]jptrA pointer to the structure json_str which will be used as a handle for the rest of the json_str operations.
[out]buffA pointer to a buffer that will be used to store the generated JSON data.
[in]lenThe length of the buffer pointed to by buff

◆ json_str_init_no_clear()

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.

Parameters
[out]jptrA pointer to the structure json_str which will be used as a handle for the rest of the json_str operations.
[out]buffA pointer to a buffer that will be used to store the generated JSON data.
[in]lenThe length of the buffer pointed to by buff
json_set_val_int
#define json_set_val_int(jptr, name, val)
Create a key with an integer value.
Definition: json_generator.h:252
json_set_array_value
int json_set_array_value(struct json_str *jptr, char *str, int value, float val, json_data_types data)
JSON_VAL_BOOL
@ JSON_VAL_BOOL
Definition: json_generator.h:91
json_pop_array_object
int json_pop_array_object(struct json_str *jptr)
Close a JSON array object.
json_set_object_value
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)
json_start_object
int json_start_object(struct json_str *jptr)
Start a new JSON object.
json_pop_object
#define json_pop_object(jptr)
Close a composite object.
Definition: json_generator.h:178
json_push_object
int json_push_object(struct json_str *jptr, const char *name)
Start a new composite object.
json_set_array_int
#define json_set_array_int(jptr, val)
Create an integer array element.
Definition: json_generator.h:381
json_push_array_object
int json_push_array_object(struct json_str *jptr, const char *name)
Start a JSON array object.
json_set_val_str
#define json_set_val_str(jptr, name, val)
Create a key with a string value.
Definition: json_generator.h:240
json_str_init
void json_str_init(struct json_str *jptr, char *buff, int len)
Initialize the JSON generator.
json_close_object
int json_close_object(struct json_str *jptr)
Close JSON object.