Skip to content
CryoCryo home
Stdlibjson

parser

import std::json::parser; · source

Free Functions

function parse(text: Str) -> Result<JsonValue, JsonError>;

Parse a complete JSON document. Returns the parsed value on success, or a JsonError pointing at the first byte that failed to parse. Trailing whitespace is allowed; trailing non-whitespace produces JsonErrorKind::TrailingData.

The parser is single-pass recursive descent over a borrowed Str, and it is RFC 8259 strict: no trailing commas, no unquoted keys, no comments. String literals decode \X and \uXXXX escapes, and UTF-16 surrogate pairs are recombined; a lone surrogate is InvalidUnicode.

MAX_DEPTH caps combined object and array nesting at 256. The default is generous for hand-authored data and tight enough that a malicious megabyte of [[[...]]] cannot exhaust the stack.

The Parser struct behind parse is public but is an implementation detail; drive the free function.

Constants

const MAX_DEPTH: u64 = 256;
const HT:        u8  = 9;
const LF:        u8  = 10;
const CR:        u8  = 13;
const SPACE:     u8  = 32;
const QUOTE:     u8  = 34;
const PLUS:      u8  = 43;
const COMMA:     u8  = 44;
const MINUS:     u8  = 45;
const DOT:       u8  = 46;
const ZERO:      u8  = 48;
const NINE:      u8  = 57;
const COLON:     u8  = 58;
const UPPER_A:   u8  = 65;
const UPPER_E:   u8  = 69;
const UPPER_F:   u8  = 70;
const LBRACK:    u8  = 91;
const BACKSLASH: u8  = 92;
const RBRACK:    u8  = 93;
const LOWER_A:   u8  = 97;
const LOWER_B:   u8  = 98;
const LOWER_E:   u8  = 101;
const LOWER_F:   u8  = 102;
const LOWER_L:   u8  = 108;
const LOWER_N:   u8  = 110;
const LOWER_R:   u8  = 114;
const LOWER_S:   u8  = 115;
const LOWER_T:   u8  = 116;
const LOWER_U:   u8  = 117;
const LBRACE:    u8  = 123;
const RBRACE:    u8  = 125;