Enum JsonParseError

Source
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:

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.

§position: usize

Byte position in the input string where the unexpected end occurred.

§

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: JsonValueKind

Kind of JSON value that was successfully parsed before the trailing characters.

§position: usize

Byte position in the input string where the non-whitespace trailing characters begin.

§

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.

§position: usize

Byte position in the input string where the unexpected character was found.

§

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.

Fields

§kind: JsonValueKind

Kind of JSON value that failed validation.

§position: usize

Byte position in the input string where the invalid JSON value starts.

§error: Box<dyn Send + Sync + Error>

Error reason that describes why the value is invalid.

Implementations§

Source§

impl JsonParseError

Source

pub fn invalid_value<E>(value: RawJsonValue<'_, '_>, error: E) -> JsonParseError
where E: Into<Box<dyn Send + Sync + Error>>,

Source

pub fn kind(&self) -> Option<JsonValueKind>

Returns the kind of JSON value associated with the error.

Source

pub fn position(&self) -> usize

Returns the byte position in the input string where the error occurred.

Source

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.

Source

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.

Trait Implementations§

Source§

impl Debug for JsonParseError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for JsonParseError

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Error for JsonParseError

Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0: use the Display impl or to_string()
1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0: replaced by Error::source, which can support downcasting
Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more
Source§

impl From<Infallible> for JsonParseError

Source§

fn from(_value: Infallible) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.