mirror of
https://github.com/serde-rs/json.git
synced 2025-10-03 07:46:05 +00:00
Merge pull request #1106 from serde-rs/invalidvalue
Handle Unexpected::Unit in Error::invalid_value
This commit is contained in:
commit
107c2d1c42
29
src/error.rs
29
src/error.rs
@ -438,11 +438,20 @@ impl de::Error for Error {
|
|||||||
|
|
||||||
#[cold]
|
#[cold]
|
||||||
fn invalid_type(unexp: de::Unexpected, exp: &dyn de::Expected) -> Self {
|
fn invalid_type(unexp: de::Unexpected, exp: &dyn de::Expected) -> Self {
|
||||||
if let de::Unexpected::Unit = unexp {
|
Error::custom(format_args!(
|
||||||
Error::custom(format_args!("invalid type: null, expected {}", exp))
|
"invalid type: {}, expected {}",
|
||||||
} else {
|
JsonUnexpected(unexp),
|
||||||
Error::custom(format_args!("invalid type: {}, expected {}", unexp, exp))
|
exp,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[cold]
|
||||||
|
fn invalid_value(unexp: de::Unexpected, exp: &dyn de::Expected) -> Self {
|
||||||
|
Error::custom(format_args!(
|
||||||
|
"invalid value: {}, expected {}",
|
||||||
|
JsonUnexpected(unexp),
|
||||||
|
exp,
|
||||||
|
))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -453,6 +462,18 @@ impl ser::Error for Error {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
struct JsonUnexpected<'a>(de::Unexpected<'a>);
|
||||||
|
|
||||||
|
impl<'a> Display for JsonUnexpected<'a> {
|
||||||
|
fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||||
|
if let de::Unexpected::Unit = self.0 {
|
||||||
|
formatter.write_str("null")
|
||||||
|
} else {
|
||||||
|
Display::fmt(&self.0, formatter)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Parse our own error message that looks like "{} at line {} column {}" to work
|
// Parse our own error message that looks like "{} at line {} column {}" to work
|
||||||
// around erased-serde round-tripping the error through de::Error::custom.
|
// around erased-serde round-tripping the error through de::Error::custom.
|
||||||
fn make_error(mut msg: String) -> Error {
|
fn make_error(mut msg: String) -> Error {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user