JsonParseError variantv0.1.0

Source
Why parsing stopped, and where.
pub variant JsonParseError {
    Lexical(JsonLexError, uint),
    Unexpected(uint),
    Truncated(uint),
    TrailingContent(uint),
    DuplicateName(uint),
    TooDeep(uint),
    TooManyEntries(uint),
    OutOfMemory(uint),
    BadSurrogate(uint),
    Source(uint)
}

Failure-only: there is no case meaning nothing went wrong, because a parse that did not fail hands back a document instead. Every case carries the zero-based byte offset into the input where the parser stopped, which is the first thing anybody debugging a document asks for and used to live in a separate structure that a caller had to remember to read.

Cases

NameDescription
Lexical(JsonLexError, uint)The lexer refused the input, for the reason carried. Nested rather than reported beside this, so a caller holding the failure cannot be holding half of it.
Unexpected(uint)A token that cannot appear where it did.
Truncated(uint)The document ended in the middle of a value.
TrailingContent(uint)Text after the end of the top-level value. The offset is where that text begins.
DuplicateName(uint)An object contained the same name twice, under the rejecting policy. The offset is the second occurrence.
TooDeep(uint)Containers nested deeper than the limit allowed.
TooManyEntries(uint)A container held more entries than the limit allowed.
OutOfMemory(uint)The allocator refused.
BadSurrogate(uint)A \u escape naming half of a surrogate pair with no valid partner. JSON's escapes are UTF-16, and a lone surrogate is not a character; the offset is the escape that has no partner.
Source(uint)The stream a JsonEventReader reads from failed with something other than its end. Only a streaming reader reports this; the source's own IoError is kept on the reader, through JsonEventReader::SourceError.

Implements ParseFailure

JsonParseError conforms to ParseFailure.

Offset

pub func Offset(self: &JsonParseError) -> uint

Where in the input parsing stopped. Borrows the failure and leaves it untouched. Cannot fail; runs in constant time and allocates nothing.

Returns: the zero-based byte offset into the document

Source

Implements Display

JsonParseError conforms to Display.

WriteDisplay

pub func WriteDisplay(
    self: &JsonParseError,
    writer: &var TextWriter,
    spec: FormatSpec
) -> ! FormatError

Writes the description of why parsing stopped, without the position. The offset is a number a caller renders where it makes sense for them — beside a line and column they computed, or not at all — so it is not written into the middle of a sentence here. Debug carries it. Reports whatever the writer reported, and UnsupportedRequest for any style or precision.

NameTypeDescription
writer&var TextWriterthe destination
specFormatSpecwhat the placeholder asked for

Returns: success when the whole description was accepted

Source

Implements Debug

JsonParseError conforms to Debug.

WriteDebug

pub func WriteDebug(
    self: &JsonParseError,
    writer: &var TextWriter,
    spec: FormatSpec
) -> ! FormatError

Names the case and its position: JsonParseError::Unexpected(12). A lexical failure names both, so nothing about it has to be looked up elsewhere: JsonParseError::Lexical(BadEscape, 12). Reports whatever the writer reported, and UnsupportedRequest for any style or precision.

NameTypeDescription
writer&var TextWriterthe destination
specFormatSpecwhat the placeholder asked for

Returns: success when the whole name was accepted

Source