JsonParserGeneratorRK
Public Member Functions | Protected Attributes
JsonParserString Class Reference

Class used internally for writing to strings. More...

#include <JsonParserGeneratorRK.h>

Public Member Functions

 JsonParserString (String *str)
 Construct a JsonParserString wrapping a Wiring String. More...
 
 JsonParserString (char *buf, size_t bufLen)
 Construct a JsonParserString wrapping a buffer and length. More...
 
void append (char ch)
 Append a single char to the underlying string. More...
 
void append (const char *str, size_t len)
 Append a buffer and length to the underlying string. More...
 
size_t getLength () const
 Get the length of the string. More...
 

Protected Attributes

Stringstr
 When writing to a String, the String object.
 
char * buf
 When writing to a buffer, the pointer to the buffer. Not used for String.
 
size_t bufLen
 When writing to a buffer, the length of the buffer in bytes. Not used for String.
 
size_t length
 The current offset being written to.
 

Detailed Description

Class used internally for writing to strings.

This is a wrapper around either String (the Wiring version) or a buffer and length. This allows writing to a static buffer with no dynamic memory allocation at all.

One of the things about String is that while you can pre-allocate reserve space for data, you can't get access to the internal length field, so you can't write to raw bytes then resize it to the correct size. This wrapper is that allows appending to either a String or buffer to get around this limitation of String.

You can also use it for sizing only by passing NULL for buf.

Constructor & Destructor Documentation

◆ JsonParserString() [1/2]

JsonParserString::JsonParserString ( String str)

Construct a JsonParserString wrapping a Wiring String.

Parameters
strA pointer Wiring String object to write to.

◆ JsonParserString() [2/2]

JsonParserString::JsonParserString ( char *  buf,
size_t  bufLen 
)

Construct a JsonParserString wrapping a buffer and length.

Parameters
bufA pointer to a buffer
bufLenThe length of the buffer in bytes

Member Function Documentation

◆ append() [1/2]

void JsonParserString::append ( char  ch)

Append a single char to the underlying string.

Parameters
chThe char to append.

◆ append() [2/2]

void JsonParserString::append ( const char *  str,
size_t  len 
)

Append a buffer and length to the underlying string.

Parameters
strA pointer to the character to add. Does not need to be null-terminated.
lenLength of the string to append in bytes.

◆ getLength()

size_t JsonParserString::getLength ( ) const
inline

Get the length of the string.

Returns
The string length in bytes. If the string contains UTF-8 characters, it will be the number of bytes, not characters.

For buffer and bufLenb, the maximum string length will be bufLen - 1 to leave room for the null terminator.


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