code wiki / (root) / json_emit.nx

json_emit.nx

buildroot/runtime/json_emit.nx

9701 B273 linesdepth 3pulls 3 transitivereach 184 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 · 21 importers

syscalls.nx json_emit.nx nx_behaveprobe.nx nx_behaveprobe_evidence_candidate_ nx_behaveprobe_review_candidate_t3 nx_bundle_ingest.nx nx_bundle_ingest_cli_candidate_t14 nx_cms_api.nx nx_compare_growth_json.nx nx_compare_growth_json_20260910.nx nx_compare_growth_json_owner_gate_ nx_comparestale_dependency_t138.nx

diagram shows first 10 each side; +0 more imports, +11 more importers in the complete lists below.

imports: syscalls.nx

imported by: nx_behaveprobe.nxnx_behaveprobe_evidence_candidate_t297.nxnx_behaveprobe_review_candidate_t371.nxnx_bundle_ingest.nxnx_bundle_ingest_cli_candidate_t144.nxnx_cms_api.nxnx_compare_growth_json.nxnx_compare_growth_json_20260910.nxnx_compare_growth_json_owner_gate_20260910.nxnx_comparestale_dependency_t138.nxnx_comparestale_edge_lib_t139.nxnx_comparestale_lib.nxnx_json_test.nxnx_site_publish_alpha_candidate_t343.nxnx_site_publish_boundary_candidate_t336.nxnx_site_publish_lib.nxnx_site_publish_prepare_lib_t256.nxnx_tokroi.nxnx_wflow_result.nxnx_wflow_result_candidate_t344.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 {
239func json_emit_bool(w: *JsonWriter, b: i64) -> i64 {
246func json_emit_null(w: *JsonWriter) -> i64 {
253func main() -> i64 {