use serde::Serialize;
use serde::Deserialize;
#[derive(Serialize, Deserialize, Debug, PartialEq)]
#[serde(untagged)]
pub enum TestResponse {
Unavailable { status: String },
Error { status: String },
}
fn main() {
let given = TestResponse::Error {
status: String::from("There is an error"),
};
println!("Given: {:?}", given);
let serialized = serde_json::to_string(&given).unwrap();
println!("Serialize: {}", serialized);
let deserialized: TestResponse = serde_json::from_str(serialized.as_str()).unwrap();
println!("Deserialize: {:?}", deserialized);
assert_eq!(given, deserialized);
}
Output:
Given: Error { status: "There is an error" }
Serialize: {"status":"There is an error"}
Deserialize: Unavailable { status: "There is an error" }
Errors:
Compiling playground v0.0.1 (/playground)
Finished dev [unoptimized + debuginfo] target(s) in 0.76s
Running `target/debug/playground`
thread 'main' panicked at 'assertion failed: `(left == right)`
left: `Error { status: "There is an error" }`,
right: `Unavailable { status: "There is an error" }`', src/main.rs:26:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
My expectation should be the same with the given but Instead of Error, it chooses the Unavailable enum field. Is this a bug?