serde_json_wasm/de/
errors.rs

1use alloc::string::{String, ToString};
2
3use serde::de;
4
5/// Deserialization result
6pub type Result<T> = core::result::Result<T, Error>;
7
8/// This type represents all possible errors that can occur when deserializing
9/// JSON data
10///
11/// It implements [`std::error::Error`] trait so long as either `std` or
12/// `unstable` features are enabled.  `std` is enabled by default and disabling
13/// it makes the crate `no_std`.  `unstable` makes it necessary to build code
14/// with nightly compiler.
15#[derive(Debug, PartialEq, Eq)]
16#[non_exhaustive]
17pub enum Error {
18    /// Control character (U+0000 to U+001F) found in string. Those must always be escaped.
19    ControlCharacterInString,
20
21    /// EOF while parsing a list.
22    EofWhileParsingList,
23
24    /// EOF while parsing an object.
25    EofWhileParsingObject,
26
27    /// EOF while parsing a string.
28    EofWhileParsingString,
29
30    /// EOF while parsing a JSON value.
31    EofWhileParsingValue,
32
33    /// Expected this character to be a `':'`.
34    ExpectedColon,
35
36    /// Expected a high surrogate (D800–DBFF) but found something else
37    ExpectedHighSurrogate,
38
39    /// Expected this character to be either a `','` or a `']'`.
40    ExpectedListCommaOrEnd,
41
42    /// Expected a low surrogate (DC00–DFFF) but found something else
43    ExpectedLowSurrogate,
44
45    /// Expected this character to be either a `','` or a `'}'`.
46    ExpectedObjectCommaOrEnd,
47
48    /// Expected to parse either a `true`, `false`, or a `null`.
49    ExpectedSomeIdent,
50
51    /// Expected this character to start a JSON value.
52    ExpectedSomeValue,
53
54    /// Invalid escape sequence
55    InvalidEscape,
56
57    /// Invalid number.
58    InvalidNumber,
59
60    /// Invalid type
61    InvalidType,
62
63    /// Invalid unicode code point.
64    InvalidUnicodeCodePoint,
65
66    /// Object key is not a string.
67    KeyMustBeAString,
68
69    /// Found a lone surrogate, which can exist in JSON but cannot be encoded to UTF-8
70    LoneSurrogateFound,
71
72    /// JSON has non-whitespace trailing characters after the value.
73    TrailingCharacters,
74
75    /// JSON has a comma after the last value in an array or map.
76    TrailingComma,
77
78    /// JSON is nested too deeply, exceeded the recursion limit.
79    RecursionLimitExceeded,
80
81    /// Custom error message from serde
82    Custom(String),
83}
84
85impl de::StdError for Error {}
86
87impl de::Error for Error {
88    fn custom<T: core::fmt::Display>(msg: T) -> Self {
89        Error::Custom(msg.to_string())
90    }
91}
92
93impl core::fmt::Display for Error {
94    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
95        write!(
96            f,
97            "{}",
98            match self {
99                Error::ControlCharacterInString => "Control character found in string.",
100                Error::EofWhileParsingList => "EOF while parsing a list.",
101                Error::EofWhileParsingObject => "EOF while parsing an object.",
102                Error::EofWhileParsingString => "EOF while parsing a string.",
103                Error::EofWhileParsingValue => "EOF while parsing a JSON value.",
104                Error::ExpectedColon => "Expected this character to be a `':'`.",
105                Error::ExpectedHighSurrogate => "Expected a high surrogate (D800–DBFF).",
106                Error::ExpectedListCommaOrEnd => {
107                    "Expected this character to be either a `','` or\
108                     a \
109                     `']'`."
110                }
111                Error::ExpectedLowSurrogate => "Expected a low surrogate (DC00–DFFF).",
112                Error::ExpectedObjectCommaOrEnd => {
113                    "Expected this character to be either a `','` \
114                     or a \
115                     `'}'`."
116                }
117                Error::ExpectedSomeIdent => {
118                    "Expected to parse either a `true`, `false`, or a \
119                     `null`."
120                }
121                Error::ExpectedSomeValue => "Expected this character to start a JSON value.",
122                Error::InvalidEscape => "Invalid escape sequence.",
123                Error::InvalidNumber => "Invalid number.",
124                Error::InvalidType => "Invalid type",
125                Error::InvalidUnicodeCodePoint => "Invalid unicode code point.",
126                Error::KeyMustBeAString => "Object key is not a string.",
127                Error::LoneSurrogateFound => "Found a lone surrogate, which can exist in JSON but cannot be encoded to UTF-8.",
128                Error::TrailingCharacters => {
129                    "JSON has non-whitespace trailing characters after \
130                     the \
131                     value."
132                }
133                Error::TrailingComma => "JSON has a comma after the last value in an array or map.",
134                Error::RecursionLimitExceeded => "JSON is nested too deeply, exceeded the recursion limit.",
135                Error::Custom(msg) => msg,
136            }
137        )
138    }
139}