This program will error, but it will not tell you which field errored.
use serde::{Deserialize};
use serde_json;
#[derive(Deserialize)]
struct Test {
_foo: usize,
_bar: Bar,
}
#[derive(Deserialize)]
#[serde(try_from = "BarDe")]
struct Bar(usize);
#[derive(Deserialize)]
#[serde(transparent)]
struct BarDe(i32);
impl TryFrom<BarDe> for Bar {
type Error = &'static str;
fn try_from(_: BarDe) -> Result<Self, &'static str> {
Err("this field is erroneous")
}
}
fn main() {
let _: Test = serde_json::from_str(r#"{"_foo": 1, "_bar": 2}"#).unwrap();
}
I wonder if there is a pretty/succinct way to get the field name that failed to deseriliaze?
I thought about adding a #[track_caller]
to the try_from, but that gives me code location, not a helpful field name.