expression

Reject unexpected JSON fields

Choose strict input parsing when unknown keys indicate a likely typo or unsupported contract. Default derived JSON structs normally ignore unknown fields, which can aid compatibility. Serde and serde_json are external crates; use the staged Cargo manifest. Expected stdout (not executed): true true Common mistake: Treating strict parsing as full security validation or combining it with flatten.

Formal Syntax & Structure

rust syntaxrust
#[serde(deny_unknown_fields)]

Working Example

Example Snippetrust
fn main() {    #[derive(serde::Deserialize)]    #[serde(deny_unknown_fields)]    struct Settings { enabled: bool }    let ok: Settings = serde_json::from_str(r#"{"enabled":true}"#).unwrap();    println!("{}", ok.enabled);    println!("{}", serde_json::from_str::<Settings>(r#"{"enabled":true,"extra":1}"#).is_err());}
Rust Reference