Skip to main content

Module json_record

Module json_record 

Source
Expand description

Compact JSON records read and written with the incumbent’s JSON semantics. What: Reading and writing the compact JSON records both wrappers share, with the incumbent’s JSON.parse and JSON.stringify semantics. Why: Lock, owner, journal and capture records written by either wrapper are read by the other. A record must be read the way the incumbent reads it (numbers are doubles, a whole number in safe-integer range is an integer, the last duplicate key wins) and written with the same field order and escaping.

In TS you’d write (pseudocode):

const value: unknown = JSON.parse(text); const bytes = `${JSON.stringify(record)}\n`;

Structs§

ObjectWriter
What: Builder of one compact JSON object in insertion order, as JSON.stringify writes it. A struct groups named fields; this one holds the text written so far. Why: Records must list their fields in the incumbent’s order so the bytes match for the same values; escaping comes from serde_json, which escapes the same characters JSON.stringify does.

Constants§

MAX_SAFE_INTEGER
The largest whole number a double holds exactly, JavaScript’s Number.MAX_SAFE_INTEGER.

Functions§

decode_fatal
What: Decode record bytes the way a fatal TextDecoder does: valid UTF-8 only, with one leading byte-order mark removed. Why: The incumbent decodes owner and journal records with new TextDecoder('utf-8', { fatal: true }), which rejects invalid bytes and drops a leading mark.
number_equals
What: Whether a field holds exactly the JSON number expected. Why: value.schemaVersion !== 1 compares doubles, so 1.0 matches 1.
parse_object
What: Parse record text into a JSON object, or nothing for any other document. Option<Map<String, Value>> is “an object, or nothing”. Why: Every record is an object; an array, a scalar or text that is not JSON is malformed.
quote
What: Quote text as a JSON string literal. Why: serde_json escapes ", \ and control characters as JSON.stringify does; serializing a &str cannot fail.
safe_integer
What: The safe-integer reading of one JSON value. Why: Shared by object fields and array items.
safe_integer_field
What: Read a field as a JavaScript safe integer, or nothing. i64 is a signed 64-bit integer. Why: typeof value === 'number' && Number.isSafeInteger(value) accepts 1, 1.0 and 1e0 alike, because JavaScript has only doubles; anything with a fraction or beyond 2^53 - 1 is refused.
string_array
What: A JSON array of text items. Why: Pathspec and path lists in journal records.
string_field
What: Read a field as text, or nothing when it is absent or not a string. Why: typeof value.key === 'string' is the incumbent’s check.