I've read the docs, but I haven't seen a way to solve my problem. I'm parsing an API response which on success would return only a 2xx status code, while on bad requests it sends JSON.
extern crate serde;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
fn main() {
let _: X = serde_json::from_str(r#"{ "ok": true }"#).expect("with string");
let _: X = serde_json::from_str("").expect("empty string");
}
#[derive(Deserialize)]
struct X {
ok: Option<bool>,
}
This is the struct I would like to deserialize into. I've tried adding #[serde(skip_serializing_if = "some_func")] to the field, but it doesn't change anything.
The error is
Error("EOF while parsing a value", line: 1, column: 0)
Is there a way to handle this properly, or do I always have to check the status code if it's 2xx before attempting to deserialize the response?
#[serde(skip_serializing_if...)], includingsome_func? - E_net4#[serde(skip_deserializing)], not#[serde(skip_serializing)]. The latter only works when encoding the value. :) - E_net4mainfunction deserializing an example of a response reproducing the issue. This makes the question much easier to address and much easier for us to help. - E_net4