MegaWiFi 1.5
MegaWiFi API documentation
json

JSON implementation, based on jsmn by Serge Zaitsev. More...

Macros

#define JSMN_STATIC
 Hides jsmn API definitions to avoid linking errors. More...
 
#define json_item(json_str, obj_tok, idx)
 

Functions

int json_null_terminate (char *json_str, const jsmntok_t *obj_tok, int num_tok)
 Add null-terminators to the input JSON string. Useful to further extract token values as null-terminated strings. More...
 
bool json_is_key (const jsmntok_t *json_tok, int obj_idx, int num_tok)
 Determines if pointed token corresponds to a key in a key:value pair. More...
 
int json_object_next (const jsmntok_t *obj_tok, int obj_idx, int parent_idx, int num_tok)
 Return the index of the next item. This function only works properly when specified token is of JSMN_OBJECT type. More...
 
int json_key_next (const jsmntok_t *json_tok, int obj_idx, int parent_idx, int num_tok)
 Return the index of the next key in the current JSON level. More...
 
int json_key_search (const char *key, const char *json_str, const jsmntok_t *json_tok, int obj_idx, int parent_idx, int num_tok)
 Search for a given key in the current JSON level. More...
 
int json_bool_get (const char *json_str, const jsmntok_t *json_tok, int obj_idx)
 Obtains the boolean value corresponding to the indicated token. More...
 

Detailed Description

JSON implementation, based on jsmn by Serge Zaitsev.

This module just adds some helper functions to jsmn to ease navigating the JSON structure and extracting values.

Author
Jesus Alonso (doragasu)
Date
2019
Todo:
Add more functions to extract data.

Macro Definition Documentation

◆ JSMN_STATIC

#define JSMN_STATIC

Hides jsmn API definitions to avoid linking errors.

Definition at line 26 of file json.h.

◆ json_item

#define json_item (   json_str,
  obj_tok,
  idx 
)
Value:
(idx) < 0 ? NULL : \
&((json_str)[(obj_tok)[idx].start])
#define NULL
NULL Pointer.
Definition: util.h:29

Macro to extract the item (as string) from a token. Requires the json_str to have the null terminations added by json_null_terminate().

Definition at line 31 of file json.h.

Function Documentation

◆ json_bool_get()

int json_bool_get ( const char *  json_str,
const jsmntok_t json_tok,
int  obj_idx 
)

Obtains the boolean value corresponding to the indicated token.

Parameters
[in]json_strJSON string.
[in]json_tokJSON token array extracted from json_str.
[in]obj_idxIndex of the token to analyse.
Returns
1 if true, 0 if false, less than 0 if token is not a proper boolean.

◆ json_is_key()

bool json_is_key ( const jsmntok_t json_tok,
int  obj_idx,
int  num_tok 
)

Determines if pointed token corresponds to a key in a key:value pair.

Parameters
[in]json_tokJSON token array.
[in]obj_idxIndex of the token to analyse.
[in]num_tokNumber of tokens in json_tok.
Returns
true if token is a key token. false otherwise.

◆ json_key_next()

int json_key_next ( const jsmntok_t json_tok,
int  obj_idx,
int  parent_idx,
int  num_tok 
)

Return the index of the next key in the current JSON level.

Parameters
[in]json_tokJSON token array.
[in]obj_idxIndex of the token to analyse.
[in]parent_idxIndex of the parent object or array token.
[in]num_tokNumber of tokens in obj_tok.
Returns
The index of the next key item on success, of JSMN_ERROR_PART if there are no more key items available in the parent object.

◆ json_key_search()

int json_key_search ( const char *  key,
const char *  json_str,
const jsmntok_t json_tok,
int  obj_idx,
int  parent_idx,
int  num_tok 
)

Search for a given key in the current JSON level.

Parameters
[in]keykey string to search.
[in]json_strJSON string.
[in]json_tokJSON token array.
[in]obj_idxIndex of the token to analyse.
[in]parent_idxIndex of the parent object or array token.
[in]num_tokNumber of tokens in obj_tok.
Returns
The index of the value token on success, or JSMN_ERROR_PART if the specified key was not found in parent object.
Note
The returned value on success is the index of the value token, it is NOT the index of the found key token.

◆ json_null_terminate()

int json_null_terminate ( char *  json_str,
const jsmntok_t obj_tok,
int  num_tok 
)

Add null-terminators to the input JSON string. Useful to further extract token values as null-terminated strings.

Parameters
[in,out]json_strThe JSON string to add null terminators to.
[in]obj_tokTokens extracted from json_str.
[in]num_tokNumber of tokens in obj_tok.
Returns
0 on success, JSMN_ERROR_INVAL if string ends prematurely.
Warning
Resulting string can no longer be used as a valid json.

◆ json_object_next()

int json_object_next ( const jsmntok_t obj_tok,
int  obj_idx,
int  parent_idx,
int  num_tok 
)

Return the index of the next item. This function only works properly when specified token is of JSMN_OBJECT type.

Parameters
[in]obj_tokJSON token array.
[in]obj_idxIndex of the token to analyse.
[in]parent_idxIndex of the parent object or array token.
[in]num_tokNumber of tokens in obj_tok.
Returns
The index of the next item on success, of JSMN_ERROR_PART if there are no more items available in the parent object.