Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ambiguous Error message when deserializing adjacently tagged enum #2893

Open
bentheiii opened this issue Feb 18, 2025 · 0 comments
Open

Ambiguous Error message when deserializing adjacently tagged enum #2893

bentheiii opened this issue Feb 18, 2025 · 0 comments

Comments

@bentheiii
Copy link

Consider the following snippet:

use serde::Deserialize;

#[derive(Deserialize)]
#[serde(tag = "t", content = "c")]
enum Block {
    Para(Vec<String>),
    Str(String),
}

fn main(){
    let inp = r#"{"t": "Para", "c": ["hi", 15]} "#;
    let _deserialized: Block = serde_json::from_str(&inp).unwrap();
}

the error message that arises from this invalid string is:

called `Result::unwrap()` on an `Err` value: Error("invalid type: integer `15`, expected a string", line: 1, column: 28)

Which is not very helpful since we don't even know which variant failed to deserialize.

I propose that the tag of the failed variant be included in the error message (this will also help disambiguated cases where the tag is provided as an integer), something like:

called `Result::unwrap()` on an `Err` value: Error("deserialzing tag `Para`: invalid type: integer `15`, expected a string", line: 1, column: 28)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant