Skip to content
CryoCryo home
Stdlibjson

error

import std::json::error; · source

Types

JsonError

type struct JsonError {
    kind:   JsonErrorKind;
    line:   u64;
    column: u64;
    offset: u64;
    msg:    String;

    static new(kind: JsonErrorKind, line: u64, column: u64, offset: u64, msg: String) -> JsonError;
    describe(&this) -> Str;
}

Modelled as a kind plus a position plus a message. Branch on kind for programmatic decisions; msg carries the human-readable detail.

line and column are 1-indexed, matching editor conventions; offset is a 0-indexed byte offset into the source. describe() is the standard accessor every stdlib error exposes.

Properties

PropertyTypeNotes
kindJsonErrorKind
lineu641-indexed line of the offending byte.
columnu641-indexed column of the offending byte.
offsetu640-indexed byte offset into the source.
msgStringHuman-readable detail. Owned; call drop before discarding.

Constructors

static new(kind: JsonErrorKind, line: u64, column: u64, offset: u64, msg: String) -> JsonError;

Methods

describe(&this) -> Str;

A short, stable description of what went wrong, as a borrowed Str. The convergent accessor every stdlib error type exposes (describe(&this) -> Str); Display/Debug for JsonError delegate to it. Returns the human-readable msg; the kind slug and the line/column/offset position remain available as fields/methods. Borrows through this, so the view is valid for the error's lifetime.

JsonErrorKind

type enum JsonErrorKind {
    UnexpectedEof;
    UnexpectedChar;
    InvalidNumber;
    InvalidEscape;
    InvalidUnicode;
    DepthExceeded;
    TrailingData;
    KeyNotString;
    OutOfMemory;
}

implement enum JsonErrorKind {
    label(&this) -> string;
}

The closed set of parser failures. label() gives a short diagnostic slug: "unexpected_eof", "invalid_number", and so on.

Variants
VariantNotes
UnexpectedEofReached end of input mid-value.
UnexpectedCharEncountered a byte that does not begin a valid token.
InvalidNumberA number literal could not be parsed (overflow, malformed exponent, lone -, leading zeros, etc.).
InvalidEscapeA \X escape inside a string is not one of the recognized escape sequences.
InvalidUnicodeA \uXXXX sequence is malformed or names an unpaired surrogate.
DepthExceededObject/array nesting exceeded parser::MAX_DEPTH.
TrailingDataGarbage after the top-level value.
KeyNotStringObject key was not a string.
OutOfMemoryThe parser's working buffer allocation failed (only fires on a genuine malloc returning NULL, e.g. under memory pressure).
Methods
label(&this) -> string;

Short slug for diagnostics. "unexpected_eof", "invalid_number", etc.

Trait implementations

implement trait Drop for struct JsonError
implement trait Display for struct JsonError
std::fmt::display
implement trait Debug for struct JsonError
std::fmt::display

JsonError owns its message, which is why it carries a drop.