cryptnox-pos 1.0.0
Standalone USDC payment terminal firmware (ESP32 + Cryptnox smart card)
Loading...
Searching...
No Matches
json_out.h File Reference

Escaping for values the config portal puts inside a JSON string. More...

#include <stddef.h>
#include <stdio.h>
Include dependency graph for json_out.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

static size_t json_escape (char *out, size_t n, const char *val)
 Copy val into out, escaping what JSON requires.

Detailed Description

Escaping for values the config portal puts inside a JSON string.

Header-only and free of ESP-IDF dependencies, for the same reason form_parse.h is: one of the values that goes through here is a Wi-Fi SSID, which is 32 arbitrary bytes chosen by whoever named their router. A bare quote in one produces a response the page cannot parse — the whole config UI goes blank on a device with no console to read — so it is kept where a host test can reach it (tests/units/test_json_out.cpp).

Not a JSON writer. It escapes one string value; the surrounding braces, keys and commas are the caller's snprintf, which is fine because every one of those is a literal in provision.cpp.

Definition in file json_out.h.

Function Documentation

◆ json_escape()

size_t json_escape ( char * out,
size_t n,
const char * val )
inlinestatic

Copy val into out, escaping what JSON requires.

Handles the two characters that would end the string early (" and \\endiskip) and the C0 controls, which JSON forbids raw. Everything else is passed through as bytes, including UTF-8 — an SSID is not required to be valid UTF-8 and re-encoding it would be a second way to get it wrong.

Truncates rather than overflowing, and never mid-escape: the loop stops while there is still room for the longest single expansion (␟, six bytes) plus the NUL, so a clipped value is still parseable JSON.

Parameters
[out]outDestination, always NUL-terminated when n > 0.
[in]nCapacity of out including the NUL.
[in]valValue to escape; NULL is treated as empty.
Returns
bytes written, excluding the NUL.

Definition at line 46 of file json_out.h.

Referenced by scan_get(), and state_get().