parser
import std::json::parser; · source
Free Functions
function parse(text: Str) -> Result<JsonValue, JsonError>;
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;const NAME: Type = value;At module level, a true compile-time constant, conventionally SCREAMING_SNAKE_CASE. (mut at module level declares mutable global state instead.)
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;import a::b;
import a::b::{ X, Y };
import a::b as c;
import a::b::*;Brings a module, or selected items from it, into scope. One path per declaration: use the brace form for several items, as to alias, and * for everything public (prefer the brace form; wildcards invite name collisions).
function name<T>(param: Type) -> Ret { ... }Declares a function. Parameters are written name: Type and are never inferred; the return type follows -> and defaults to void. Declaration order does not matter: every signature is collected before any body is checked, so functions may call each other freely and recurse.
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.
(parameter) text: Strtype struct Str {
bytes: u8*;
length: u64;
}Str: a borrowed UTF-8 string slice.
std::collections::strtype enum JsonValue {
Null;
Bool(boolean);
Number(JsonNumber);
String(String);
Array(Array<JsonValue>);
Object(JsonObject);
}One JSON value. Recursive through Array and Object.
type struct JsonError {
kind: JsonErrorKind;
line: u64;
column: u64;
offset: u64;
msg: String;
}type enum JsonErrorKind {
UnexpectedEof;
UnexpectedChar;
InvalidNumber;
InvalidEscape;
InvalidUnicode;
DepthExceeded;
TrailingData;
KeyNotString;
OutOfMemory;
}Closed set of failure kinds the JSON parser produces. New kinds are rare; most failures fold into one of these.
std::json::errorJsonErrorKind::InvalidUnicodeA \uXXXX sequence is malformed or names an unpaired surrogate.
const MAX_DEPTH: u64 = 256;type struct Parser {
src: u8*;
len: u64;
pos: u64;
line: u64;
column: u64;
depth: u64;
}Recursive-descent JSON parser.
std::json::parserfunction 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.