Ambiguous Error message when deserializing adjacently tagged enum
Author: bentheiiiCreated Feb 18, 2025Updated Sep 14, 2026
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)Source: serde-rs/serde