How to deserialize JSON to structs

Deserialize JSON to Rust structs using the serde crate with the Deserialize derive macro and serde_json parsing functions.

Use the serde crate to derive the Deserialize trait on your struct and call serde_json::from_str or serde_json::from_slice to parse the JSON.

use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct Config {
    output_mode: Mode,
}

let json = r#"{"output_mode": "simple"}"#;
let config: Config = serde_json::from_str(json).unwrap();

For nested config paths like preprocessor.test-preprocessor, access the specific key from the parent object before deserializing:

use serde::Deserialize;

#[derive(Debug, Deserialize)]
struct PreprocessorConfig {
    output_mode: Mode,
}

let json = r#"{"test-preprocessor": {"output-mode": "simple"}}"#;
let parent: serde_json::Value = serde_json::from_str(json).unwrap();
let preprocessor_json = parent.get("test-preprocessor").unwrap();
let config: PreprocessorConfig = serde_json::from_value(preprocessor_json.clone()).unwrap();

Use #[serde(rename_all = "kebab-case")] on the struct to match JSON keys like output-mode to Rust fields like output_mode.