ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
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 charc_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 19 of file json.cpp.

Member Function Documentation

◆ begin_object()

void JsonWriter::begin_object ( )

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

Definition at line 161 of file json.cpp.

◆ end_object()

void JsonWriter::end_object ( )

Close }.

Definition at line 165 of file json.cpp.

◆ begin_array()

void JsonWriter::begin_array ( )

Open [.

Definition at line 169 of file json.cpp.

◆ end_array()

void JsonWriter::end_array ( )

Close ].

Definition at line 173 of file json.cpp.

◆ key()

void JsonWriter::key ( const char k)

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

Definition at line 178 of file json.cpp.

References raw().

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 188 of file json.cpp.

Referenced by kv_str().

◆ integer()

void JsonWriter::integer ( long  v)

Emit a signed integer value.

Definition at line 196 of file json.cpp.

References pc_sb_finish(), pc_sb_i64(), and raw().

Referenced by kv_int().

◆ uinteger()

void JsonWriter::uinteger ( unsigned long  v)

Emit an unsigned integer value.

Definition at line 209 of file json.cpp.

References pc_sb_finish(), pc_sb_u32(), and raw().

Referenced by kv_uint().

◆ boolean()

void JsonWriter::boolean ( bool  v)

Emit true/false.

Definition at line 222 of file json.cpp.

Referenced by kv_bool().

◆ null_value()

void JsonWriter::null_value ( )

Emit null.

Definition at line 228 of file json.cpp.

Referenced by kv_null().

◆ raw()

void JsonWriter::raw ( const char *  literal)

Emit a pre-formatted literal verbatim.

Definition at line 234 of file json.cpp.

References raw().

Referenced by integer(), key(), kv_bool(), kv_int(), kv_null(), kv_raw(), kv_str(), kv_uint(), raw(), and uinteger().

◆ kv_str()

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

"k":"v" (escaped).

Definition at line 240 of file json.cpp.

References key(), raw(), and str().

◆ kv_int()

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

"k":<int>.

Definition at line 245 of file json.cpp.

References integer(), key(), and raw().

◆ kv_uint()

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

"k":<uint>.

Definition at line 250 of file json.cpp.

References key(), raw(), and uinteger().

◆ kv_bool()

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

"k":true|false.

Definition at line 255 of file json.cpp.

References boolean(), key(), and raw().

◆ kv_null()

void JsonWriter::kv_null ( const char k)

"k":null.

Definition at line 260 of file json.cpp.

References key(), null_value(), and raw().

◆ kv_raw()

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

"k":<literal>.

Definition at line 265 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: