pub struct Error { /* private fields */ }Expand description
Why a document was rejected, and where.
Every error carries the byte offset of the first byte that made the input invalid. For a syntax error that is the first byte at which the input stops being the beginning of any valid JSON text, so the offset points at the mistake itself, not at the start of the value that contains it. When the input ends too early the offset is the input’s length.
A few errors point at a byte that starts something larger: a lone
surrogate at the backslash of its \u escape, a duplicate key at the
opening quote of its second occurrence, and the budget errors at the start
of the value that exceeded the budget. ErrorKind says which applies.
§Examples
use jsonx_lib::{parse, ErrorKind};
let err = parse(r#"{"a": 1,}"#).unwrap_err();
assert_eq!(err.kind(), ErrorKind::TrailingComma);
assert_eq!(err.offset(), 8);
assert_eq!(err.to_string(), "trailing comma before `}` or `]` at byte 8");Implementations§
Source§impl Error
impl Error
Source§impl Error
impl Error
Sourcepub fn line_col(&self, input: impl AsRef<[u8]>) -> (usize, usize)
pub fn line_col(&self, input: impl AsRef<[u8]>) -> (usize, usize)
The error’s line and column in input (the text that was parsed),
both counted from 1, the column in characters: for a message a person
reads. A line ends at \n, \r\n, or \r.
§Examples
let text = "{\n \"a\": tru\n}";
let err = jsonx_lib::parse(text).unwrap_err();
assert_eq!(err.line_col(text), (2, 11));Sourcepub fn utf16_position(&self, input: impl AsRef<[u8]>) -> (usize, usize)
pub fn utf16_position(&self, input: impl AsRef<[u8]>) -> (usize, usize)
The error’s position in input as the Language Server Protocol
counts it: line and character from 0, the character in UTF-16 code
units (a character above U+FFFF counts two). A line ends at \n,
\r\n, or \r.
§Examples
let text = "[\"\u{1f600}\", tru]";
let err = jsonx_lib::parse(text).unwrap_err();
assert_eq!(err.offset(), 12);
assert_eq!(err.utf16_position(text), (0, 10)); // the emoji is two unitsTrait Implementations§
impl Copy for Error
impl Eq for Error
Source§impl Error for Error
impl Error for Error
1.30.0 · Source§fn source(&self) -> Option<&(dyn Error + 'static)>
fn source(&self) -> Option<&(dyn Error + 'static)>
1.0.0 · Source§fn description(&self) -> &str
fn description(&self) -> &str
use the Display impl or to_string()