Skip to content

File syn_json_write.h

FileList > src > syntropic > util > syn_json_write.h

Go to the source code of this file

Streaming JSON builder — zero-alloc, caller-provided buffer. More...

  • #include <stdbool.h>
  • #include <stddef.h>
  • #include <stdint.h>

Classes

Type Name
struct SYN_JsonWriter
Streaming JSON writer — tracks nesting, commas, and overflow.

Public Functions

Type Name
void syn_json_arr_close (SYN_JsonWriter * w)
Close a JSON array ] .
void syn_json_arr_open (SYN_JsonWriter * w)
Open a JSON array [ .
void syn_json_init (SYN_JsonWriter * w, char * buf, size_t capacity)
Initialize a JSON writer.
void syn_json_key (SYN_JsonWriter * w, const char * key)
Write a bare key "key": for nested objects/arrays.
void syn_json_key_bool (SYN_JsonWriter * w, const char * key, bool val)
Write "key":true or"key":false .
void syn_json_key_int (SYN_JsonWriter * w, const char * key, int32_t val)
Write "key":123 .
void syn_json_key_null (SYN_JsonWriter * w, const char * key)
Write "key":null .
void syn_json_key_str (SYN_JsonWriter * w, const char * key, const char * val)
Write "key":"value" . Escapes" and\\ in value.
void syn_json_key_uint (SYN_JsonWriter * w, const char * key, uint32_t val)
Write "key":123 (unsigned).
void syn_json_obj_close (SYN_JsonWriter * w)
Close a JSON object } .
void syn_json_obj_open (SYN_JsonWriter * w)
Open a JSON object { .
void syn_json_val_bool (SYN_JsonWriter * w, bool val)
Append a boolean value to an array.
void syn_json_val_int (SYN_JsonWriter * w, int32_t val)
Append an integer value to an array.
void syn_json_val_str (SYN_JsonWriter * w, const char * val)
Append a string value to an array.
void syn_json_val_uint (SYN_JsonWriter * w, uint32_t val)
Append an unsigned integer value to an array.

Public Static Functions

Type Name
size_t syn_json_len (const SYN_JsonWriter * w)
Get the number of bytes written.
bool syn_json_ok (const SYN_JsonWriter * w)
Check if the writer is in a valid state (no overflow).
const char * syn_json_str (const SYN_JsonWriter * w)
Get the null-terminated output string.

Macros

Type Name
define SYN_JSON_MAX_DEPTH 8

Detailed Description

Builds JSON output incrementally into a fixed buffer. Handles auto-commas, nesting, and overflow detection. No parser — this is write-only for building API responses, telemetry payloads, etc.

** **

char buf[256];
SYN_JsonWriter w;
syn_json_init(&w, buf, sizeof(buf));
syn_json_obj_open(&w);
  syn_json_key_str(&w, "device", "esp32");
  syn_json_key_int(&w, "uptime", 12345);
  syn_json_key_bool(&w, "wifi", true);
syn_json_obj_close(&w);
// buf = {"device":"esp32","uptime":12345,"wifi":true}

Public Functions Documentation

function syn_json_arr_close

Close a JSON array ] .

void syn_json_arr_close (
    SYN_JsonWriter * w
) 

Parameters:

  • w Writer.

function syn_json_arr_open

Open a JSON array [ .

void syn_json_arr_open (
    SYN_JsonWriter * w
) 

Parameters:

  • w Writer.

function syn_json_init

Initialize a JSON writer.

void syn_json_init (
    SYN_JsonWriter * w,
    char * buf,
    size_t capacity
) 

Parameters:

  • w Writer instance.
  • buf Output buffer.
  • capacity Buffer size in bytes.

function syn_json_key

Write a bare key "key": for nested objects/arrays.

void syn_json_key (
    SYN_JsonWriter * w,
    const char * key
) 

Follow with syn_json_obj_open() or syn_json_arr_open().

Parameters:

  • w Writer.
  • key JSON key.

function syn_json_key_bool

Write "key":true or"key":false .

void syn_json_key_bool (
    SYN_JsonWriter * w,
    const char * key,
    bool val
) 

Parameters:

  • w Writer.
  • key JSON key.
  • val Boolean value.

function syn_json_key_int

Write "key":123 .

void syn_json_key_int (
    SYN_JsonWriter * w,
    const char * key,
    int32_t val
) 

Parameters:

  • w Writer.
  • key JSON key.
  • val Integer value.

function syn_json_key_null

Write "key":null .

void syn_json_key_null (
    SYN_JsonWriter * w,
    const char * key
) 

Parameters:

  • w Writer.
  • key JSON key.

function syn_json_key_str

Write "key":"value" . Escapes" and\\ in value.

void syn_json_key_str (
    SYN_JsonWriter * w,
    const char * key,
    const char * val
) 

Parameters:

  • w Writer.
  • key JSON key.
  • val String value.

function syn_json_key_uint

Write "key":123 (unsigned).

void syn_json_key_uint (
    SYN_JsonWriter * w,
    const char * key,
    uint32_t val
) 

Parameters:

  • w Writer.
  • key JSON key.
  • val Unsigned integer value.

function syn_json_obj_close

Close a JSON object } .

void syn_json_obj_close (
    SYN_JsonWriter * w
) 

Parameters:

  • w Writer.

function syn_json_obj_open

Open a JSON object { .

void syn_json_obj_open (
    SYN_JsonWriter * w
) 

Parameters:

  • w Writer.

function syn_json_val_bool

Append a boolean value to an array.

void syn_json_val_bool (
    SYN_JsonWriter * w,
    bool val
) 

Parameters:

  • w Writer.
  • val Boolean value.

function syn_json_val_int

Append an integer value to an array.

void syn_json_val_int (
    SYN_JsonWriter * w,
    int32_t val
) 

Parameters:

  • w Writer.
  • val Integer value.

function syn_json_val_str

Append a string value to an array.

void syn_json_val_str (
    SYN_JsonWriter * w,
    const char * val
) 

Parameters:

  • w Writer.
  • val String value.

function syn_json_val_uint

Append an unsigned integer value to an array.

void syn_json_val_uint (
    SYN_JsonWriter * w,
    uint32_t val
) 

Parameters:

  • w Writer.
  • val Unsigned value.

Public Static Functions Documentation

function syn_json_len

Get the number of bytes written.

static inline size_t syn_json_len (
    const SYN_JsonWriter * w
) 

Parameters:

  • w Writer.

Returns:

Byte count.


function syn_json_ok

Check if the writer is in a valid state (no overflow).

static inline bool syn_json_ok (
    const SYN_JsonWriter * w
) 

Parameters:

  • w Writer.

Returns:

true if no overflow occurred.


function syn_json_str

Get the null-terminated output string.

static inline const char * syn_json_str (
    const SYN_JsonWriter * w
) 

Parameters:

  • w Writer.

Returns:

Pointer to the output buffer.


Macro Definition Documentation

define SYN_JSON_MAX_DEPTH

#define SYN_JSON_MAX_DEPTH `8`

Maximum nesting depth



The documentation for this class was generated from the following file src/syntropic/util/syn_json_write.h