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

Layer 6 (Presentation) - zero-heap JSON: a bounded writer and top-level reader. More...

#include "ServerConfig.h"
#include <stddef.h>
#include <stdint.h>

Go to the source code of this file.

Classes

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

Functions

bool json_get_str (const char *json, const char *key, char *out, size_t out_cap)
 Read a top-level string member from a JSON object body.
 
bool json_get_int (const char *json, const char *key, long *out)
 Read a top-level integer member from a JSON object body.
 
bool json_get_bool (const char *json, const char *key, bool *out)
 Read a top-level boolean member (true/false) from a JSON object body.
 

Detailed Description

Layer 6 (Presentation) - zero-heap JSON: a bounded writer and top-level reader.

A deliberately small JSON helper for the common IoT shapes (a flat-ish object of strings / numbers / booleans, with bounded nesting). It allocates nothing: the writer formats into a caller-provided buffer, and the reader scans a NUL-terminated body in place. ArduinoJson remains the option when you need a full DOM - it heap-allocates, which this library avoids.

Writing

char buf[128];
JsonWriter w(buf, sizeof(buf));
w.begin_object();
w.kv_str("status", "ok");
w.kv_int("count", 3);
w.key("items"); w.begin_array();
w.str("a"); w.str("b");
w.end_array();
w.end_object();
if (w.ok()) server.send(slot, 200, "application/json", w.c_str());
// -> {"status":"ok","count":3,"items":["a","b"]}
Builds a JSON document into a fixed caller buffer, no heap.
Definition json.h:59

Reading (top-level keys of an object body)

char ssid[33];
if (json_get_str(req->body, "ssid", ssid, sizeof(ssid))) { ... }
long port;
if (json_get_int(req->body, "port", &port)) { ... }
bool json_get_str(const char *json, const char *key, char *out, size_t out_cap)
Read a top-level string member from a JSON object body.
Definition json.cpp:512
bool json_get_int(const char *json, const char *key, long *out)
Read a top-level integer member from a JSON object body.
Definition json.cpp:552

Definition in file json.h.

Function Documentation

◆ json_get_str()

bool json_get_str ( const char *  json,
const char *  key,
char *  out,
size_t  out_cap 
)

Read a top-level string member from a JSON object body.

Finds "key": "..." at the root object level (nested objects/arrays and string contents are skipped, so a same-named nested key is not matched), unescapes the value, and copies it (NUL-terminated, bounded by out_cap) into out.

Parameters
jsonNUL-terminated JSON object text.
keyMember name to find.
outDestination buffer.
out_capCapacity of out including the NUL.
Returns
true if a string member was found and copied; false otherwise.

Definition at line 512 of file json.cpp.

References EMITTED, and TRUNCATED.

◆ json_get_int()

bool json_get_int ( const char *  json,
const char *  key,
long *  out 
)

Read a top-level integer member from a JSON object body.

Returns
true if the member exists and parses as an integer; false otherwise.

Definition at line 552 of file json.cpp.

References det_strtol().

◆ json_get_bool()

bool json_get_bool ( const char *  json,
const char *  key,
bool *  out 
)

Read a top-level boolean member (true/false) from a JSON object body.

Returns
true if the member exists and is a JSON boolean; false otherwise.

Definition at line 567 of file json.cpp.