JsonParserGeneratorRK
Public Member Functions | Static Public Member Functions | Protected Attributes | Friends
JsonParser Class Reference

API to the JsonParser. More...

#include <JsonParserGeneratorRK.h>

Inheritance diagram for JsonParser:
JsonBuffer JsonParserStatic< BUFFER_SIZE, MAX_TOKENS >

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_tgetOuterObject () const
 Gets the outer JSON object token. More...
 
const JsonParserGeneratorRK::jsmntok_tgetOuterArray () const
 Gets the outer JSON array token. More...
 
const JsonParserGeneratorRK::jsmntok_tgetOuterToken () 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_tgetTokenByIndex (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_tgetTokens ()
 Used internally in the test suite for printing the token list.
 
JsonParserGeneratorRK::jsmntok_tgetTokensEnd ()
 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_ttokens
 Array of tokens after parsing.
 
JsonParserGeneratorRK::jsmntok_ttokensEnd
 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
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ JsonParser()

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.

◆ ~JsonParser()

JsonParser::~JsonParser ( )
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.

Member Function Documentation

◆ allocateTokens()

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.

◆ copyTokenValue()

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.

◆ getArraySize()

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().

◆ getKeyValueByIndex()

template<class T >
bool JsonParser::getKeyValueByIndex ( const JsonParserGeneratorRK::jsmntok_t container,
size_t  index,
String key,
T &  result 
) const
inline

Gets the key/value pair of an object by index.

Parameters
containerThe object to look in (see getOuterKeyValueByIndex if you want to the outermost object you parsed)
index0 = first, 1 = second, ...
keyFilled in with the name of the key
resultFilled in with the value. The value can be of type: bool, int, unsigned long, float, double, String, or (char *, size_t&).
Returns
true if the call succeeded or false if it failed.

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.

◆ getKeyValueTokenByIndex()

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.

Parameters
containerThe array token to look in.
keyFilled in with the key token for nth key value pair.
valueFilled in with the value token for then nth key value pair.
indexThe index to retrieve (0 = first, 1 = second, ...).

This is a low-level function; you will typically use getValueByIndex() or getValueByKey() instead.

◆ getOuterArray()

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..

◆ getOuterKeyValueByIndex()

template<class T >
bool JsonParser::getOuterKeyValueByIndex ( size_t  index,
String key,
T &  result 
) const
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.

Parameters
index0 = first, 1 = second, ...
keyFilled in with the name of the key
resultFilled in with the value. The value can be of type: bool, int, unsigned long, float, double, String, or (char *, size_t&).
Returns
true if the call succeeded or false if it failed.

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.

◆ getOuterObject()

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..

◆ getOuterToken()

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..

◆ getOuterValueByKey()

template<class T >
bool JsonParser::getOuterValueByKey ( const char *  name,
T &  result 
) const
inline

Gets the value with the specified key name out of the outer object.

Parameters
nameThe name of the key to retrieve
resultThe returned data.
Returns
true if the data was retrieved successfully, false if not (key not present or incompatible data type).

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.

◆ getTokenByIndex()

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.

Parameters
containerThe array token to look in.
desiredIndexThe index to retrieve (0 = first, 1 = second, ...).
Returns
The token

This is used internally. It should not be used to get the nth array value, use getValueTokenByIndex instead.

◆ getTokenJsonString() [1/3]

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.

Parameters
tokenThe token to convert back to a string
resultFilled in with the string. Any previous contents in the string are cleared first.

◆ getTokenJsonString() [2/3]

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.

Parameters
tokenThe token to convert back to a string
strThe buffer to be written to
strLenThe length of the buffer on entry, set to the number of bytes written on exit.

◆ getTokenJsonString() [3/3]

bool JsonParser::getTokenJsonString ( const JsonParserGeneratorRK::jsmntok_t token,
JsonParserString str 
) const

Gets a token as a JSON string.

Parameters
tokenThe token to convert back to a string
strThe 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.

◆ getTokenValue() [1/8]

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.

◆ getTokenValue() [2/8]

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.

◆ getTokenValue() [3/8]

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.

◆ getTokenValue() [4/8]

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.

◆ getTokenValue() [5/8]

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.

◆ getTokenValue() [6/8]

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.

◆ getTokenValue() [7/8]

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.

◆ getTokenValue() [8/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.

◆ getValueByColRow()

template<class T >
bool JsonParser::getValueByColRow ( const JsonParserGeneratorRK::jsmntok_t arrayContainer,
size_t  col,
size_t  row,
T &  result 
) const
inline

This method is used to extract data from a 2-dimensional JSON array, an array of arrays of values.

Parameters
arrayContainerA token for an array containing another array
colThe column (outer array index, 0 = first column, 1 = second column, ...)
rowThe row (inner array index, 0 = first row, 1 = second row, ...)
resultFilled in with the value. The value can be of type: bool, int, unsigned long, float, double, String, or (char *, size_t&).
Returns
true if the call succeeded or false if it failed. You can call this repeatedly until it returns false to iterate the array.

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.

◆ getValueByIndex()

template<class T >
bool JsonParser::getValueByIndex ( const JsonParserGeneratorRK::jsmntok_t arrayContainer,
size_t  index,
T &  result 
) const
inline

Given an array token in arrayContainer, gets the value with the specified index.

Parameters
arrayContainerA token for an array
indexThe index in the array. 0 = first item, 1 = second item, ...
resultFilled in with the value. The value can be of type: bool, int, unsigned long, float, double, String, or (char *, size_t&).
Returns
true if the call succeeded or false if it failed. You can call this repeatedly until it returns false to iterate the 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 array, use getValueTokenByIndex() instead.

◆ getValueByKey()

template<class T >
bool JsonParser::getValueByKey ( const JsonParserGeneratorRK::jsmntok_t container,
const char *  name,
T &  result 
) const
inline

Given an object token in container, gets the value with the specified key name.

Parameters
containerThe token for the object to obtain the data from.
nameThe name of the key to retrieve
resultThe returned data. The value can be of type: bool, int, unsigned long, float, double, String, or (char *, size_t&).
Returns
true if the data was retrieved successfully, false if not (key not present or incompatible data type).

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.

◆ getValueTokenByColRow()

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.

Parameters
containerA token for an array containing another array
colThe column (outer array index, 0 = first column, 1 = second column, ...)
rowThe row (inner array index, 0 = first row, 1 = second row, ...)
valueFilled in with the token for the value for key.
Returns
true if the index row and column are valid or false if either is out of range.

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.

◆ getValueTokenByIndex()

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.

Parameters
containerThe array token to look in.
desiredIndexThe index to retrieve (0 = first, 1 = second, ...).
valueFilled in with the token for the value for key.
Returns
true if the index is valid or false if the index exceeds the size of the array.

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.

◆ getValueTokenByKey()

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.

Parameters
containerThe object token to look in.
keyThe key to look for.
valueFilled in with the token for the value for key.
Returns
true if the key is found or false if not.

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.

◆ parse()

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.

◆ skipObject()

bool JsonParser::skipObject ( const JsonParserGeneratorRK::jsmntok_t container,
const JsonParserGeneratorRK::jsmntok_t *&  obj 
) const

Used internally to skip over the token in obj.

Parameters
containerThe array token to look in.
objObject within the token, updated to the next object if true is returned
Returns
true if there was a next object, false if not.

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.


The documentation for this class was generated from the following files: