JsonParserGeneratorRK
|
API to the JsonParser. More...
#include <JsonParserGeneratorRK.h>
Public Member Functions | |
JsonParser () | |
Construct a parser object. More... | |
virtual | ~JsonParser () |
Destroy a parser object. More... | |
JsonParser (char *buffer, size_t bufferLen, JsonParserGeneratorRK::jsmntok_t *tokens, size_t maxTokens) | |
Static buffers constructor. | |
bool | allocateTokens (size_t maxTokens) |
Preallocates a specific number of tokens. More... | |
bool | parse () |
Parses the data you have added using addData() or addString(). More... | |
JsonReference | getReference () const |
Get a JsonReference object. This is used for fluent-style access to the data. | |
const JsonParserGeneratorRK::jsmntok_t * | getOuterObject () const |
Gets the outer JSON object token. More... | |
const JsonParserGeneratorRK::jsmntok_t * | getOuterArray () const |
Gets the outer JSON array token. More... | |
const JsonParserGeneratorRK::jsmntok_t * | getOuterToken () const |
Gets the outer JSON object or array token. More... | |
size_t | getArraySize (const JsonParserGeneratorRK::jsmntok_t *arrayContainer) const |
Given a token for an JSON array in arrayContainer, gets the number of elements in the array. More... | |
template<class T > | |
bool | getValueByKey (const JsonParserGeneratorRK::jsmntok_t *container, const char *name, T &result) const |
Given an object token in container, gets the value with the specified key name. More... | |
template<class T > | |
bool | getOuterValueByKey (const char *name, T &result) const |
Gets the value with the specified key name out of the outer object. More... | |
template<class T > | |
bool | getKeyValueByIndex (const JsonParserGeneratorRK::jsmntok_t *container, size_t index, String &key, T &result) const |
Gets the key/value pair of an object by index. More... | |
template<class T > | |
bool | getOuterKeyValueByIndex (size_t index, String &key, T &result) const |
Gets the key/value pair of the outer object by index (0 = first, 1 = second, ...) More... | |
template<class T > | |
bool | getValueByIndex (const JsonParserGeneratorRK::jsmntok_t *arrayContainer, size_t index, T &result) const |
Given an array token in arrayContainer, gets the value with the specified index. More... | |
template<class T > | |
bool | getValueByColRow (const JsonParserGeneratorRK::jsmntok_t *arrayContainer, size_t col, size_t row, T &result) const |
This method is used to extract data from a 2-dimensional JSON array, an array of arrays of values. More... | |
bool | getValueTokenByKey (const JsonParserGeneratorRK::jsmntok_t *container, const char *key, const JsonParserGeneratorRK::jsmntok_t *&value) const |
Given an object token in container, gets the token value with the specified key name. More... | |
bool | getValueTokenByIndex (const JsonParserGeneratorRK::jsmntok_t *container, size_t desiredIndex, const JsonParserGeneratorRK::jsmntok_t *&value) const |
Given an array token in container, gets the token value with the specified index. More... | |
bool | getValueTokenByColRow (const JsonParserGeneratorRK::jsmntok_t *container, size_t col, size_t row, const JsonParserGeneratorRK::jsmntok_t *&value) const |
This method is used to extract data from a 2-dimensional JSON array, an array of arrays of values. More... | |
const JsonParserGeneratorRK::jsmntok_t * | getTokenByIndex (const JsonParserGeneratorRK::jsmntok_t *container, size_t desiredIndex) const |
Given a containing object, finds the nth token in the object. Internal use only. More... | |
bool | getKeyValueTokenByIndex (const JsonParserGeneratorRK::jsmntok_t *container, const JsonParserGeneratorRK::jsmntok_t *&key, const JsonParserGeneratorRK::jsmntok_t *&value, size_t index) const |
Given a JSON object in container, gets the key/value pair specified by index. Internal use only. More... | |
bool | skipObject (const JsonParserGeneratorRK::jsmntok_t *container, const JsonParserGeneratorRK::jsmntok_t *&obj) const |
Used internally to skip over the token in obj. More... | |
void | copyTokenValue (const JsonParserGeneratorRK::jsmntok_t *token, char *dst, size_t dstLen) const |
Copies the value of the token into a buffer, making it a null-terminated cstring. More... | |
bool | getTokenValue (const JsonParserGeneratorRK::jsmntok_t *token, bool &result) const |
Gets a bool (boolean) value. More... | |
bool | getTokenValue (const JsonParserGeneratorRK::jsmntok_t *token, int &result) const |
Gets an integer value. More... | |
bool | getTokenValue (const JsonParserGeneratorRK::jsmntok_t *token, unsigned long &result) const |
Gets an unsigned long value. More... | |
bool | getTokenValue (const JsonParserGeneratorRK::jsmntok_t *token, float &result) const |
Gets a float (single precision floating point) value. More... | |
bool | getTokenValue (const JsonParserGeneratorRK::jsmntok_t *token, double &result) const |
Gets a double (double precision floating point) value. More... | |
bool | getTokenValue (const JsonParserGeneratorRK::jsmntok_t *token, String &result) const |
Gets a String value into a Wiring String object. More... | |
bool | getTokenValue (const JsonParserGeneratorRK::jsmntok_t *token, char *str, size_t &strLen) const |
Gets a string as a c-string into the specified buffer. More... | |
bool | getTokenValue (const JsonParserGeneratorRK::jsmntok_t *token, JsonParserString &str) const |
Gets a string as a JsonParserString object. More... | |
bool | getTokenJsonString (const JsonParserGeneratorRK::jsmntok_t *token, String &result) const |
Converts a token (object, array, string, or primitive) back into JSON in a Wiring String. More... | |
bool | getTokenJsonString (const JsonParserGeneratorRK::jsmntok_t *token, char *str, size_t &strLen) const |
Converts a token (object, array, string, or primitive) back into JSON in a buffer. More... | |
bool | getTokenJsonString (const JsonParserGeneratorRK::jsmntok_t *token, JsonParserString &str) const |
Gets a token as a JSON string. More... | |
JsonParserGeneratorRK::jsmntok_t * | getTokens () |
Used internally in the test suite for printing the token list. | |
JsonParserGeneratorRK::jsmntok_t * | getTokensEnd () |
Used internally in the test suite for printing the token list. | |
size_t | getMaxTokens () const |
Used internally in the test suite for printing the token list. | |
Public Member Functions inherited from JsonBuffer | |
JsonBuffer () | |
Construct a JsonBuffer object with no external buffer specified. | |
virtual | ~JsonBuffer () |
Destructor. Destroying the object does not delete any underlying buffer! | |
JsonBuffer (char *buffer, size_t bufferLen) | |
Construct a JsonBuffer with an external buffer of a given size. More... | |
void | setBuffer (char *buffer, size_t bufferLen) |
Sets the buffers to the specified buffer and length. More... | |
bool | allocate (size_t len) |
Allocate the buffer using malloc/realloc. More... | |
bool | addString (const char *data) |
Add a c-string to the end of the buffer. More... | |
bool | addData (const char *data, size_t dataLen) |
Add a string to the end of the buffer. More... | |
char * | getBuffer () const |
Gets a pointer to the internal buffer. More... | |
size_t | getOffset () const |
Gets the current offset for writing. | |
void | setOffset (size_t offset) |
swets the current offset for writing | |
size_t | getBufferLen () const |
Gets the current length of the buffer. More... | |
void | clear () |
Clears the current buffer for writing. More... | |
Static Public Member Functions | |
static void | appendUtf8 (uint16_t unicode, JsonParserString &str) |
Given a Unicode UTF-16 code point, converts it to UTF-8 and appends it to str. | |
Protected Attributes | |
JsonParserGeneratorRK::jsmntok_t * | tokens |
Array of tokens after parsing. | |
JsonParserGeneratorRK::jsmntok_t * | tokensEnd |
Pointer into tokens, points after last used token. | |
size_t | maxTokens |
Number of tokens that can be stored in tokens. | |
JsonParserGeneratorRK::jsmn_parser | parser |
The JSMN parser object. | |
Protected Attributes inherited from JsonBuffer | |
char * | buffer |
The buffer to to read from or write to. This is not null-terminated. | |
size_t | bufferLen |
The length of the buffer in bytes,. | |
size_t | offset |
The read or write offset. | |
bool | staticBuffers |
True if the buffers were passed in and should not freed or reallocated. | |
Friends | |
class | JsonModifier |
API to the JsonParser.
This is a memory-efficient JSON parser based on jsmn. It only keeps one copy of the data in raw format and an array of tokens. You make calls to read values out.
JsonParser::JsonParser | ( | ) |
Construct a parser object.
This version dynamically allocates the buffer and token storage. If you want to minimize memory allocations you can pass in a static buffer and array of tokens to use instead.
|
virtual |
Destroy a parser object.
If the buffer was allocated dynamically it will be deleted. If you passed in a static buffer the static buffer is not deleted.
bool JsonParser::allocateTokens | ( | size_t | maxTokens | ) |
Preallocates a specific number of tokens.
Optional: You should set this larger than the expected number of tokens for efficiency, but if you are not using the static allocator it will resize the token storage space if it's too small.
void JsonParser::copyTokenValue | ( | const JsonParserGeneratorRK::jsmntok_t * | token, |
char * | dst, | ||
size_t | dstLen | ||
) | const |
Copies the value of the token into a buffer, making it a null-terminated cstring.
If the string is longer than dstLen - 1 bytes, it will be truncated and the result will still be a valid cstring.
This is used internally because the token data is not null-terminated, and doing things like sscanf or strtoul on it can read past the end of the buffer. This assures that only null-terminated data is passed to these functions.
size_t JsonParser::getArraySize | ( | const JsonParserGeneratorRK::jsmntok_t * | arrayContainer | ) | const |
Given a token for an JSON array in arrayContainer, gets the number of elements in the array.
0 = no elements, 1 = one element, ...
The index values for getValueByIndex(), etc. are 0-based, so the last index you pass in is less than getArraySize().
|
inline |
Gets the key/value pair of an object by index.
container | The object to look in (see getOuterKeyValueByIndex if you want to the outermost object you parsed) |
index | 0 = first, 1 = second, ... |
key | Filled in with the name of the key |
result | Filled in with the value. The value can be of type: bool, int, unsigned long, float, double, String, or (char *, size_t&). |
Normally you get a value in an object by its key, but if you want to iterate all of the keys you can use this method. Call it until it returns false.
This should only be used for things like string, numbers, booleans, etc.. If you want to get a JSON array or object within an object, use getValueTokenByKey() instead.
bool JsonParser::getKeyValueTokenByIndex | ( | const JsonParserGeneratorRK::jsmntok_t * | container, |
const JsonParserGeneratorRK::jsmntok_t *& | key, | ||
const JsonParserGeneratorRK::jsmntok_t *& | value, | ||
size_t | index | ||
) | const |
Given a JSON object in container, gets the key/value pair specified by index. Internal use only.
container | The array token to look in. |
key | Filled in with the key token for nth key value pair. |
value | Filled in with the value token for then nth key value pair. |
index | The index to retrieve (0 = first, 1 = second, ...). |
This is a low-level function; you will typically use getValueByIndex() or getValueByKey() instead.
const JsonParserGeneratorRK::jsmntok_t * JsonParser::getOuterArray | ( | ) | const |
Gets the outer JSON array token.
Sometimes the JSON will contain an array of values (or objects) instead of starting with an object. This gets the outermost array.
A token (JsonParserGeneratorRK::jsmntok_t) identifies a particular piece of data in the JSON data, such as an object, array, or element within an object or array, such as a string, integer, boolean, etc..
|
inline |
Gets the key/value pair of the outer object by index (0 = first, 1 = second, ...)
Normally you get a value in an object by its key, but if you want to iterate all of the keys you can use this method.
index | 0 = first, 1 = second, ... |
key | Filled in with the name of the key |
result | Filled in with the value. The value can be of type: bool, int, unsigned long, float, double, String, or (char *, size_t&). |
This should only be used for things like string, numbers, booleans, etc.. If you want to get a JSON array or object within an object, use getValueTokenByKey() instead.
const JsonParserGeneratorRK::jsmntok_t * JsonParser::getOuterObject | ( | ) | const |
Gets the outer JSON object token.
Typically JSON will contain an object that contains values and possibly other objects. This method gets the token for the outer object.
A token (JsonParserGeneratorRK::jsmntok_t) identifies a particular piece of data in the JSON data, such as an object, array, or element within an object or array, such as a string, integer, boolean, etc..
const JsonParserGeneratorRK::jsmntok_t * JsonParser::getOuterToken | ( | ) | const |
Gets the outer JSON object or array token.
A token (JsonParserGeneratorRK::jsmntok_t) identifies a particular piece of data in the JSON data, such as an object, array, or element within an object or array, such as a string, integer, boolean, etc..
|
inline |
Gets the value with the specified key name out of the outer object.
name | The name of the key to retrieve |
result | The returned data. |
The outer object must be a JSON object, not an array.
This should only be used for things like string, numbers, booleans, etc.. If you want to get a JSON array or object within an object, use getValueTokenByKey() instead.
const JsonParserGeneratorRK::jsmntok_t * JsonParser::getTokenByIndex | ( | const JsonParserGeneratorRK::jsmntok_t * | container, |
size_t | desiredIndex | ||
) | const |
Given a containing object, finds the nth token in the object. Internal use only.
container | The array token to look in. |
desiredIndex | The index to retrieve (0 = first, 1 = second, ...). |
This is used internally. It should not be used to get the nth array value, use getValueTokenByIndex instead.
bool JsonParser::getTokenJsonString | ( | const JsonParserGeneratorRK::jsmntok_t * | token, |
String & | result | ||
) | const |
Converts a token (object, array, string, or primitive) back into JSON in a Wiring String.
token | The token to convert back to a string |
result | Filled in with the string. Any previous contents in the string are cleared first. |
bool JsonParser::getTokenJsonString | ( | const JsonParserGeneratorRK::jsmntok_t * | token, |
char * | str, | ||
size_t & | strLen | ||
) | const |
Converts a token (object, array, string, or primitive) back into JSON in a buffer.
token | The token to convert back to a string |
str | The buffer to be written to |
strLen | The length of the buffer on entry, set to the number of bytes written on exit. |
bool JsonParser::getTokenJsonString | ( | const JsonParserGeneratorRK::jsmntok_t * | token, |
JsonParserString & | str | ||
) | const |
Gets a token as a JSON string.
token | The token to convert back to a string |
str | The JsonParserString object to write to |
This overload is typically used internally, normally you'd use the version that takes a String& or char *, size_t.
bool JsonParser::getTokenValue | ( | const JsonParserGeneratorRK::jsmntok_t * | token, |
bool & | result | ||
) | const |
Gets a bool (boolean) value.
Normally you'd use getValueByKey(), getValueByIndex() or getValueByColRow() which will automatically use this when the result parameter is a bool variable.
bool JsonParser::getTokenValue | ( | const JsonParserGeneratorRK::jsmntok_t * | token, |
int & | result | ||
) | const |
Gets an integer value.
Normally you'd use getValueByKey(), getValueByIndex() or getValueByColRow() which will automatically use this when the result parameter is an int variable.
bool JsonParser::getTokenValue | ( | const JsonParserGeneratorRK::jsmntok_t * | token, |
unsigned long & | result | ||
) | const |
Gets an unsigned long value.
Normally you'd use getValueByKey(), getValueByIndex() or getValueByColRow() which will automatically use this when the result parameter is an unsigned long variable.
bool JsonParser::getTokenValue | ( | const JsonParserGeneratorRK::jsmntok_t * | token, |
float & | result | ||
) | const |
Gets a float (single precision floating point) value.
Normally you'd use getValueByKey(), getValueByIndex() or getValueByColRow() which will automatically use this when the result parameter is a float variable.
bool JsonParser::getTokenValue | ( | const JsonParserGeneratorRK::jsmntok_t * | token, |
double & | result | ||
) | const |
Gets a double (double precision floating point) value.
Normally you'd use getValueByKey(), getValueByIndex() or getValueByColRow() which will automatically use this when the result parameter is a double variable.
bool JsonParser::getTokenValue | ( | const JsonParserGeneratorRK::jsmntok_t * | token, |
String & | result | ||
) | const |
Gets a String value into a Wiring String object.
This will automatically decode Unicode character escapes in the data and the returned String will contain UTF-8.
Normally you'd use getValueByKey(), getValueByIndex() or getValueByColRow() which will automatically use this when the result parameter is a String variable.
bool JsonParser::getTokenValue | ( | const JsonParserGeneratorRK::jsmntok_t * | token, |
char * | str, | ||
size_t & | strLen | ||
) | const |
Gets a string as a c-string into the specified buffer.
If the token specifies too large of a string it will be truncated. This will automatically decode Unicode character escapes in the data and the returned string will contain UTF-8.
bool JsonParser::getTokenValue | ( | const JsonParserGeneratorRK::jsmntok_t * | token, |
JsonParserString & | str | ||
) | const |
Gets a string as a JsonParserString object.
This is used internally by getTokenValue() overloads that take a String or buffer and length; you will normally not need to use this directly.
This will automatically decode Unicode character escapes in the data and the returned string will contain UTF-8.
|
inline |
This method is used to extract data from a 2-dimensional JSON array, an array of arrays of values.
arrayContainer | A token for an array containing another array |
col | The column (outer array index, 0 = first column, 1 = second column, ...) |
row | The row (inner array index, 0 = first row, 1 = second row, ...) |
result | Filled in with the value. The value can be of type: bool, int, unsigned long, float, double, String, or (char *, size_t&). |
This should only be used for things like string, numbers, booleans, etc.. If you want to get a JSON array or object within a two-dimensional array, use getValueTokenByColRow() instead.
|
inline |
Given an array token in arrayContainer, gets the value with the specified index.
arrayContainer | A token for an array |
index | The index in the array. 0 = first item, 1 = second item, ... |
result | Filled in with the value. The value can be of type: bool, int, unsigned long, float, double, String, or (char *, size_t&). |
This should only be used for things like string, numbers, booleans, etc.. If you want to get a JSON array or object within an array, use getValueTokenByIndex() instead.
|
inline |
Given an object token in container, gets the value with the specified key name.
container | The token for the object to obtain the data from. |
name | The name of the key to retrieve |
result | The returned data. The value can be of type: bool, int, unsigned long, float, double, String, or (char *, size_t&). |
This should only be used for things like string, numbers, booleans, etc.. If you want to get a JSON array or object within an object, use getValueTokenByKey() instead.
bool JsonParser::getValueTokenByColRow | ( | const JsonParserGeneratorRK::jsmntok_t * | container, |
size_t | col, | ||
size_t | row, | ||
const JsonParserGeneratorRK::jsmntok_t *& | value | ||
) | const |
This method is used to extract data from a 2-dimensional JSON array, an array of arrays of values.
container | A token for an array containing another array |
col | The column (outer array index, 0 = first column, 1 = second column, ...) |
row | The row (inner array index, 0 = first row, 1 = second row, ...) |
value | Filled in with the token for the value for key. |
This can be used for 2-dimensional arrays whose values are arrays or objects, to get the token for the container. It can also be used for values, but normally you'd use getValueByColRow() instead, which is generally more convenient.
bool JsonParser::getValueTokenByIndex | ( | const JsonParserGeneratorRK::jsmntok_t * | container, |
size_t | desiredIndex, | ||
const JsonParserGeneratorRK::jsmntok_t *& | value | ||
) | const |
Given an array token in container, gets the token value with the specified index.
container | The array token to look in. |
desiredIndex | The index to retrieve (0 = first, 1 = second, ...). |
value | Filled in with the token for the value for key. |
This can be used for arrays whose values are arrays or objects, to get the token for the container. It can also be used for values, but normally you'd use getValueByIndex() instead, which is generally more convenient.
bool JsonParser::getValueTokenByKey | ( | const JsonParserGeneratorRK::jsmntok_t * | container, |
const char * | key, | ||
const JsonParserGeneratorRK::jsmntok_t *& | value | ||
) | const |
Given an object token in container, gets the token value with the specified key name.
container | The object token to look in. |
key | The key to look for. |
value | Filled in with the token for the value for key. |
This can be used for objects whose keys are arrays or objects, to get the token for the container. It can also be used for values, but normally you'd use getValueByKey() instead, which is generally more convenient.
bool JsonParser::parse | ( | ) |
Parses the data you have added using addData() or addString().
When parsing data split into multiple chunks as a webhook response you can call addString() in your webhook subscription handler and call parse after each chunk. Only on the last chunk will parse return true, and you'll know the entire reponse has been received.
bool JsonParser::skipObject | ( | const JsonParserGeneratorRK::jsmntok_t * | container, |
const JsonParserGeneratorRK::jsmntok_t *& | obj | ||
) | const |
Used internally to skip over the token in obj.
container | The array token to look in. |
obj | Object within the token, updated to the next object if true is returned |
For simple primitives and strings, this is equivalent to obj++. For objects and arrays, however, this skips over the entire object or array, including any nested objects within them.