serde-rs / serde

Serialization framework for Rust
https://serde.rs/
Apache License 2.0
8.82k stars 748 forks source link

Confusing error message when deserializing a simple enum from JSON #2702

Open avandecreme opened 4 months ago

avandecreme commented 4 months ago

With a simple enum type like this one:

#[derive(Debug, Deserialize)]
enum Foo {
    Bar, Baz,
}

If the json passed does not have the correct type, the error message is confusing. For example:

    let bool_foo = serde_json::from_str::<Foo>("true");
    println!("Bool foo: {:?}", bool_foo);
   // This prints: Bool foo: Err(Error("expected value", line: 1, column: 1))

I would expect the error message to tell that the type is a boolean instead of the expected string.

See the following playground to reproduce together with a custom Deserialize implementation which behave more like I would expect: https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=be6d3c0aa282ae7ee1bf5d9748709f4b