Silk

silk/json_reader

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc.

Allocation-free JSON token reading from a fixed-capacity buffered input.

When to use

Use JsonReader when a JSON document does not fit in one input slice. Use silk.json_scanner.JsonScanner when the complete document is available.

Details

Each call to next returns one validated token. Raw string, number, and literal bytes use caller-owned scratch storage. The reader keeps no input or scratch view between calls.

Gotchas

A raw token longer than scratch fails with JsonReason.TokenTooLarge. The reader becomes terminal after a JSON, buffer, or capacity failure. Drop each token view before the next call.

Import as JsonReader with import silk.json_reader { JsonReader }.

Public declarations: 2.

JsonReadToken

pub struct JsonReadToken<'scratch>

One validated token and its raw bytes in caller-owned scratch storage.

Details

raw includes quotes and escapes for strings. Punctuation and end-of-input have an empty raw slice. The view remains valid only while its scratch borrow is held.

Field token

pub token: JsonToken

The token with absolute document offsets.

Field raw

pub raw: &'scratch [u8]

The raw source spelling for a string, number, or literal token.

JsonReader

pub struct JsonReader

Owned state for one JSON document read from BufferedInput.

Details

This state keeps no input or scratch reference. It retains scanner progress across input fills. A completed document returns sticky end-of-input.

Associated function JsonReader.make

pub fn make() -> JsonReader

Creates an empty reader for one JSON document without allocation.

Method JsonReader.next

pub effect<'scratch & 'life1 & 'life2> fn next<'scratch, 'life1, 'life2>(self: &'life1 mut silk/json_reader.JsonReader, input: &'life2 mut silk/buffered_input.BufferedInput, scratch: &'scratch mut [u8], deadline: silk/option.Option<silk/system_clock.Instant>) -> silk/json_reader.JsonReadToken<'scratch> ! JsonError | BufferError ? &mut ByteDuplex | &mut MonotonicClock

Returns the next token with raw bytes copied into caller scratch.

Details

The token view borrows only scratch. A number at an input boundary waits for its delimiter. The delimiter stays in input for the next call. Whitespace uses no scratch space.

Gotchas

If scratch is too small, JsonReason.TokenTooLarge reports the token's first byte and the available capacity. JSON and buffer failures also make the reader terminal. End is sticky.

On this page