JsonParserGeneratorRK
Public Member Functions | Static Public Attributes | Protected Attributes
JsonWriter Class Reference

Class for building a JSON string. More...

#include <JsonParserGeneratorRK.h>

Inheritance diagram for JsonWriter:
JsonBuffer JsonModifier JsonWriterStatic< BUFFER_SIZE >

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.
 

Detailed Description

Class for building a JSON string.

Constructor & Destructor Documentation

◆ JsonWriter() [1/2]

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!

◆ ~JsonWriter()

JsonWriter::~JsonWriter ( )
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() [2/2]

JsonWriter::JsonWriter ( char *  buffer,
size_t  bufferLen 
)

Construct a JsonWriter to write to a static buffer.

Parameters
bufferPointer to the buffer
bufferLenLength of the buffer in bytes

Member Function Documentation

◆ init()

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.

◆ insertArrayValue()

template<class T >
void JsonWriter::insertArrayValue ( value)
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 *.

◆ insertChar()

void JsonWriter::insertChar ( char  ch)

Used internally to insert a character.

Used internally. You should use insertKeyValue() or insertArrayValue() with a string instead.

◆ insertCheckSeparator()

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

◆ insertKeyArray()

void JsonWriter::insertKeyArray ( const char *  key)

Inserts a new key and empty array. You must close the object using finishObjectOrArray()!

Parameters
keythe key name to insert

◆ insertKeyObject()

void JsonWriter::insertKeyObject ( const char *  key)

Inserts a new key and empty object. You must close the object using finishObjectOrArray()!

Parameters
keythe key name to insert

◆ insertKeyValue()

template<class T >
void JsonWriter::insertKeyValue ( const char *  key,
value 
)
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 *.

◆ insertsprintf()

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.

◆ insertString()

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.

◆ insertValue() [1/9]

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.

◆ insertValue() [2/9]

void JsonWriter::insertValue ( int  value)
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.

◆ insertValue() [3/9]

void JsonWriter::insertValue ( unsigned int  value)
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.

◆ insertValue() [4/9]

void JsonWriter::insertValue ( long  value)
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.

◆ insertValue() [5/9]

void JsonWriter::insertValue ( unsigned long  value)
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.

◆ insertValue() [6/9]

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.

◆ insertValue() [7/9]

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.

◆ insertValue() [8/9]

void JsonWriter::insertValue ( const char *  value)
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.

◆ insertValue() [9/9]

void JsonWriter::insertValue ( const String value)
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.

◆ insertvsprintf()

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.

◆ isTruncated()

bool JsonWriter::isTruncated ( ) const
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.

◆ setFloatPlaces()

void JsonWriter::setFloatPlaces ( int  floatPlaces)
inline

Sets the number of digits for formatting float and double values.

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

◆ startObjectOrArray()

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

Field Documentation

◆ MAX_NESTED_CONTEXT

const size_t JsonWriter::MAX_NESTED_CONTEXT = 9
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.


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