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

JSON Parser module. More...

#include <stdbool.h>
#include <stdint.h>
#include "jsmn.h"

Go to the source code of this file.

Data Structures

struct  jobj_t
 Object used by the JSON parser internally. More...
 

Macros

#define MOD_JSON   7
 
#define MOD_ERROR_START(x)   (x << 12 | 0)
 
#define VS_JSON_ERR_OK   0
 

Typedefs

typedef int jsmnindex_t
 
typedef jsmntype_t jsmnenumtype_t
 
typedef jsmntok_t jsontok_t
 JSON token. More...
 
typedef jsmn_parser json_parser_t
 

Enumerations

enum  wm_json_errno {
  WM_E_JSON_ERRNO_BASE = MOD_ERROR_START(MOD_JSON), WM_E_JSON_FAIL, WM_E_JSON_NOMEM, WM_E_JSON_INVAL,
  WM_E_JSON_INCOMPLETE, WM_E_JSON_INVALID_JOBJ, WM_E_JSON_NOT_FOUND, WM_E_JSON_INVALID_TYPE,
  WM_E_JSON_INVALID_INDEX, WM_E_JSON_INVALID_JARRAY, WM_E_JSON_INVSTART, WM_E_JSON_OBUF
}
 Json Error Codes. More...
 

Functions

int json_init (jobj_t *jobj, jsontok_t *tokens, int num_tokens, char *js, size_t js_len)
 Initialize the JSON Parser and parse the given JSON string. More...
 
int json_parse_start (jobj_t *jobj, char *js, size_t js_len)
 Start JSON Parsing. More...
 
void json_parse_stop (jobj_t *jobj)
 Stop JSON Parsing. More...
 
bool json_is_object (jobj_t *jobj)
 Find out if the current object is a JSON Object. More...
 
bool json_is_array (jobj_t *jobj)
 Find out if the current object is a JSON Array. More...
 
int json_get_val_bool (jobj_t *jobj, char *key, bool *value)
 Get JSON bool value. More...
 
int json_get_val_int (jobj_t *jobj, char *key, int *value)
 Get JSON integer value. More...
 
int json_get_val_int64 (jobj_t *jobj, char *key, int64_t *value)
 Get 64bit JSON integer value. More...
 
int json_get_val_float (jobj_t *jobj, char *key, float *value)
 Get JSON float value. More...
 
int json_get_val_str (jobj_t *jobj, char *key, char *value, int max_len)
 Get JSON string value. More...
 
int json_get_val_str_len (jobj_t *jobj, char *key, int *len)
 Get JSON string length. More...
 
int json_get_composite_object (jobj_t *jobj, char *key)
 Get JSON composite object. More...
 
int json_release_composite_object (jobj_t *jobj)
 Release a JSON composite object. More...
 
int json_get_array_object (jobj_t *jobj, char *key, int *num_elements)
 Get JSON array object. More...
 
int json_release_array_object (jobj_t *jobj)
 Release a JSON array object. More...
 
int json_array_get_num_elements (jobj_t *jobj)
 Get number of elements in an array. More...
 
int json_array_get_bool (jobj_t *jobj, uint16_t index, bool *value)
 Get JSON bool value from array. More...
 
int json_array_get_int (jobj_t *jobj, uint16_t index, int *value)
 Get JSON integer value from array. More...
 
int json_array_get_int64 (jobj_t *jobj, uint16_t index, int64_t *value)
 Get 64bit JSON integer value from array. More...
 
int json_array_get_float (jobj_t *jobj, uint16_t index, float *value)
 Get JSON float value from array. More...
 
int json_array_get_str (jobj_t *jobj, uint16_t index, char *value, int maxlen)
 Get JSON string value from array. More...
 
int json_array_get_str_len (jobj_t *jobj, uint16_t index, int *len)
 Get JSON string length from array. More...
 
int json_array_get_composite_object (jobj_t *jobj, uint16_t index)
 Get JSON composite object from array. More...
 
int json_array_release_composite_object (jobj_t *jobj)
 Release a JSON composite object from an array. More...
 
int json_array_get_array_object (jobj_t *jobj, uint16_t index, int *num_elements)
 Get JSON array object from array. More...
 
int json_array_release_array_object (jobj_t *jobj)
 Release a JSON array object from an array. More...
 

Detailed Description

JSON Parser module.

This module is used to parse a JSON string For JSON generation, please refer #json_generator.h

Usage

The JSON data can be viewed as a tree of key-value pairs. Composite objects indicate special nodes within the tree that anchor other subtrees. Arrays are special nodes that hold a collection of JSON elements.

The JSON parser module provides two sets of APIs, one to search inside a JSON object and the other to search inside a JSON array.

Search inside an Object

The APIs in this category are:

Search inside an array

The APIs in this category are:

A valid JSON string is an object and so the object search APIs should be used first.

The JSON data that looks like the following:

{ "name" : "JSON Parser",
"version" : 2.0,
"copyright" : 2014,
"supported_el" : ["bool","int","float","str","object","array"],
"features" : { "objects":true,
"arrays":"yes"
}
}

can be thought of as a tree with a root node that has 5 children "name", "version", "copyright", "supported_el" and "features". The node at "supported_el" is an array with 6 elements. The node at "features" further has 2 child nodes "objects", "arrays". The JSON can be much more complex with multiple levels of objects/arrays and with different types of elements inside an array or object.

This JSON parser is based on JSMN which treats the JSON string as a collection of tokens. A token can be an object, array, string or primitive. In the above, "name", "JSON Parser", "version", etc. are all strings. 2.0, 2014, true are primitives. The entire thing is a JSON object, as is the portion beginning from "{" after "features" :. The part between [] is an array. So, in all, the above JSON string consists of 21 tokens. This detail is given here so that the application developers have an idea about how large the JSON token array should be, for successful JSON parsing.

Using this JSON parser module you can parse the above elements with the following C code:

#define NUM_TOKENS 30
// Define an array of JSON tokens
jsontok_t json_tokens[NUM_TOKENS];
jobj_t jobj;
// Initialise the JSON parser and parse the given string
int err = json_parse_start(&jobj, json_str_ptr, strlen(json_str_ptr));
if (err != WM_SUCCESS)
return err;
// Get the value of "name" which is a string
json_get_val_str(&jobj, "name", sval, sizeof(sval));
// Get the value of "version" which is a float
json_get_val_float(&jobj, "version", &fval);
// Get the value of "copyright" which is an integer
json_get_val_int(&jobj, "copyright", &ival);
// Get the JSON array object "supported_el". After this function returns
// successfully, only the array search APIs can be used unless the array
// is released.
json_get_array_object(&jobj, "supported_el", &num_elements);
// Get the value at index 2 of the array which is a string value
json_array_get_str(&jobj, 2, sval, sizeof(sval));
// Release the array object so that the scope of searches returns back
// to the parent object.
// Get the JSON composite object "features". After this function returns
// successfully, the scope of the subsequent searches is constrained to
// within this object, unless the object is released.
json_get_composite_object(&jobj, "features");
// Get the value of "objects" which is a boolean
json_get_val_bool(&jobj, "objects", &bval);
// Release the composite object so that the scope of searches returns back
// to the parent object.
// Stop the JSON Parsing. This call is mandatory as it clears up the memory
// allocated internally by json_parse_start()

Macro Definition Documentation

◆ MOD_ERROR_START

#define MOD_ERROR_START (   x)    (x << 12 | 0)

◆ MOD_JSON

#define MOD_JSON   7

◆ VS_JSON_ERR_OK

#define VS_JSON_ERR_OK   0

Typedef Documentation

◆ jsmnenumtype_t

typedef jsmntype_t jsmnenumtype_t

◆ jsmnindex_t

typedef int jsmnindex_t

◆ json_parser_t

typedef jsmn_parser json_parser_t

◆ jsontok_t

typedef jsmntok_t jsontok_t

JSON token.

Application just needs to define an array of these tokens and pass it to json_init(). If the json_parse_start() API is used, this will not be required.

Enumeration Type Documentation

◆ wm_json_errno

Json Error Codes.

Enumerator
WM_E_JSON_ERRNO_BASE 
WM_E_JSON_FAIL 

Generic JSON parse failure.

WM_E_JSON_NOMEM 

Insufficient memory to hold the results.

WM_E_JSON_INVAL 

Invalid characters in JSON.

WM_E_JSON_INCOMPLETE 

Incomplete JSON string.

WM_E_JSON_INVALID_JOBJ 

Invalid JSON object.

WM_E_JSON_NOT_FOUND 

JSON element not found.

WM_E_JSON_INVALID_TYPE 

JSON element of invalid type.

WM_E_JSON_INVALID_INDEX 

Invalid array index given.

WM_E_JSON_INVALID_JARRAY 

Invalid search array provided.

WM_E_JSON_INVSTART 

Invalid json start.

WM_E_JSON_OBUF 

JSON buffer overflow.

Function Documentation

◆ json_array_get_array_object()

int json_array_get_array_object ( jobj_t jobj,
uint16_t  index,
int *  num_elements 
)

Get JSON array object from array.

Gets a JSON array object from another array based on the given index.

Parameters
[in,out]jobjCurrent JSON search object jobj_t. On success, the jobj will be modified such that the scope of subsequent searches will be limited to this composite object. Use json_array_release_composite_object() to expand the scope back to the parent array.
[in]indexIndex in the array (beginning from 0).
[out]num_elementsPointer to an integer assigned by the application. On sucess, this will hold the number of elements in the array found.
Returns
WM_SUCCESS on success
-WM_E_JSON_INVALID_INDEX if the index provided was invalid.
-WM_E_JSON_INVALID_TYPE if an element was found at the given index, but it was not an array.
-WM_E_JSON_INVALID_JARRAY if the search object was not a valid JSON array.
-WM_E_JSON_FAIL if any other error was encountered.

◆ json_array_get_bool()

int json_array_get_bool ( jobj_t jobj,
uint16_t  index,
bool *  value 
)

Get JSON bool value from array.

Gets the value of a JSON boolean element from an array based on the given index.

Parameters
[in]jobjCurrent JSON search object jobj_t.
[in]indexIndex in the array (beginning from 0).
[out]valuePointer to a boolean variable assigned by the application. This will hold the value on success.
Returns
WM_SUCCESS on success
-WM_E_JSON_INVALID_INDEX if the index provided was invalid.
-WM_E_JSON_INVALID_TYPE if an element was found at the given index, but it was not a boolean.
-WM_E_JSON_INVALID_JARRAY if the search object was not a valid JSON array.
-WM_E_JSON_FAIL if any other error was encountered.

◆ json_array_get_composite_object()

int json_array_get_composite_object ( jobj_t jobj,
uint16_t  index 
)

Get JSON composite object from array.

Gets a composite JSON object from an array based on the given index.

Parameters
[in,out]jobjCurrent JSON search object jobj_t. On success, the jobj will be modified such that the scope of subsequent searches will be limited to this composite object. Use json_array_release_composite_object() to expand the scope back to the parent array.
[in]indexIndex in the array (beginning from 0).
Returns
WM_SUCCESS on success
-WM_E_JSON_INVALID_INDEX if the index provided was invalid.
-WM_E_JSON_INVALID_TYPE if an element was found at the given index, but it was not an object.
-WM_E_JSON_INVALID_JARRAY if the search object was not a valid JSON array.
-WM_E_JSON_INVALID_JOBJ if the object found was not a valid JSON object.
-WM_E_JSON_FAIL if any other error was encountered.

◆ json_array_get_float()

int json_array_get_float ( jobj_t jobj,
uint16_t  index,
float *  value 
)

Get JSON float value from array.

Gets the value of a JSON float element from an array based on the given index.

Parameters
[in]jobjCurrent JSON search object jobj_t.
[in]indexIndex in the array (beginning from 0).
[out]valuePointer to a float variable assigned by the application. This will hold the value on success.
Returns
WM_SUCCESS on success
-WM_E_JSON_INVALID_INDEX if the index provided was invalid.
-WM_E_JSON_INVALID_TYPE if an element was found at the given index, but it was not a float.
-WM_E_JSON_INVALID_JARRAY if the search object was not a valid JSON array.
-WM_E_JSON_FAIL if any other error was encountered.

◆ json_array_get_int()

int json_array_get_int ( jobj_t jobj,
uint16_t  index,
int *  value 
)

Get JSON integer value from array.

Gets the value of a JSON integer element from an array based on the given index.

Parameters
[in]jobjCurrent JSON search object jobj_t.
[in]indexIndex in the array (beginning from 0).
[out]valuePointer to an integer variable assigned by the application. This will hold the value on success.
Returns
WM_SUCCESS on success
-WM_E_JSON_INVALID_INDEX if the index provided was invalid.
-WM_E_JSON_INVALID_TYPE if an element was found at the given index, but it was not an integer.
-WM_E_JSON_INVALID_JARRAY if the search object was not a valid JSON array.
-WM_E_JSON_FAIL if any other error was encountered.

◆ json_array_get_int64()

int json_array_get_int64 ( jobj_t jobj,
uint16_t  index,
int64_t *  value 
)

Get 64bit JSON integer value from array.

Gets the value of a JSON integer element from an array based on the given index.

Parameters
[in]jobjCurrent JSON search object jobj_t.
[in]indexIndex in the array (beginning from 0).
[out]valuePointer to a 64bit integer variable assigned by the application. This will hold the value on success.
Returns
WM_SUCCESS on success
-WM_E_JSON_INVALID_INDEX if the index provided was invalid.
-WM_E_JSON_INVALID_TYPE if an element was found at the given index, but it was not an integer.
-WM_E_JSON_INVALID_JARRAY if the search object was not a valid JSON array.
-WM_E_JSON_FAIL if any other error was encountered.

◆ json_array_get_num_elements()

int json_array_get_num_elements ( jobj_t jobj)

Get number of elements in an array.

This API is valid only if the current scope is an array

Parameters
[in]jobjThe current JSON object pointer.
Returns
Number of elements in the JSON Array
-WM_FAIL if the current scope is not an array

◆ json_array_get_str()

int json_array_get_str ( jobj_t jobj,
uint16_t  index,
char *  value,
int  maxlen 
)

Get JSON string value from array.

Gets the value of a JSON string element from an array based on the given index.

Parameters
[in]jobjCurrent JSON search object jobj_t.
[in]indexIndex in the array (beginning from 0).
[out]valuePointer to a buffer assigned by the application to hold the string. This will hold the null terminated string on success.
[in]maxlenLength of the buffer passed.
Returns
WM_SUCCESS on success
-WM_E_JSON_INVALID_INDEX if the index provided was invalid.
-WM_E_JSON_INVALID_TYPE if an element was found at the given index, but it was not a string.
-WM_E_JSON_INVALID_JARRAY if the search object was not a valid JSON array.
-WM_E_JSON_NOMEM if the buffer provided was insufficient.
-WM_E_JSON_FAIL if any other error was encountered.

◆ json_array_get_str_len()

int json_array_get_str_len ( jobj_t jobj,
uint16_t  index,
int *  len 
)

Get JSON string length from array.

Gets the length of a JSON string element from an array based on the given index. This is useful when the application does not know what size of buffer to use for json_array_get_str()

Parameters
[in]jobjCurrent JSON search object jobj_t.
[in]indexIndex in the array (beginning from 0).
[out]lenPointer to an integer assigned by the application to hold the length of string. Applications will have to use a buffer of size atleast 1 byte more than this length
Returns
WM_SUCCESS on success
-WM_E_JSON_INVALID_INDEX if the index provided was invalid.
-WM_E_JSON_INVALID_TYPE if an element was found at the given index, but it was not a string.
-WM_E_JSON_INVALID_JARRAY if the search object was not a valid JSON array.
-WM_E_JSON_FAIL if any other error was encountered.

◆ json_array_release_array_object()

int json_array_release_array_object ( jobj_t jobj)

Release a JSON array object from an array.

This function expands the scope of searches back to the parent array, if it was previously constrained using json_array_get_array_object().

Parameters
[in,out]jobjCurrent JSON search object jobj_t. On success, this will be modified such that the scope of future searches will be expanded back to the parent array.
Returns
WM_SUCCESS on success.
-WM_E_JSON_FAIL if any error was encountered.

◆ json_array_release_composite_object()

int json_array_release_composite_object ( jobj_t jobj)

Release a JSON composite object from an array.

This function expands the scope of searches back to the parent array, if it was previously constrained using json_array_get_composite_object().

Parameters
[in,out]jobjCurrent JSON search object jobj_t. On success, this will be modified such that the scope of future searches will be expanded back to the parent array.
Returns
WM_SUCCESS on success.
-WM_E_JSON_FAIL if any error was encountered.

◆ json_get_array_object()

int json_get_array_object ( jobj_t jobj,
char *  key,
int *  num_elements 
)

Get JSON array object.

Gets a JSON array object from another object based on the given key.

Parameters
[in,out]jobjCurrent JSON search object jobj_t. On success, the jobj will be modified such that the scope of subsequent searches will be limited to this array. Use json_release_array_object() to expand the scope back to the parent object.
[in]keyName of the JSON element.
[out]num_elementsPointer to an integer assigned by the application. On sucess, this will hold the number of elements in the array found.
Returns
WM_SUCCESS on success
-WM_E_JSON_NOT_FOUND if an element with the given key was not found.
-WM_E_JSON_INVALID_TYPE if an element with the given key was found but it was not an array.
-WM_E_JSON_INVALID_JOBJ if the search object was not a valid JSON object.
-WM_E_JSON_FAIL if any other error was encountered.

◆ json_get_composite_object()

int json_get_composite_object ( jobj_t jobj,
char *  key 
)

Get JSON composite object.

Gets a composite JSON object from another object based on the given key.

Parameters
[in,out]jobjCurrent JSON search object jobj_t. On success, the jobj will be modified such that the scope of subsequent searches will be limited to this composite object. Use json_release_composite_object() to expand the scope back to the parent object.
[in]keyName of the JSON element.
Returns
WM_SUCCESS on success
-WM_E_JSON_NOT_FOUND if an element with the given key was not found.
-WM_E_JSON_INVALID_TYPE if an element with the given key was found but it was not an object.
-WM_E_JSON_INVALID_JOBJ if the search object was not a valid JSON object or the object found was itself not a valid JSON object.
-WM_E_JSON_FAIL if any other error was encountered.

◆ json_get_val_bool()

int json_get_val_bool ( jobj_t jobj,
char *  key,
bool *  value 
)

Get JSON bool value.

Gets the value of a JSON boolean element from an object based on the given key.

Parameters
[in]jobjCurrent JSON search object jobj_t.
[in]keyName of the JSON element.
[out]valuePointer to a boolean variable assigned by the application. This will hold the value on success.
Returns
WM_SUCCESS on success
-WM_E_JSON_NOT_FOUND if an element with the given key was not found.
-WM_E_JSON_INVALID_TYPE if an element with the given key was found but it was not a boolean.
-WM_E_JSON_INVALID_JOBJ if the search object was not a valid JSON object.
-WM_E_JSON_FAIL if any other error was encountered.

◆ json_get_val_float()

int json_get_val_float ( jobj_t jobj,
char *  key,
float *  value 
)

Get JSON float value.

Gets the value of a JSON float element from an object based on the given key.

Parameters
[in]jobjCurrent JSON search object jobj_t.
[in]keyName of the JSON element.
[out]valuePointer to a float variable assigned by the application. This will hold the value on success.
Returns
WM_SUCCESS on success
-WM_E_JSON_NOT_FOUND if an element with the given key was not found.
-WM_E_JSON_INVALID_TYPE if an element with the given key was found but it was not a float.
-WM_E_JSON_INVALID_JOBJ if the search object was not a valid JSON object.
-WM_E_JSON_FAIL if any other error was encountered.

◆ json_get_val_int()

int json_get_val_int ( jobj_t jobj,
char *  key,
int *  value 
)

Get JSON integer value.

Gets the value of a JSON integer element from an object based on the given key.

Parameters
[in]jobjCurrent JSON search object jobj_t.
[in]keyName of the JSON element.
[out]valuePointer to an integer variable assigned by the application. This will hold the value on success.
Returns
WM_SUCCESS on success
-WM_E_JSON_NOT_FOUND if an element with the given key was not found.
-WM_E_JSON_INVALID_TYPE if an element with the given key was found but it was not an integer.
-WM_E_JSON_INVALID_JOBJ if the search object was not a valid JSON object.
-WM_E_JSON_FAIL if any other error was encountered.

◆ json_get_val_int64()

int json_get_val_int64 ( jobj_t jobj,
char *  key,
int64_t *  value 
)

Get 64bit JSON integer value.

Gets the value of a JSON integer element from an object based on the given key.

Parameters
[in]jobjCurrent JSON search object jobj_t.
[in]keyName of the JSON element.
[out]valuePointer to a 64bit integer variable assigned by the application. This will hold the value on success.
Returns
WM_SUCCESS on success
-WM_E_JSON_NOT_FOUND if an element with the given key was not found.
-WM_E_JSON_INVALID_TYPE if an element with the given key was found but it was not an integer.
-WM_E_JSON_INVALID_JOBJ if the search object was not a valid JSON object.
-WM_E_JSON_FAIL if any other error was encountered.

◆ json_get_val_str()

int json_get_val_str ( jobj_t jobj,
char *  key,
char *  value,
int  max_len 
)

Get JSON string value.

Gets the value of a JSON string element from an object based on the given key.

Parameters
[in]jobjCurrent JSON search object jobj_t.
[in]keyName of the JSON element.
[out]valuePointer to a buffer assigned by the application to hold the string. This will hold the null terminated string on success.
[in]max_lenLength of the buffer passed.
Returns
WM_SUCCESS on success
-WM_E_JSON_NOT_FOUND if an element with the given key was not found.
-WM_E_JSON_INVALID_TYPE if an element with the given key was found but it was not a string.
-WM_E_JSON_INVALID_JOBJ if the search object was not a valid JSON object.
-WM_E_JSON_NOMEM if the buffer provided was insufficient.
-WM_E_JSON_FAIL if any other error was encountered.

◆ json_get_val_str_len()

int json_get_val_str_len ( jobj_t jobj,
char *  key,
int *  len 
)

Get JSON string length.

Gets the length of a JSON string element from an object based on the given key. This is useful when the application does not know what size of buffer to use for json_get_val_str()

Parameters
[in]jobjCurrent JSON search object jobj_t.
[in]keyName of the JSON element.
[out]lenPointer to an integer assigned by the application to hold the length of string. Applications will have to use a buffer of size atleast 1 byte more than this length
Returns
WM_SUCCESS on success
-WM_E_JSON_NOT_FOUND if an element with the given key was not found.
-WM_E_JSON_INVALID_TYPE if an element with the given key was found but it was not a string.
-WM_E_JSON_INVALID_JOBJ if the search object was not a valid JSON object.
-WM_E_JSON_FAIL if any other error was encountered.

◆ json_init()

int json_init ( jobj_t jobj,
jsontok_t tokens,
int  num_tokens,
char *  js,
size_t  js_len 
)

Initialize the JSON Parser and parse the given JSON string.

This function initializes the JSON object that will be used for all the subsequent JSON parser APIs. It also parses the given JSON string and sets up the internal jobj for the subsequent APIs.

Note
Instead of json_init(), it is recommended to use json_parse_start() if the number of tokens that will be required for successful parsing are not known in advance.
Parameters
[in,out]jobjPointer to a JSON object jobj_t assigned by the application. This is set up for further use internally by this API.
[in]tokensPointer to an array of JSON tokens jsontok_t assigned by the application. To get an idea about how large the array should be, please refer #json_parser_usage section.
[in]num_tokensNumber of tokens in the tokens array.
[in]jsPointer to the JSON string.
[in]js_lenLength of the JSON string.
Returns
WM_SUCCESS on successful parsing. json_is_object() and/or json_is_array() can then be used to find out if the parsed string is a JSON object or array so that the appropriate parsing APIs can be used.
-WM_E_JSON_NOMEM if insufficient number of tokens are provided in json_init().
-WM_E_JSON_INVAL if there are invalid characters in the given JSON string.
-WM_E_JSON_INCOMPLETE if the given JSON string is incomplete.
-WM_E_JSON_INVALID_JOBJ if the given json string is an invalid JSON object.
-WM_E_JSON_INVALID_JARRAY if the given json string is an invalid JSON array
-WM_E_JSON_FAIL if any other error was encountered.

◆ json_is_array()

bool json_is_array ( jobj_t jobj)

Find out if the current object is a JSON Array.

Parameters
[in]jobjThe current JSON object pointer of type jobj_t
Returns
true if it is an array
false if it is not an array

◆ json_is_object()

bool json_is_object ( jobj_t jobj)

Find out if the current object is a JSON Object.

Parameters
[in]jobjThe current JSON object pointer of type jobj_t
Returns
true if it is an object
false if it is not an object

◆ json_parse_start()

int json_parse_start ( jobj_t jobj,
char *  js,
size_t  js_len 
)

Start JSON Parsing.

This API is exactly the same as json_init() with the only difference that it allocates tokens internally as per the actual requirement.

Note
You need to use only one of the 2 APIs json_init() OR json_parse_start(), not both.
After all the parsing has been done (i.e. all required elements fetched), it is mandatory to call json_parse_stop() if this API is used.
Parameters
[in,out]jobjPointer to a JSON object jobj_t assigned by the application. This is set up for further use internally by this API.
[in]jsPointer to the JSON string.
[in]js_lenLength of the JSON string.
Returns
WM_SUCCESS on successful parsing. The jobj_t structure can be used to find out the total number of parsed tokens if required. json_is_object() and/or json_is_array() can then be used to find out if the parsed string is a JSON object or array so that the appropriate parsing APIs can be used.
-WM_E_JSON_NOMEM if tokens could not be allocated internally
-WM_E_JSON_INVAL if there are invalid characters in the given JSON string.
-WM_E_JSON_INCOMPLETE if the given JSON string is incomplete.
-WM_E_JSON_INVALID_JOBJ if the given json string is an invalid JSON object.
-WM_E_JSON_INVALID_JARRAY if the given json string is an invalid JSON array
-WM_E_JSON_FAIL if any other error was encountered.

◆ json_parse_stop()

void json_parse_stop ( jobj_t jobj)

Stop JSON Parsing.

This API must be called if json_parse_start() has been used to parse a JSON string.

Parameters
[in]jobjPointer to the same JSON object that was passed to json_parse_start()

◆ json_release_array_object()

int json_release_array_object ( jobj_t jobj)

Release a JSON array object.

This function expands the scope of searches back to the parent object, if it was previously constrained using json_get_array_object().

Parameters
[in,out]jobjCurrent JSON search object jobj_t. On success, this will be modified such that the scope of future searches will be expanded back to the parent object.
Returns
WM_SUCCESS on success.
-WM_E_JSON_FAIL if any error was encountered.

◆ json_release_composite_object()

int json_release_composite_object ( jobj_t jobj)

Release a JSON composite object.

This function expands the scope of searches back to the parent object, if it was previously constrained using json_get_composite_object().

Parameters
[in,out]jobjCurrent JSON search object jobj_t. On success, this will be modified such that the scope of future searches will be expanded back to the parent object.
Returns
WM_SUCCESS on success.
-WM_E_JSON_FAIL if any error was encountered.
json_parse_stop
void json_parse_stop(jobj_t *jobj)
Stop JSON Parsing.
json_get_val_bool
int json_get_val_bool(jobj_t *jobj, char *key, bool *value)
Get JSON bool value.
jobj_t
Object used by the JSON parser internally.
Definition: json_parser.h:193
jsontok_t
jsmntok_t jsontok_t
JSON token.
Definition: json_parser.h:179
json_array_get_str
int json_array_get_str(jobj_t *jobj, uint16_t index, char *value, int maxlen)
Get JSON string value from array.
json_parse_start
int json_parse_start(jobj_t *jobj, char *js, size_t js_len)
Start JSON Parsing.
json_get_val_str
int json_get_val_str(jobj_t *jobj, char *key, char *value, int max_len)
Get JSON string value.
json_get_val_int
int json_get_val_int(jobj_t *jobj, char *key, int *value)
Get JSON integer value.
json_release_composite_object
int json_release_composite_object(jobj_t *jobj)
Release a JSON composite object.
json_get_composite_object
int json_get_composite_object(jobj_t *jobj, char *key)
Get JSON composite object.
json_get_val_float
int json_get_val_float(jobj_t *jobj, char *key, float *value)
Get JSON float value.
json_release_array_object
int json_release_array_object(jobj_t *jobj)
Release a JSON array object.
json_get_array_object
int json_get_array_object(jobj_t *jobj, char *key, int *num_elements)
Get JSON array object.