JsonParserGeneratorRK
|
Class for building a JSON string. More...
#include <JsonParserGeneratorRK.h>
Public Member Functions | |
JsonWriter () | |
Construct a JsonWriter with a dynamically allocated buffer. More... | |
virtual | ~JsonWriter () |
Destroy the object. If the buffer was dynamically allocated it will be freed. More... | |
JsonWriter (char *buffer, size_t bufferLen) | |
Construct a JsonWriter to write to a static buffer. More... | |
void | init () |
Reset the writer, clearing all data. More... | |
bool | startObject () |
Start a new JSON object. Make sure you finish it with finishObjectOrArray() | |
bool | startArray () |
Start a new JSON array. Make sure you finish it with finishObjectOrArray() | |
void | finishObjectOrArray () |
Finsh an object or array started with startObject() or startArray() | |
void | insertValue (bool value) |
Inserts a boolean value ("true" or "false"). More... | |
void | insertValue (int value) |
Inserts an integer value. More... | |
void | insertValue (unsigned int value) |
Inserts an unsigned integer value. More... | |
void | insertValue (long value) |
Inserts a long integer value. More... | |
void | insertValue (unsigned long value) |
Inserts an unsigned long integer value. More... | |
void | insertValue (float value) |
Inserts a floating point value. More... | |
void | insertValue (double value) |
Inserts a floating point double value. More... | |
void | insertValue (const char *value) |
Inserts a quoted string value. This escapes special characters and encodes utf-8. More... | |
void | insertValue (const String &value) |
Inserts a quoted string value. More... | |
void | insertKeyObject (const char *key) |
Inserts a new key and empty object. You must close the object using finishObjectOrArray()! More... | |
void | insertKeyArray (const char *key) |
Inserts a new key and empty array. You must close the object using finishObjectOrArray()! More... | |
template<class T > | |
void | insertKeyValue (const char *key, T value) |
Inserts a key/value pair into an object. More... | |
template<class T > | |
void | insertArrayValue (T value) |
Inserts a value into an array. More... | |
bool | isTruncated () const |
void | setFloatPlaces (int floatPlaces) |
Sets the number of digits for formatting float and double values. More... | |
void | insertCheckSeparator () |
Check to see if a separator needs to be inserted. Used internally. More... | |
bool | startObjectOrArray (char startChar, char endChar) |
Used internally to start an object or array. More... | |
void | insertChar (char ch) |
Used internally to insert a character. More... | |
void | insertString (const char *s, bool quoted=false) |
Used internally to insert a string, quoted or not. More... | |
void | insertsprintf (const char *fmt,...) |
Used internally to insert using snprintf formatting. More... | |
void | insertvsprintf (const char *fmt, va_list ap) |
Used internally to insert using snprintf formatting with a va_list. More... | |
void | setIsFirst (bool isFirst=true) |
Used internally to set the current isFirst flag in the context. | |
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 Attributes | |
static const size_t | MAX_NESTED_CONTEXT = 9 |
Protected Attributes | |
size_t | contextIndex |
Index into the context for the current level of nesting. | |
JsonWriterContext | context [MAX_NESTED_CONTEXT] |
Structure for managing nested objects. | |
bool | truncated |
true if data was added that didn't fit and was truncated | |
int | floatPlaces |
default number of places to display for floating point numbers (default is -1, the default for sprintf) | |
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. | |
Class for building a JSON string.
JsonWriter::JsonWriter | ( | ) |
Construct a JsonWriter with a dynamically allocated buffer.
The buffer will be resized as necessary but you can improve efficiency by using the allocate() method of JsonBuffer to pre-allocate space rather than have to incrementally make it bigger as it's written to.
Use getBuffer() to get the pointer to the buffer and getOffset() to get the buffer pointer and size. The buffer is not null-terminated!
|
virtual |
Destroy the object. If the buffer was dynamically allocated it will be freed.
If the buffer was passed in using the buffer, bufferLen constructor the buffer is not freed by this call as it's likely statically allocated.
JsonWriter::JsonWriter | ( | char * | buffer, |
size_t | bufferLen | ||
) |
Construct a JsonWriter to write to a static buffer.
buffer | Pointer to the buffer |
bufferLen | Length of the buffer in bytes |
void JsonWriter::init | ( | ) |
Reset the writer, clearing all data.
You do not need to call init() as it's called from the two constructors. You can call it again if you want to reset the writer and reuse it, such as when you use JsonWriterStatic in a global variable.
|
inline |
Inserts a value into an array.
Uses templates so you can pass any type object that's supported by insertValue() overloads, for example: bool, int, float, double, const char *.
void JsonWriter::insertChar | ( | char | ch | ) |
Used internally to insert a character.
Used internally. You should use insertKeyValue() or insertArrayValue() with a string instead.
void JsonWriter::insertCheckSeparator | ( | ) |
Check to see if a separator needs to be inserted. Used internally.
You normally don't need to use this as it's called by insertKeyValue() and insertArrayValue().
void JsonWriter::insertKeyArray | ( | const char * | key | ) |
Inserts a new key and empty array. You must close the object using finishObjectOrArray()!
key | the key name to insert |
void JsonWriter::insertKeyObject | ( | const char * | key | ) |
Inserts a new key and empty object. You must close the object using finishObjectOrArray()!
key | the key name to insert |
|
inline |
Inserts a key/value pair into an object.
Uses templates so you can pass any type object that's supported by insertValue() overloads, for example: bool, int, float, double, const char *.
void JsonWriter::insertsprintf | ( | const char * | fmt, |
... | |||
) |
Used internally to insert using snprintf formatting.
Used internally. You should use insertKeyValue() or insertArrayValue() with a string, float, or double instead.
This method does not quote or escape the string - it's used mainly for formatting numbers.
void JsonWriter::insertString | ( | const char * | s, |
bool | quoted = false |
||
) |
Used internally to insert a string, quoted or not.
Used internally. You should use insertKeyValue() or insertArrayValue() with a string instead.
void JsonWriter::insertValue | ( | bool | value | ) |
Inserts a boolean value ("true" or "false").
You would normally use insertKeyValue() or insertArrayValue() instead of calling this directly as those functions take care of inserting the separtators between items.
|
inline |
Inserts an integer value.
You would normally use insertKeyValue() or insertArrayValue() instead of calling this directly as those functions take care of inserting the separators between items.
|
inline |
Inserts an unsigned integer value.
You would normally use insertKeyValue() or insertArrayValue() instead of calling this directly as those functions take care of inserting the separators between items.
|
inline |
Inserts a long integer value.
You would normally use insertKeyValue() or insertArrayValue() instead of calling this directly as those functions take care of inserting the separators between items.
|
inline |
Inserts an unsigned long integer value.
You would normally use insertKeyValue() or insertArrayValue() instead of calling this directly as those functions take care of inserting the separators between items.
void JsonWriter::insertValue | ( | float | value | ) |
Inserts a floating point value.
Use setFloatPlaces() to set the number of decimal places to include.
You would normally use insertKeyValue() or insertArrayValue() instead of calling this directly as those functions take care of inserting the separtators between items.
void JsonWriter::insertValue | ( | double | value | ) |
Inserts a floating point double value.
Use setFloatPlaces() to set the number of decimal places to include.
You would normally use insertKeyValue() or insertArrayValue() instead of calling this directly as those functions take care of inserting the separtators between items.
|
inline |
Inserts a quoted string value. This escapes special characters and encodes utf-8.
You would normally use insertKeyValue() or insertArrayValue() instead of calling this directly as those functions take care of inserting the separtators between items.
|
inline |
Inserts a quoted string value.
This escapes special characters and encodes utf-8. See also the version that takes a plain const char *.
You would normally use insertKeyValue() or insertArrayValue() instead of calling this directly as those functions take care of inserting the separtators between items.
void JsonWriter::insertvsprintf | ( | const char * | fmt, |
va_list | ap | ||
) |
Used internally to insert using snprintf formatting with a va_list.
Used internally. You should use insertKeyValue() or insertArrayValue() with a string, float, or double instead.
This method does not quote or escape the string - it's used mainly for formatting numbers.
|
inline |
If you try to insert more data than will fit in the buffer, the isTruncated flag will be set, and the buffer will likely not be valid JSON and should not be used.
|
inline |
Sets the number of digits for formatting float and double values.
floatPlaces | The number of decimal places for float and double. Set it to -1 to use the default for snprintf. -1 is the default value if you don't call setFloatPlaces. |
bool JsonWriter::startObjectOrArray | ( | char | startChar, |
char | endChar | ||
) |
Used internally to start an object or array.
Used internally; you should use startObject() or startArray() instead. Make sure you finish any started object or array using finishObjectOrArray().
|
static |
This constant is the maximum number of nested objects that are supported; the actual number is one less than this so when set to 9 you can have eight objects nested in each other.
Overhead is 8 bytes per nested context, so 9 elements is 72 bytes.