Read complex JSON
examples/json/read-person/src/main.rs
use serde::Deserialize; #[derive(Deserialize, Debug)] #[serde(deny_unknown_fields)] #[allow(dead_code)] struct Child { name: String, birthdate: u32, } #[derive(Deserialize, Debug)] #[serde(deny_unknown_fields)] #[allow(dead_code)] struct Person { fname: String, lname: String, year: u32, height: f32, married: bool, numbers: Vec<u32>, children: Vec<Child>, } fn main() { let filename = get_filename(); let content = std::fs::read_to_string(filename).unwrap(); let person = serde_json::from_str::<Person>(&content).unwrap(); println!("person = {:#?}", person); assert!(person.fname == "Foo"); assert!(person.numbers[0] == 23); assert!(person.children[0].name == "Alpha"); } 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() }
person = Person { fname: "Foo", lname: "Bar", year: 1992, height: 178.2, married: true, numbers: [ 23, 19, 42, ], children: [ Child { name: "Alpha", birthdate: 2020, }, Child { name: "Beta", birthdate: 2022, }, ], }