Lowercasing all keys in serde_json

Besides the helper crate shared above, another idea I had is first reading the json as a serde_json::Value type, and then mapping the object inside with all lowercase keys, like so:

use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Serialize, Deserialize)]
struct Person {
    name: String,
    age: u8,
    phones: Vec<String>,
}

fn main() {
    let data1 = r#"
        {
            "name": "John Doe",
            "age": 43,
            "phones": [
                "+44 1234567",
                "+44 2345678"
            ]
        }"#;

    let data2 = r#"
        {
            "NAME": "John Doe",
            "aGe": 43,
            "phoNES": [
                "+44 1234567",
                "+44 2345678"
            ]
        }"#;

    let p: Person = serde_json::from_str(data1).expect("Invalid JSON");
    println!("Please call {} at the number {}", p.name, p.phones[0]);

    let v: Value = serde_json::from_str(data2).expect("Invalid JSON");
    if let Value::Object(obj) = v {
        let v: Value = obj
            .into_iter()
            .map(|(k, v)| (k.to_lowercase(), v))
            .collect();
        let p: Person = serde_json::from_value(v).unwrap();
        println!("Please call {} at the number {}", p.name, p.phones[0]);
    }
}

But at that point you might just want to write a deserializer :wink: