- deny_unknown_fields
Read JSON avoid extra fields - deny_unknown_fields
- What should happen if a new field is added to the JSON, but our code is not updated yet?
- Should we let it slide, or should we report an error?
examples/json/avoid-extra-fields/src/main.rs
use serde::Deserialize; #[derive(Deserialize, Debug)] #[serde(deny_unknown_fields)] struct Person { fname: String, lname: String, married: bool, } fn main() { let filename = get_filename(); let content = std::fs::read_to_string(filename).unwrap(); match serde_json::from_str::<Person>(&content) { Ok(data) => { println!("{:#?}", &data); assert_eq!(data.fname, "Foo"); assert_eq!(data.lname, "Bar"); assert!(data.married); } Err(err) => { eprintln!("There was an error: {err}"); //std::process::exit(1); } } println!("Still here"); } fn get_filename() -> String { let args: Vec<String> = std::env::args().collect(); if args.len() != 2 { eprintln!("Usage: {} FILENAME", args[0]); std::process::exit(1); } args[1].to_owned() }
thread 'main' panicked at src/main.rs:16:57: JSON parsing error: Error("unknown field `year`, expected one of `fname`, `lname`, `married`", line: 4, column: 10) note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace