code wiki / (root) / json_emit.nx

json_emit.nx

buildroot/runtime/json_emit.nx

9701 B273 linesdepth 3pulls 3 transitivereach 2 importersview sourcekind tooltopic json
docsdependenciesstructsconstsfunctions

about

json_emit.nx -- RFC 8259 JSON writer (complement to json.nx parser). Used by: TLS session tickets, API responses, config serialisation, structured logs, Nishi Pages content negotiation (Accept: json). Shape: - Caller allocates an output buffer; we append into it. - A JsonWriter struct tracks position + depth + "needs comma" state so the caller doesn't manage separators manually. - Begin/end array/object and emit_* primitives for scalars. - String escaping per RFC 8259 §7 (\\, \", \/, \b, \f, \n, \r, \t, \uXXXX for < 0x20 + > 0x7E). Handles UTF-8 passthrough for BMP + astral. Invariants: JE1 Output is valid JSON for any sequence of begin/end + emit calls that matches the grammar. Callers can misuse (nest mismatched) and get garbage, but we never emit syntax errors in-pattern. JE2 String emission escapes ALL required characters per RFC 8259 §7; no silent acceptance of control bytes that would produce ambiguous output. JE3 Buffer overruns return a negative error; no silent truncation (malformed JSON tail is worse than a visible error). JE4 Emit is single-pass, no back-patching; total size can be computed up-front from the structure if needed.

dependencies 1 imports · 2 importers

syscalls.nx json_emit.nx nx_json_test.nx nxtask_main.nx

imports: syscalls.nx

imported by: nx_json_test.nxnxtask_main.nx

call flow from main pre-order; caps 40 nodes / depth 6 declared; ↻ = already shown

main json_writer_init json_begin_object je_sep je_put je_put ↻ json_emit_key je_sep ↻ je_emit_string_raw je_put ↻ je_put ↻ json_emit_string je_sep ↻ je_emit_string_raw ↻ json_emit_int je_sep ↻ je_put ↻ json_emit_bool je_sep ↻ je_emit_literal je_put ↻ json_end_object je_put ↻

structs

35struct JsonWriter {

consts

31const JE_ERR_OVERFLOW: i64 = -1
32const JE_MAX_DEPTH: i64 = 64

functions

46func json_writer_init(w: *JsonWriter, buf: *u8, cap: i64) -> i64 {
59func je_put(w: *JsonWriter, b: i64) -> i64 {
69func je_sep(w: *JsonWriter) -> i64 {
80func json_begin_object(w: *JsonWriter) -> i64 {
92func json_end_object(w: *JsonWriter) -> i64 {
99func json_begin_array(w: *JsonWriter) -> i64 {
110func json_end_array(w: *JsonWriter) -> i64 {
118func je_emit_string_raw(w: *JsonWriter, s: *u8, n: i64) -> i64 {
175func json_emit_string(w: *JsonWriter, s: *u8, n: i64) -> i64 {
186func json_emit_key(w: *JsonWriter, name: *u8, n: i64) -> i64 {
203func json_emit_int(w: *JsonWriter, n: i64) -> i64 {
229func je_emit_literal(w: *JsonWriter, lit: *u8, n: i64) -> i64 {
called by 2: json_emit_booljson_emit_null calls 1: je_put
239func json_emit_bool(w: *JsonWriter, b: i64) -> i64 {
called by 2: mainmain calls 2: je_sepje_emit_literal
246func json_emit_null(w: *JsonWriter) -> i64 {
called by 1: main calls 2: je_sepje_emit_literal
253func main() -> i64 {