pub enum JsonParseError {
UnexpectedEos {
kind: Option<JsonValueKind>,
position: usize,
},
UnexpectedTrailingChar {
kind: JsonValueKind,
position: usize,
},
UnexpectedValueChar {
kind: Option<JsonValueKind>,
position: usize,
},
InvalidValue {
kind: JsonValueKind,
position: usize,
error: Box<dyn Send + Sync + Error>,
},
}Expand description
JSON parse error.
This enum represents various errors that can occur during JSON parsing. Each variant provides specific details about the error, including the position in the input string where the error occurred.
For generating more detailed error messages, you can use these methods:
JsonParseError::get_line()JsonParseError::get_line_and_column_numbers()RawJson::get_value_by_position()
These methods help provide context for debugging and error reporting.
Variants§
UnexpectedEos
End of string was reached unexpectedly while parsing a JSON value.
This occurs when the parser reaches the end of the input string before completing the parse of the current JSON value. For example, a string that starts with a quote but doesn’t have a closing quote.
Fields
kind: Option<JsonValueKind>Kind of JSON value that was being parsed when the unexpected end was encountered.
UnexpectedTrailingChar
Additional non-whitespace characters were found after a complete JSON value was parsed.
This happens when the input contains extra data after a valid JSON value.
For example, {"key": "value"} extra.
Fields
kind: JsonValueKindKind of JSON value that was successfully parsed before the trailing characters.
UnexpectedValueChar
An unexpected character was encountered while parsing a JSON value.
This occurs when the parser encounters a character that doesn’t match the expected syntax for the current context. For example, encountering a letter when expecting a digit in a number, or an invalid escape sequence in a string.
Fields
kind: Option<JsonValueKind>Kind of JSON value that was being parsed when the unexpected character was encountered.
InvalidValue
A JSON value was syntactically correct, but invalid according to application-specific format rules.
This happens when the JSON syntax is valid, but the value doesn’t conform to additional constraints. For example, an integer that exceeds the maximum allowed value, or a string that doesn’t match an expected pattern or format.
Implementations§
Source§impl JsonParseError
impl JsonParseError
Sourcepub fn invalid_value<E>(value: RawJsonValue<'_, '_>, error: E) -> JsonParseError
pub fn invalid_value<E>(value: RawJsonValue<'_, '_>, error: E) -> JsonParseError
Makes a JsonParseError::InvalidValue error.
Sourcepub fn kind(&self) -> Option<JsonValueKind>
pub fn kind(&self) -> Option<JsonValueKind>
Returns the kind of JSON value associated with the error.
Sourcepub fn position(&self) -> usize
pub fn position(&self) -> usize
Returns the byte position in the input string where the error occurred.
Sourcepub fn get_line_and_column_numbers(
&self,
text: &str,
) -> Option<(NonZeroUsize, NonZeroUsize)>
pub fn get_line_and_column_numbers( &self, text: &str, ) -> Option<(NonZeroUsize, NonZeroUsize)>
Returns the line and column numbers for the error position in the input text.
This method calculates the line and column numbers based on the error’s position within the provided text. This is useful for creating human-readable error messages that can pinpoint the exact location of the error.
Returns None if the position is outside the text boundaries or falls on an
invalid UTF-8 boundary.
§Note
The column value counts each character as 1 column, regardless of its
actual display width. For accurate display width calculation that accounts
for multi-width characters (like CJK characters or emoji), consider using
an external crate such as unicode-width.
Sourcepub fn get_line<'a>(&self, text: &'a str) -> Option<&'a str>
pub fn get_line<'a>(&self, text: &'a str) -> Option<&'a str>
Returns the line of text where the error occurred.
This method extracts the entire line from the input text that contains the error. This is useful for error reporting as it provides context around the error location.
Returns None if the position is outside the text boundaries.