json_emit.nx
buildroot/runtime/json_emit.nx
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
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
structs
| 35 | struct JsonWriter { |
consts
| 31 | const JE_ERR_OVERFLOW: i64 = -1 |
| 32 | const JE_MAX_DEPTH: i64 = 64 |
functions
| 46 | func json_writer_init(w: *JsonWriter, buf: *u8, cap: i64) -> i64 { |
| 59 | func je_put(w: *JsonWriter, b: i64) -> i64 { |
| 69 | func je_sep(w: *JsonWriter) -> i64 {
called by 7: json_begin_objectjson_begin_arrayjson_emit_stringjson_emit_keyjson_emit_intjson_emit_bool+1 calls 1: je_put |
| 80 | func json_begin_object(w: *JsonWriter) -> i64 { |
| 92 | func json_end_object(w: *JsonWriter) -> i64 { |
| 99 | func json_begin_array(w: *JsonWriter) -> i64 { |
| 110 | func json_end_array(w: *JsonWriter) -> i64 { |
| 118 | func je_emit_string_raw(w: *JsonWriter, s: *u8, n: i64) -> i64 { |
| 175 | func json_emit_string(w: *JsonWriter, s: *u8, n: i64) -> i64 { |
| 186 | func json_emit_key(w: *JsonWriter, name: *u8, n: i64) -> i64 {
called by 9: maincms_api_emit_objmainemit_taskemit_graphmain+3 calls 3: je_sepje_emit_string_rawje_put |
| 203 | func json_emit_int(w: *JsonWriter, n: i64) -> i64 { |
| 229 | func je_emit_literal(w: *JsonWriter, lit: *u8, n: i64) -> i64 { |
| 239 | func json_emit_bool(w: *JsonWriter, b: i64) -> i64 { |
| 246 | func json_emit_null(w: *JsonWriter) -> i64 { |
| 253 | func main() -> i64 { |