Read JSON with Optional fields



examples/json/read-with-optional-field/just_name.json
{
    "name": "Foo"
}

examples/json/read-with-optional-field/no_name.json
{
    "married": true,
    "language": "Python"
}

examples/json/read-with-optional-field/married_no_language.json
{
    "name": "Foo",
    "married": true
}

examples/json/read-with-optional-field/married_with_python.json
{
    "name": "Foo",
    "married": true,
    "language": "Python"
}

examples/json/read-with-optional-field/single_with_python.json
{
    "name": "Foo",
    "married": false,
    "language": "Python"
}

examples/json/read-with-optional-field/src/main.rs
use serde::Deserialize;

#[derive(Deserialize, Debug)]
#[allow(dead_code)]
struct Person {
    name: String,

    #[serde(default = "get_default_language")]
    language: String,

    married: Option<bool>,
}

fn get_default_language() -> String {
    String::from("Rust")
}

fn main() {
    let filename = get_filename();

    let content = std::fs::read_to_string(filename).unwrap();

    let data = serde_json::from_str::<Person>(&content).expect("JSON parsing error");
    println!("{:#?}", data);

    match data.married {
        None => println!("We don't know if {} is married or not", data.name),
        Some(val) => println!("Marrige status: {val}"),
    }
}

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()
}