{"id":1183,"hash":"426958ba4ba6a1fa9c7b2c59ac296845269036a1655e8ca271c64a7bc26da7c8","pattern":"How can I ignore extra tuple items when deserializing with Serde? (&quot;trailing characters&quot; error)","full_message":"Serde ignores unknown named fields when deserializing into regular structs. How can I similarly ignore extra items when deserializing into tuple structs (e.g. from a heterogeneous JSON array)?\n\nFor example, this code ignores the extra \"c\" field just fine:\n\n#[derive(Serialize, Deserialize, Debug)]\npub struct MyStruct { a: String, b: i32 }\n\nfn test_deserialize() -> MyStruct {\n    ::serde_json::from_str::<MyStruct>(r#\"\n    {\n        \"a\": \"foo\",\n        \"b\": 123,\n        \"c\": \"ignore me\"\n    }\n    \"#).unwrap()\n}\n// => MyStruct { a: \"foo\", b: 123 }\n\nBy contrast, this fails on the extra item in the tuple:\n\n#[derive(Serialize, Deserialize, Debug)]\npub struct MyTuple(String, i32);\n\nfn test_deserialize_tuple() -> MyTuple {\n    ::serde_json::from_str::<MyTuple>(r#\"\n        [\n            \"foo\",\n            123,\n            \"ignore me\"\n        ]\n    \"#).unwrap()\n}\n// => Error(\"trailing characters\", line: 5, column: 13)\n\nI'd like to allow extra items for forward compatibility in my data format. What's the easiest way to get Serde to ignore extra tuple items when deserializing?","ecosystem":"cargo","package_name":"serde","package_version":null,"solution":"You can implement a custom Visitor which ignores rest of the sequence. Be aware that the whole sequence must be consumed. This is an important part (try to remove it and you'll get same error):\n\n// This is very important!\nwhile let Some(IgnoredAny) = seq.next_element()? {\n    // Ignore rest\n}\n\nHere's a working example:\n\nuse std::fmt;\n\nuse serde::de::{self, Deserialize, Deserializer, IgnoredAny, SeqAccess, Visitor};\nuse serde::Serialize;\n\n#[derive(Serialize, Debug)]\npub struct MyTuple(String, i32);\n\nimpl<'de> Deserialize<'de> for MyTuple {\n    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>\n    where\n        D: Deserializer<'de>,\n    {\n        struct MyTupleVisitor;\n\n        impl<'de> Visitor<'de> for MyTupleVisitor {\n            type Value = MyTuple;\n\n            fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {\n                formatter.write_str(\"struct MyTuple\")\n            }\n\n            fn visit_seq<V>(self, mut seq: V) -> Result<Self::Value, V::Error>\n            where\n                V: SeqAccess<'de>,\n            {\n                let s = seq\n                    .next_element()?\n                    .ok_or_else(|| de::Error::invalid_length(0, &self))?;\n                let n = seq\n                    .next_element()?\n                    .ok_or_else(|| de::Error::invalid_length(1, &self))?;\n\n                // This is very important!\n                while let Some(IgnoredAny) = seq.next_element()? {\n                    // Ignore rest\n                }\n\n                Ok(MyTuple(s, n))\n            }\n        }\n\n        deserializer.deserialize_seq(MyTupleVisitor)\n    }\n}\n\nfn main() {\n    let two_elements = r#\"[\"foo\", 123]\"#;\n    let three_elements = r#\"[\"foo\", 123, \"bar\"]\"#;\n\n    let tuple: MyTuple = serde_json::from_str(two_elements).unwrap();\n    assert_eq!(tuple.0, \"foo\");\n    assert_eq!(tuple.1, 123);\n\n    let tuple: MyTuple = serde_json::from_str(three_elements).unwrap();\n    assert_eq!(tuple.0, \"foo\");\n    assert_eq!(tuple.1, 123);\n}","confidence":0.95,"source":"stackoverflow","source_url":"https://stackoverflow.com/questions/57529456/how-can-i-ignore-extra-tuple-items-when-deserializing-with-serde-trailing-cha","votes":11,"created_at":"2026-04-19T04:52:32.442686+00:00","updated_at":"2026-04-19T04:52:32.442686+00:00"}