Import with #import "std/json_document".

JsonKind :: enum { Missing = 0; Null = 1; False = 2; True = 3; Number = 4; String = 5; Array = 6; Object = 7; }
JsonEraser :: #type (context: *void, bytes: []u8)
JsonNoErase :: (context: *void, bytes: []u8)
JsonNode :: struct { kind: JsonKind; text: string; owned_text: Vec(u8); key: string; owned_key: Vec(u8); first: s32; last: s32; next: s32; count: s32; }
JsonDocument :: struct { nodes: Vec(JsonNode); root: s32; error: string; error_at: s64; erase: JsonEraser; erase_context: *void; }
JsonStyle :: struct { indent: s32; item_separator: string; key_separator: string; ascii: bool; sort_keys: bool; python_floats: bool; }
JsonPythonStyle :: (indent: s32, ascii: bool, sort_keys: bool, python_floats: bool) -> JsonStyle

The style of Python's json.dumps(value, indent=INDENT); a negative INDENT stands for indent=None. ASCII escapes are on, as ensure_ascii's default.

JsonCompactStyle :: (ascii: bool, sort_keys: bool) -> JsonStyle

One line with separators=(',', ':').

JsonSetEraser :: (document: *JsonDocument, erase: JsonEraser, context: *void)

Register an optional owner-supplied eraser before building/copying private text. It receives actual owned bytes, including failed partial strings. Call JsonReset before the document leaves scope; borrowed source bytes stay the caller's responsibility. No cryptographic dependency lives in JSON.

JsonReset :: (document: *JsonDocument)
JsonRead :: (document: *JsonDocument, text: string, erase: JsonEraser, erase_context: *void) -> bool

Reads TEXT as the document's root, after clearing it. False, with error and error_at set, when TEXT is not one complete JSON value.

JsonKindOf :: (document: *JsonDocument, node: s32) -> JsonKind
JsonCount :: (document: *JsonDocument, node: s32) -> s32

The number of elements or members.

JsonFirst :: (document: *JsonDocument, node: s32) -> s32

The first element or member, then each next one, then -1.

JsonNext :: (document: *JsonDocument, node: s32) -> s32
JsonKey :: (document: *JsonDocument, node: s32) -> Text

A member's name.

JsonKeyInto :: (document: *JsonDocument, node: s32, output: *Vec(u8)) -> bool
JsonMember :: (document: *JsonDocument, object: s32, key: string) -> s32

The object's last member named KEY, or -1.

JsonElement :: (document: *JsonDocument, array: s32, index: s32) -> s32

The array's element at INDEX, or -1.

JsonString :: (document: *JsonDocument, node: s32) -> Text

A string's value; "" for anything else.

JsonStringInto :: (document: *JsonDocument, node: s32, output: *Vec(u8)) -> bool
JsonIs :: (document: *JsonDocument, node: s32, value: string) -> bool

Whether NODE is the string VALUE, without copying it.

JsonNumberText :: (document: *JsonDocument, node: s32) -> Text

A number's text as written; "" for anything else.

JsonNumberTextInto :: (document: *JsonDocument, node: s32, output: *Vec(u8)) -> bool
JsonInteger :: (document: *JsonDocument, node: s32) -> (s64, bool)

A whole number in the signed 64-bit range, without a fraction or exponent. Returns false for overflow; the document retains the original number text.

JsonBool :: (document: *JsonDocument, node: s32) -> (bool, bool)
JsonNewNull :: (document: *JsonDocument) -> s32
JsonNewBool :: (document: *JsonDocument, value: bool) -> s32
JsonNewInteger :: (document: *JsonDocument, value: s64) -> s32
JsonNewNumber :: (document: *JsonDocument, text: string) -> s32

A number written as TEXT, which must be a valid JSON number.

JsonNewString :: (document: *JsonDocument, value: string) -> s32
JsonNewStringCopy :: (document: *JsonDocument, value: string) -> s32

Own VALUE when the caller's string storage will end before this document. JsonNewString above deliberately retains its borrowed-input contract.

JsonNewArray :: (document: *JsonDocument) -> s32
JsonNewObject :: (document: *JsonDocument) -> s32
JsonAppend :: (document: *JsonDocument, array: s32, child: s32) -> bool

Adds CHILD, a node not yet in any container, to the end of ARRAY.

JsonSet :: (document: *JsonDocument, object: s32, key: string, child: s32) -> bool

Sets OBJECT's member KEY to CHILD, a node not yet in any container: a present member is replaced where it stands, as a Python dict keeps its place, and a new one goes last.

JsonSetCopy :: (document: *JsonDocument, object: s32, key: string, child: s32) -> bool

Like JsonSet, with a document-owned copy of KEY. Use this when the caller's key storage will be released before the document is written.

JsonRemove :: (document: *JsonDocument, object: s32, key: string) -> bool

Removes every member named KEY; false when there was none.

JsonClear :: (document: *JsonDocument, node: s32)

Empties an array or object.

JsonCopy :: (target: *JsonDocument, source: *JsonDocument, node: s32) -> s32

A deep copy of SOURCE's NODE as a new node of TARGET, which may be the same document; returns it, or -1 for a missing node.

JsonQuoteInto :: (output: *Vec(u8), value: string, ascii: bool) -> bool

Appends VALUE as a quoted JSON string, escaped as json.dumps escapes it.

JsonQuote :: (value: string, ascii: bool) -> Text
JsonWrite :: (document: *JsonDocument, node: s32, style: JsonStyle) -> Text

NODE and everything in it as JSON text in STYLE, without a final newline.

JsonWriteInto :: (output: *Vec(u8), document: *JsonDocument, node: s32, style: JsonStyle) -> bool

Append to caller-owned storage. It must not back this document's borrowed input, or be one of its owned node buffers. Discard partial output on error. Sensitive callers start with empty/wiped storage or pre-reserve its lifetime maximum; they erase their own output and borrowed input after use.

Source ↗ · All modules