pub struct Parser { /* private fields */ }Expand description
A configured parser: Limits and a DuplicateKeys policy.
parse is Parser::new().parse. Build a Parser to
change a budget or the duplicate-key policy; it is a small Copy value
that can be kept in a const or a struct and reused for any number of
documents.
§Examples
use jsonx_lib::{DuplicateKeys, ErrorKind, Limits, Parser};
// Messages from the network: small and shallow.
const WIRE: Parser = Parser::new()
.limits(Limits::new().with_max_input_bytes(1 << 20).with_max_depth(32))
.duplicate_keys(DuplicateKeys::Error);
assert!(WIRE.parse(r#"{"ok": true}"#).is_ok());
let deep = "[".repeat(33);
assert_eq!(WIRE.parse(&deep).unwrap_err().kind(), ErrorKind::DepthLimitExceeded);Implementations§
Source§impl Parser
impl Parser
Sourcepub fn decode<'de, T: FromJson<'de>>(
&self,
text: &'de str,
) -> Result<T, DecodeError>
pub fn decode<'de, T: FromJson<'de>>( &self, text: &'de str, ) -> Result<T, DecodeError>
Reads text as one T with this parser’s budgets and duplicate-key
policy; see from_str.
§Errors
As from_str.
§Examples
use jsonx_lib::{ErrorKind, Limits, Parser};
let shallow = Parser::new().limits(Limits::new().with_max_depth(1));
assert_eq!(shallow.decode::<Vec<u8>>("[1]")?, [1]);
let err = shallow.decode::<Vec<Vec<u8>>>("[[1]]").unwrap_err();
assert_eq!(err.syntax_error().map(|e| e.kind()), Some(ErrorKind::DepthLimitExceeded));Sourcepub fn decode_bytes<'de, T: FromJson<'de>>(
&self,
bytes: &'de [u8],
) -> Result<T, DecodeError>
pub fn decode_bytes<'de, T: FromJson<'de>>( &self, bytes: &'de [u8], ) -> Result<T, DecodeError>
Reads bytes, which must be UTF-8, as one T; see
decode.
§Errors
As from_bytes.
§Examples
assert_eq!(jsonx_lib::Parser::new().decode_bytes::<&str>(b"\"x\"")?, "x");Source§impl Parser
impl Parser
Sourcepub const fn new() -> Self
pub const fn new() -> Self
A parser with the default Limits and DuplicateKeys::Error.
§Examples
assert_eq!(jsonx_lib::Parser::new().parse("[]")?.to_string(), "[]");Sourcepub const fn limits(self, limits: Limits) -> Self
pub const fn limits(self, limits: Limits) -> Self
Replaces the budgets.
§Examples
use jsonx_lib::{ErrorKind, Limits, Parser};
let p = Parser::new().limits(Limits::new().with_max_string_bytes(3));
assert_eq!(p.parse(r#""four""#).unwrap_err().kind(), ErrorKind::StringTooLong);Sourcepub const fn duplicate_keys(self, policy: DuplicateKeys) -> Self
pub const fn duplicate_keys(self, policy: DuplicateKeys) -> Self
Sets the duplicate-key policy.
§Examples
use jsonx_lib::{DuplicateKeys, Parser};
let p = Parser::new().duplicate_keys(DuplicateKeys::Last);
assert_eq!(p.parse(r#"{"k": 1, "k": 2}"#)?["k"], 2);Sourcepub const fn get_numbers(&self) -> Numbers
pub const fn get_numbers(&self) -> Numbers
Sourcepub const fn get_limits(&self) -> Limits
pub const fn get_limits(&self) -> Limits
Sourcepub const fn get_duplicate_keys(&self) -> DuplicateKeys
pub const fn get_duplicate_keys(&self) -> DuplicateKeys
The parser’s duplicate-key policy.
§Examples
use jsonx_lib::{DuplicateKeys, Parser};
assert_eq!(Parser::new().get_duplicate_keys(), DuplicateKeys::Error);Sourcepub fn parse<'a>(&self, text: &'a str) -> Result<Value<'a>, Error>
pub fn parse<'a>(&self, text: &'a str) -> Result<Value<'a>, Error>
Parses one JSON document.
The whole input must be exactly one value, with optional whitespace
around it (space, tab, line feed, carriage return; nothing else, not
even a byte-order mark). Strings without escapes and all numbers borrow
from text.
§Errors
The first syntax error or exceeded budget, at its byte offset; see
ErrorKind for every case.
§Examples
use jsonx_lib::{ErrorKind, Parser};
let v = Parser::new().parse(" [1, \"two\"] ")?;
assert_eq!(v[1], "two");
let err = Parser::new().parse("[1] [2]").unwrap_err();
assert_eq!((err.kind(), err.offset()), (ErrorKind::TrailingCharacters, 4));Sourcepub fn parse_bytes<'a>(&self, bytes: &'a [u8]) -> Result<Value<'a>, Error>
pub fn parse_bytes<'a>(&self, bytes: &'a [u8]) -> Result<Value<'a>, Error>
Parses one JSON document from bytes, which must be UTF-8 (RFC 8259 §8.1). The size budget is checked first, then the encoding, then the syntax.
§Errors
ErrorKind::InputTooLarge, then ErrorKind::InvalidUtf8 at the
first byte of the first invalid sequence, then as
parse.
§Examples
use jsonx_lib::{ErrorKind, Parser};
assert_eq!(Parser::new().parse_bytes(b"[\"\xC3\xA9\"]")?[0], "\u{e9}");
let err = Parser::new().parse_bytes(b"[\"\xC3\"]").unwrap_err();
assert_eq!((err.kind(), err.offset()), (ErrorKind::InvalidUtf8, 2));Sourcepub fn reader<'a>(&self, text: &'a str) -> Reader<'a> ⓘ
pub fn reader<'a>(&self, text: &'a str) -> Reader<'a> ⓘ
A Reader over text with this parser’s budgets and duplicate-key
policy (which Reader::read_value applies).
§Examples
use jsonx_lib::{ErrorKind, Limits, Parser};
let mut r = Parser::new().limits(Limits::new().with_max_depth(1)).reader("[[]]");
assert!(r.next_event().is_ok());
assert_eq!(r.next_event().unwrap_err().kind(), ErrorKind::DepthLimitExceeded);Sourcepub fn reader_bytes<'a>(&self, bytes: &'a [u8]) -> Result<Reader<'a>, Error>
pub fn reader_bytes<'a>(&self, bytes: &'a [u8]) -> Result<Reader<'a>, Error>
A Reader over bytes, which must be UTF-8. The size budget and the
encoding are checked here, before any event is read.
§Errors
ErrorKind::InputTooLarge or ErrorKind::InvalidUtf8.
§Examples
use jsonx_lib::{Event, Parser};
let mut r = Parser::new().reader_bytes(b"null")?;
assert_eq!(r.next_event()?, Some(Event::Null));Source§impl Parser
impl Parser
Sourcepub fn stream_reader(&self) -> StreamReader
pub fn stream_reader(&self) -> StreamReader
A StreamReader with this parser’s budgets and duplicate-key
policy (which StreamReader::read_value applies).
§Examples
use jsonx_lib::{ErrorKind, Limits, Parser};
let mut r = Parser::new().limits(Limits::new().with_max_depth(1)).stream_reader();
r.feed(b"[[]]")?;
assert!(r.next_event().is_ok());
assert_eq!(r.next_event().unwrap_err().kind(), ErrorKind::DepthLimitExceeded);