DeterministicESPAsyncWebServer v6.27.1
Zero-allocation, bounded-execution async HTTP server for ESP32
Loading...
Searching...
No Matches
JsonWriter Class Reference

Builds a JSON document into a fixed caller buffer, no heap. More...

#include <json.h>

Public Member Functions

 JsonWriter (char *buf, size_t cap)
 Construct over a caller buffer.
 
void begin_object ()
 Open { (as a value/element where applicable).
 
void end_object ()
 Close }.
 
void begin_array ()
 Open [.
 
void end_array ()
 Close ].
 
void key (const char *k)
 Emit an object member name ("k":); follow with one value.
 
void str (const char *v)
 Emit a quoted, escaped string value.
 
void integer (long v)
 Emit a signed integer value.
 
void uinteger (unsigned long v)
 Emit an unsigned integer value.
 
void boolean (bool v)
 Emit true/false.
 
void null_value ()
 Emit null.
 
void raw (const char *literal)
 Emit a pre-formatted literal verbatim.
 
void kv_str (const char *k, const char *v)
 "k":"v" (escaped).
 
void kv_int (const char *k, long v)
 "k":<int>.
 
void kv_uint (const char *k, unsigned long v)
 "k":<uint>.
 
void kv_bool (const char *k, bool v)
 "k":true|false.
 
void kv_null (const char *k)
 "k":null.
 
void kv_raw (const char *k, const char *literal)
 "k":<literal>.
 
bool ok () const
 < False after any overflow / structural error.
 
size_t length () const
 < Bytes written so far (excludes the NUL).
 
const char * c_str () const
 < NUL-terminated output (truncated if !ok()).
 

Detailed Description

Builds a JSON document into a fixed caller buffer, no heap.

Commas, key quoting, and string escaping are emitted automatically. On buffer overflow or a structural error (nesting past JSON_MAX_DEPTH), writing stops and ok() returns false; c_str() still yields a NUL-terminated (truncated) string so a partial result never runs off the end.

Value methods (str(), integer(), ...) emit a single value - use them for array elements or immediately after key(). The kv_*() helpers emit a key and value together for object members.

Definition at line 58 of file json.h.

Constructor & Destructor Documentation

◆ JsonWriter()

JsonWriter::JsonWriter ( char *  buf,
size_t  cap 
)

Construct over a caller buffer.

Parameters
bufDestination (must be non-null, cap >= 1).
capCapacity in bytes including the NUL terminator.

Definition at line 18 of file json.cpp.

Member Function Documentation

◆ begin_object()

void JsonWriter::begin_object ( )

Open { (as a value/element where applicable).

Definition at line 144 of file json.cpp.

◆ end_object()

void JsonWriter::end_object ( )

Close }.

Definition at line 148 of file json.cpp.

◆ begin_array()

void JsonWriter::begin_array ( )

Open [.

Definition at line 152 of file json.cpp.

◆ end_array()

void JsonWriter::end_array ( )

Close ].

Definition at line 156 of file json.cpp.

◆ key()

void JsonWriter::key ( const char *  k)

Emit an object member name ("k":); follow with one value.

Definition at line 161 of file json.cpp.

Referenced by kv_bool(), kv_int(), kv_null(), kv_raw(), kv_str(), and kv_uint().

◆ str()

void JsonWriter::str ( const char *  v)

Emit a quoted, escaped string value.

Definition at line 171 of file json.cpp.

Referenced by kv_str().

◆ integer()

void JsonWriter::integer ( long  v)

Emit a signed integer value.

Definition at line 179 of file json.cpp.

Referenced by kv_int().

◆ uinteger()

void JsonWriter::uinteger ( unsigned long  v)

Emit an unsigned integer value.

Definition at line 187 of file json.cpp.

Referenced by kv_uint().

◆ boolean()

void JsonWriter::boolean ( bool  v)

Emit true/false.

Definition at line 195 of file json.cpp.

Referenced by kv_bool().

◆ null_value()

void JsonWriter::null_value ( )

Emit null.

Definition at line 201 of file json.cpp.

Referenced by kv_null().

◆ raw()

void JsonWriter::raw ( const char *  literal)

Emit a pre-formatted literal verbatim.

Definition at line 207 of file json.cpp.

Referenced by kv_raw().

◆ kv_str()

void JsonWriter::kv_str ( const char *  k,
const char *  v 
)

"k":"v" (escaped).

Definition at line 213 of file json.cpp.

References key(), and str().

◆ kv_int()

void JsonWriter::kv_int ( const char *  k,
long  v 
)

"k":<int>.

Definition at line 218 of file json.cpp.

References integer(), and key().

◆ kv_uint()

void JsonWriter::kv_uint ( const char *  k,
unsigned long  v 
)

"k":<uint>.

Definition at line 223 of file json.cpp.

References key(), and uinteger().

◆ kv_bool()

void JsonWriter::kv_bool ( const char *  k,
bool  v 
)

"k":true|false.

Definition at line 228 of file json.cpp.

References boolean(), and key().

◆ kv_null()

void JsonWriter::kv_null ( const char *  k)

"k":null.

Definition at line 233 of file json.cpp.

References key(), and null_value().

◆ kv_raw()

void JsonWriter::kv_raw ( const char *  k,
const char *  literal 
)

"k":<literal>.

Definition at line 238 of file json.cpp.

References key(), and raw().

◆ ok()

bool JsonWriter::ok ( ) const
inline

< False after any overflow / structural error.

Definition at line 90 of file json.h.

◆ length()

size_t JsonWriter::length ( ) const
inline

< Bytes written so far (excludes the NUL).

Definition at line 94 of file json.h.

◆ c_str()

const char * JsonWriter::c_str ( ) const
inline

< NUL-terminated output (truncated if !ok()).

Definition at line 98 of file json.h.


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