The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Read JSON Into A Rust Struct With Serde

TODO: Pull subtitle into page object
Code
//! ```cargo
//! [dependencies]
//! serde_json = "1.0.110"
//! serde = { version = "1.0.194", features = ["derive"] }
//! ```

#![allow(dead_code)]
use serde::Deserialize;
use serde_json;

#[derive(Debug, Deserialize)]
struct Site {
    title: String,
    pages: Vec<Page>
}

#[derive(Debug, Deserialize)]
struct Page {
    id: u32,
    content: String 
}

fn main() {
     let source = r#"{
        "title": "asdf",
        "pages": [
            { "id": 12, "content": "quick fox"},
            { "id": 37, "content": "slow dog"}
        ]
    }"#;
    let data: Site = serde_json::from_str(source).unwrap();
    println!("{:?}", data);
}
Results
Site { title: "asdf", pages: [Page { id: 12, content: "quick fox" }, Page { id: 37, content: "slow dog" }] }

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Read JSON Into A Rust Struct With Serde

-- code
-- rust

//! ```cargo
//! [dependencies]
//! serde_json = "1.0.110"
//! serde = { version = "1.0.194", features = ["derive"] }
//! ```

#![allow(dead_code)]
use serde::Deserialize;
use serde_json;

#[derive(Debug, Deserialize)]
struct Site {
    title: String,
    pages: Vec<Page>
}

#[derive(Debug, Deserialize)]
struct Page {
    id: u32,
    content: String 
}

fn main() {
     let source = r#"{
        "title": "asdf",
        "pages": [
            { "id": 12, "content": "quick fox"},
            { "id": 37, "content": "slow dog"}
        ]
    }"#;
    let data: Site = serde_json::from_str(source).unwrap();
    println!("{:?}", data);
}

-- results/

Site { title: "asdf", pages: [Page { id: 12, content: "quick fox" }, Page { id: 37, content: "slow dog" }] }

-- /results

-- ref
-- url: https://docs.rs/serde_json/latest/serde_json/
-- title: Rust: serde_json

-- ref
-- url: https://docs.rs/serde/latest/serde/
-- title: Rust: serde


-- categories
-- Rust 
-- serde 

-- metadata
-- date: 2024-01-03 16:16:33
-- id: 2lpycsr3
-- status: published 
-- type: post
-- site: aws