logos.mem.encoding.json

Module mem · package logos-mem

Types

enumJson

enum Json

Represents a parsed JSON value (RFC 8259 AST).

Variants

Array(Vec<Json>)
Bool(bool)
Null
Number(f64)
Object(Vec<JsonField>)
Str(String)

structJsonError

struct JsonError

Describes a JSON parse failure: what went wrong and where.

Fields

message: String

Human-readable error description.

offset: i64

Byte offset into the source where the error was detected.

structJsonField

struct JsonField

Represents one key/value member of a JSON object.

Fields

key: String

Member key, with string escapes already decoded.

val: Json

Member value.

structJsonWriter

struct JsonWriter

Structured builder that emits well-formed JSON into an owned String. Commas and object key/value alternation are handled automatically via a fixed 64-frame container stack (root frame included), capping nesting at 63 containers.

Fields

buf: String
depth: i32
object_expecting_key: [bool; 64]
stack_count: [i32; 64]
stack_kind: [u8; 64]

Methods

fn append_into(self: &JsonWriter, out: &mut String) -> void

Appends the emitted JSON bytes to out.

fn as_str(self: &JsonWriter) -> *const u8

Returns a raw pointer to the emitted bytes; valid for len() bytes until the next write.

fn begin_array(self: &mut JsonWriter) -> void

Opens a JSON array, emitting [ and any needed comma separator.

fn begin_object(self: &mut JsonWriter) -> void

Opens a JSON object, emitting { and any needed comma separator.

fn end_array(self: &mut JsonWriter) -> void

Closes the current array with ].

fn end_object(self: &mut JsonWriter) -> void

Closes the current object with }.

fn len(self: &JsonWriter) -> i64

Returns the number of bytes emitted so far.

fn new() -> JsonWriter

Creates an empty writer at root depth.

fn write_bool(self: &mut JsonWriter, v: bool) -> void

Writes true or false as the next value.

fn write_i64(self: &mut JsonWriter, n: i64) -> void

Writes n as a JSON number.

fn write_key(self: &mut JsonWriter, k: &[u8]) -> void

Writes an object key (quoted, escaped) followed by :, with a preceding comma if needed. Must be called before each value inside an object.

fn write_null(self: &mut JsonWriter) -> void

Writes the literal null as the next value.

fn write_raw(self: &mut JsonWriter, s: &[u8]) -> void

Writes a caller-supplied raw JSON fragment verbatim (e.g. a pre-formatted number). No validation — a malformed fragment corrupts the output.

fn write_str(self: &mut JsonWriter, s: &[u8]) -> void

Writes s as a quoted, escaped JSON string value.

fn write_u64(self: &mut JsonWriter, n: u64) -> void

Writes n as a JSON number.

structParser

struct Parser

Tracks the byte cursor over the source during recursive-descent parsing. All fields and helpers are private; use the free parse function instead.

Fields

len: i64
pos: i64
src: *const u8

Functions

fnjson_escape_str

fn json_escape_str(s: &[u8], out: &mut String) -> void

Appends a JSON string literal (quoted, escaped) encoding s to out.

fnjson_escape_string

fn json_escape_string(src: *const u8, len: i64, out: &mut String) -> void

Appends a JSON string literal (including surrounding double quotes) encoding the bytes [src, src+len) to out. Non-ASCII bytes pass through as-is (valid per RFC 8259 for UTF-8 input).

fnjson_field_index

fn json_field_index(fields: &Vec<JsonField>, key: &[u8]) -> i64

Returns the index of the first field whose key equals key, or -1 if absent. Linear search.

fnjson_unescape_string

fn json_unescape_string(src: *const u8, len: i64, out: &mut String) -> i64

Decodes the JSON string literal starting at src (must point at the opening "), appending the decoded bytes to out. Returns bytes consumed including both quotes, or -1 on malformed input — in which case out may already hold a partial decode.

fnjson_validate

fn json_validate(src: *const u8, len: i64) -> i64

Returns the byte length of the first valid JSON value in [src, src+len), after skipping leading whitespace; -1 on malformed input. Purely syntactic; trailing bytes after the value are ignored.

fnjson_validate_str

fn json_validate_str(s: &[u8]) -> i64

Returns the byte length of the first valid JSON value in s (leading whitespace skipped); -1 on malformed input.

fnparse

fn parse(src: &[u8]) -> Result

Parses src as a single complete JSON document into a Json AST. Errors carry the byte offset of the failure; trailing non-whitespace content is an error. Strings support ASCII escapes only — \uXXXX sequences are rejected as invalid.