Virgil IoT KIT
|
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... | |
JSON Parser module.
This module is used to parse a JSON string For JSON generation, please refer #json_generator.h
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.
The APIs in this category are:
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:
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 MOD_ERROR_START | ( | x | ) | (x << 12 | 0) |
#define MOD_JSON 7 |
#define VS_JSON_ERR_OK 0 |
typedef jsmntype_t jsmnenumtype_t |
typedef int jsmnindex_t |
typedef jsmn_parser json_parser_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.
enum wm_json_errno |
Json Error Codes.
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.
[in,out] | jobj | Current 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] | index | Index in the array (beginning from 0). |
[out] | num_elements | Pointer to an integer assigned by the application. On sucess, this will hold the number of elements in the array found. |
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.
[in] | jobj | Current JSON search object jobj_t. |
[in] | index | Index in the array (beginning from 0). |
[out] | value | Pointer to a boolean variable assigned by the application. This will hold the value on success. |
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.
[in,out] | jobj | Current 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] | index | Index in the array (beginning from 0). |
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.
[in] | jobj | Current JSON search object jobj_t. |
[in] | index | Index in the array (beginning from 0). |
[out] | value | Pointer to a float variable assigned by the application. This will hold the value on success. |
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.
[in] | jobj | Current JSON search object jobj_t. |
[in] | index | Index in the array (beginning from 0). |
[out] | value | Pointer to an integer variable assigned by the application. This will hold the value on success. |
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.
[in] | jobj | Current JSON search object jobj_t. |
[in] | index | Index in the array (beginning from 0). |
[out] | value | Pointer to a 64bit integer variable assigned by the application. This will hold the value on success. |
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
[in] | jobj | The current JSON object pointer. |
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.
[in] | jobj | Current JSON search object jobj_t. |
[in] | index | Index in the array (beginning from 0). |
[out] | value | Pointer to a buffer assigned by the application to hold the string. This will hold the null terminated string on success. |
[in] | maxlen | Length of the buffer passed. |
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()
[in] | jobj | Current JSON search object jobj_t. |
[in] | index | Index in the array (beginning from 0). |
[out] | len | Pointer 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 |
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().
[in,out] | jobj | Current 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. |
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().
[in,out] | jobj | Current 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. |
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.
[in,out] | jobj | Current 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] | key | Name of the JSON element. |
[out] | num_elements | Pointer to an integer assigned by the application. On sucess, this will hold the number of elements in the array found. |
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.
[in,out] | jobj | Current 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] | key | Name of the JSON element. |
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.
[in] | jobj | Current JSON search object jobj_t. |
[in] | key | Name of the JSON element. |
[out] | value | Pointer to a boolean variable assigned by the application. This will hold the value on success. |
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.
[in] | jobj | Current JSON search object jobj_t. |
[in] | key | Name of the JSON element. |
[out] | value | Pointer to a float variable assigned by the application. This will hold the value on success. |
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.
[in] | jobj | Current JSON search object jobj_t. |
[in] | key | Name of the JSON element. |
[out] | value | Pointer to an integer variable assigned by the application. This will hold the value on success. |
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.
[in] | jobj | Current JSON search object jobj_t. |
[in] | key | Name of the JSON element. |
[out] | value | Pointer to a 64bit integer variable assigned by the application. This will hold the value on success. |
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.
[in] | jobj | Current JSON search object jobj_t. |
[in] | key | Name of the JSON element. |
[out] | value | Pointer to a buffer assigned by the application to hold the string. This will hold the null terminated string on success. |
[in] | max_len | Length of the buffer passed. |
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()
[in] | jobj | Current JSON search object jobj_t. |
[in] | key | Name of the JSON element. |
[out] | len | Pointer 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 |
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.
[in,out] | jobj | Pointer to a JSON object jobj_t assigned by the application. This is set up for further use internally by this API. |
[in] | tokens | Pointer 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_tokens | Number of tokens in the tokens array. |
[in] | js | Pointer to the JSON string. |
[in] | js_len | Length of the JSON string. |
bool json_is_array | ( | jobj_t * | jobj | ) |
Find out if the current object is a JSON Array.
[in] | jobj | The current JSON object pointer of type jobj_t |
bool json_is_object | ( | jobj_t * | jobj | ) |
Find out if the current object is a JSON Object.
[in] | jobj | The current JSON object pointer of type jobj_t |
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.
[in,out] | jobj | Pointer to a JSON object jobj_t assigned by the application. This is set up for further use internally by this API. |
[in] | js | Pointer to the JSON string. |
[in] | js_len | Length of the JSON string. |
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.
[in] | jobj | Pointer to the same JSON object that was passed to json_parse_start() |
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().
[in,out] | jobj | Current 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. |
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().
[in,out] | jobj | Current 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. |