Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependents: ATT_WNCInterface_Info WNCInterface_HTTP_example NerfUS-Coord Mbed_Prototype_copy_4_INNO_day_15_6_2017 ... more
Json Class Reference
C++ JSON wrapper over JSMN lib (https://github.com/zserge/jsmn). More...
#include <Json.h>
Public Member Functions | |
Json (const char *jsonString, size_t length, unsigned int maxTokens=32) | |
The only constructor allowed. | |
virtual | ~Json () |
Although there is no virtual function to this class, destructor is still made virtual, for just-in-case use. | |
int | findKeyIndex (const char *key, const int &startingAt=0) const |
findKeyIndex will find and return the token index representing the 'Key' in underlying JSON object. | |
int | findKeyIndexIn (const char *key, const int &parentIndex=0) const |
findKeyIndexIn will find and return the token index representing the 'Key' in underlying JSON object node. | |
int | findChildIndexOf (const int &parentIndex, const int &startingAt=0) const |
findChildIndexOf will find and return the token index representing first child a JSON node represented by parentIndex (that is either a Key, an Object, or an Array), and exists after the startingAt value. | |
bool | matches (const int &tokenIndex, const char *value) const |
matches will tell if the token data (either key or value) matches with the value provided. | |
int | parsedTokenCount () const |
parsedTokenCount will tell how many tokens have been parsed by JSMN parser. | |
bool | isValidJson () const |
isValidJson will tell the caller if the parsed JSON was valid, parsed, and accepted to further work on. | |
bool | isValidToken (const int tokenIndex) const |
isValidToken will tell the caller if the tokenIndex is in valid range. | |
jsmntype_t | type (const int tokenIndex) const |
type will return the JSMN type represented by the tokenIndex. | |
int | parent (const int tokenIndex) const |
parent is a utility function to get the parent index of the tokenIndex passed. | |
int | childCount (const int tokenIndex) const |
childCount returns the number of children sharing the same parent. | |
int | tokenLength (const int tokenIndex) const |
tokenLength returns the number of characters a node takes up in JSON source string. | |
const char * | tokenAddress (const int tokenIndex) const |
tokenAddress returns the pointer that marks as the start of token in JSON source string. | |
int | tokenIntegerValue (const int tokenIndex, int &returnValue) const |
tokenInterValue will convert the value as int represented by the tokenIndex. | |
int | tokenNumberValue (const int tokenIndex, float &returnValue) const |
tokenNumberValue will convert the value as float represented by the tokenIndex. | |
int | tokenBooleanValue (const int tokenIndex, bool &returnValue) const |
tokenBooleanValue will convert the value as bool represented by the tokenIndex. | |
Static Public Member Functions | |
static char * | unescape (char *jsonString) |
unescape is a utility function to unescape a JSON string. |
Detailed Description
C++ JSON wrapper over JSMN lib (https://github.com/zserge/jsmn).
This C++ Class is a set of common tools/procedures as a C++ wrapper over JSMN JSON parser library. It is intended to provide the boiler-plate code, with intentions to reduce code clutter, in more of C++ fashion.
In contrast to original library, Json is intended to work strictly with valid JSON structures. Non-standard JSON structures should result in an error.
This class works explicitly on the indices returned by underlying JSMN library. In the scope of this class, its function parameters, return types, and documentation, the term 'index' will always mean the index of JSMN tokens, parsed by the Json constructor, unless and until explicitly mentioned otherwise.
Definition at line 147 of file Json.h.
Constructor & Destructor Documentation
Json | ( | const char * | jsonString, |
size_t | length, | ||
unsigned int | maxTokens = 32 |
||
) |
The only constructor allowed.
As JSON object will create/allocate memory for its working, in favor of small memory footprints, it is not allowed to be passed-by-value. So there is no copy- or default-constructor
- Parameters:
-
jsonString char string containing JSON data length length of the jsonString maxTokens optional maximum count of Tokens. Default is 32.
~Json | ( | ) | [virtual] |
Member Function Documentation
int childCount | ( | const int | tokenIndex ) | const |
childCount returns the number of children sharing the same parent.
This utility function is handy for iterating over Arrays or Objects.
- Parameters:
-
tokenIndex representing the token in the JSON source. The valid value range is 0 to [parsedTokenCount()-1] both inclusive.
- Returns:
- non-negative integer representing the number of children tokenIndex node has. 0 is a valid number, in case the node has no child nodes. -1 will be returned if the tokenIndex is not valid.
int findChildIndexOf | ( | const int & | parentIndex, |
const int & | startingAt = 0 |
||
) | const |
findChildIndexOf will find and return the token index representing first child a JSON node represented by parentIndex (that is either a Key, an Object, or an Array), and exists after the startingAt value.
This function is particularly handy in iterating over Array Objects, or getting the index for JSON 'Value' of a JSON 'Key'.
- Parameters:
-
parentIndex token index representing the parent node in JSON source. The valid value range is 0 to [parsedTokenCount()-1] both inclusive. startingAt describes the starting index of the nodes to search. In other words, if caller wants to skip some nodes, they can provide this value. Default value is 0, which means search for all nodes in the parent.
- Returns:
- a non-zero positive integer, if the child node is found in source JSON. If no child is found, -1 will be returned. There should be no '0' value returned in any valid case.
int findKeyIndex | ( | const char * | key, |
const int & | startingAt = 0 |
||
) | const |
findKeyIndex will find and return the token index representing the 'Key' in underlying JSON object.
It is a strictly a linear key search and will return the first occurrence, without the JSON node structure semantics. For search in a specific node, refer to findKeyIndex
- Parameters:
-
key a char string to find as a 'Key' in JSON structure. startingAt the starting token-index for 'key' search. The search will NOT include this index, but instead will use the next one as the starting point. In case, a negative value is passed, search will start from '0'. It's caller's responsibility to make sure what values they're passing. Default value is set to '0', as the zero-th token index in any valid JSON object should always be starting object brace '{'. So default behavior is to always find the very first occurrence of key as represented by 'key' parameter.
- Returns:
- a non-zero positive integer, if a key is found in the source JSON. If no key is found, -1 will be returned. There should be no '0' value returned in any valid case.
int findKeyIndexIn | ( | const char * | key, |
const int & | parentIndex = 0 |
||
) | const |
findKeyIndexIn will find and return the token index representing the 'Key' in underlying JSON object node.
It is strictly a single-level key search function, and will NOT look for the key in any child JSON nodes (JSON Object/Array).
- Parameters:
-
key a char string to find as a 'Key' in JSON structure. parentIndex the starting token-index for 'key' search. The search will look for the key, only under the JSON node represented by this parentIndex. Default value is '0', making the default behavior to look for only root-level keys. The valid value range is 0 to [parsedTokenCount()-1] both inclusive.
- Returns:
- a non-zero positive integer, if a key is found in the source JSON. If no key is found, -1 will be returned. There should be no '0' value returned in any valid case.
bool isValidJson | ( | ) | const |
bool isValidToken | ( | const int | tokenIndex ) | const |
isValidToken will tell the caller if the tokenIndex is in valid range.
The valid value range is 0 to [parsedTokenCount()-1] both inclusive.
- Parameters:
-
tokenIndex representing the token in the JSON source
- Returns:
- true if the JSON is valid, false otherwise.
bool matches | ( | const int & | tokenIndex, |
const char * | value | ||
) | const |
matches will tell if the token data (either key or value) matches with the value provided.
This function is particularly handy in iterating over the keys, and finding a specific key, in an object. The comparison is case-sensitive. i.e. 'Apple' will NOT match with 'apple'.
- Parameters:
-
tokenIndex representing the token to compare. The valid value range is 0 to [parsedTokenCount()-1] both inclusive. value to compare the token data with.
- Returns:
- true if the token data matches with value. false will be returned either the value doesn't match OR the tokenIndex is not valid.
int parent | ( | const int | tokenIndex ) | const |
parent is a utility function to get the parent index of the tokenIndex passed.
- Parameters:
-
tokenIndex representing the token in the JSON source. The valid value range is 0 to [parsedTokenCount()-1] both inclusive.
- Returns:
- the parentIndex if the node has a parent, and tokenIndex is a valid index. In case of no parent, or invalid tokenIndex, -1 is returned.
int parsedTokenCount | ( | ) | const |
parsedTokenCount will tell how many tokens have been parsed by JSMN parser.
It is a utility function, for token validity.
- Returns:
- non-negative integer number of tokens parsed by JSMN library. Negative value may be returned in case of error, but this behavior is not tested, yet.
const char * tokenAddress | ( | const int | tokenIndex ) | const |
tokenAddress returns the pointer that marks as the start of token in JSON source string.
This is a utility function for character/string manipulation by the caller.
- Parameters:
-
tokenIndex representing the token in the JSON source. The valid value range is 0 to [parsedTokenCount()-1] both inclusive.
- Returns:
- a non-NULL pointer will be returned if tokenIndex is valid, -1 otherwise.
int tokenBooleanValue | ( | const int | tokenIndex, |
bool & | returnValue | ||
) | const |
tokenBooleanValue will convert the value as bool represented by the tokenIndex.
A typical use is that caller has found the Key-index, and then has retrieved the Value-index (by using findChildIndexOf function), and now they want to read the value of Value-index, as boolean value.
- Parameters:
-
tokenIndex representing the "value" in the JSON source. The valid value range is 0 to [parsedTokenCount()-1] both inclusive. returnValue is a return-parameter passed by reference to hold up the bool value parsed by this function.
- Returns:
- 0 if the operation is successful. -1 if tokenIndex is invalid.
int tokenIntegerValue | ( | const int | tokenIndex, |
int & | returnValue | ||
) | const |
tokenInterValue will convert the value as int represented by the tokenIndex.
A typical use is that caller has found the Key-index, and then has retrieved the Value-index (by using findChildIndexOf function) , and now they want to read the value of Value-index, as integer value.
- Parameters:
-
tokenIndex representing the "value" in the JSON source. The valid value range is 0 to [parsedTokenCount()-1] both inclusive. returnValue is a return-parameter passed by reference to hold up the integer value parsed by this function. If the converted value would be out of the range of representable values by an int, it causes undefined behavior. It is caller's responsibility to check for these cases.
- Returns:
- 0 if the operation is successful. -1 if tokenIndex is invalid.
int tokenLength | ( | const int | tokenIndex ) | const |
tokenLength returns the number of characters a node takes up in JSON source string.
- Parameters:
-
tokenIndex representing the token in the JSON source. The valid value range is 0 to [parsedTokenCount()-1] both inclusive.
- Returns:
- positive integer value representing the length of the token sub-string in the source JSON. The 0 value is an invalid state and should never occur. -1 will be returned in case of invalid tokenIndex.
int tokenNumberValue | ( | const int | tokenIndex, |
float & | returnValue | ||
) | const |
tokenNumberValue will convert the value as float represented by the tokenIndex.
A typical use is that caller has found the Key-index, and then has retrieved the Value-index (by using findChildIndexOf function) , and now they want to read the value of Value-index, as floating-point value.
- Parameters:
-
tokenIndex representing the "value" in the JSON source. The valid value range is 0 to [parsedTokenCount()-1] both inclusive. returnValue is a return-parameter passed by reference to hold up the floating-point value parsed by this function. If the converted value would be out of the range of representable values by a float, it causes undefined behavior. It is caller's responsibility to check for these cases.
- Returns:
- 0 if the operation is successful. -1 if tokenIndex is invalid.
jsmntype_t type | ( | const int | tokenIndex ) | const |
type will return the JSMN type represented by the tokenIndex.
- Parameters:
-
tokenIndex representing the token in the JSON source. The valid value range is 0 to [parsedTokenCount()-1] both inclusive.
- Returns:
- the type represented by tokenIndex. In case of invalid tokenIndex, JSMN_UNDEFINED is returned.
char * unescape | ( | char * | jsonString ) | [static] |
unescape is a utility function to unescape a JSON string.
This function does not change any state of Json object, and is a pure static utility function. This function is in-pace unescaping, and WILL modify the source parameter.
- Parameters:
-
jsonString representing an escaped JSON string. This parameter is also the return parameter as well. All modifications will be reflected in this parameter.
- Returns:
- pointer to unescaped JSON string. This is exactly the same pointer as jsonString parameter.
Generated on Tue Jul 12 2022 19:29:14 by
