Read local .json data in Rocket.rs

I'm crafting a dead simple Rust — Job Board.

I will be storing an array of Rust engineers inside a local .json file. Which, I then render as a list of Rust Engineers — Seeking Work.

Here is how we do this in Rocket.rs.

use std::fs;
use rocket::serde::{Deserialize, Serialize};
use rocket_dyn_templates::{Template, context};


#[derive(Deserialize, Serialize)]
#[serde(crate = "rocket::serde")]
struct RustEngineer {
    ...
}

#[get("/")]
pub async fn get() -> Template {
    // Open and read JSON file data.
    let data = fs::read_to_string("././static/json/rust_engineers.json").unwrap();

    // Serialize data into JSON.
    let rust_engineers: Vec<RustEngineer> = serde_json::from_str(&data).unwrap();

    Template::render("rust-engineers", context!{ rust_engineers })
}

Comments • 2

  • John Wooden

    That's what we're all doing... We're setting examples,... and our conduct will have a great bearing on our youth, certainly, and our youth are our future.

    • John S. C. Abbott

      We must be what we wish our children to be. They will form their characters from ours.

      • Marcus Aurelius

        Momento mori.

    • Seneca

      Wherever there is a human being, there is an opportunity for kindness.

    • Xunzi

      If a person puts even one measure of effort into following ritual and the standards of righteousness, he will get back twice as much.

  • Seneca

    Classical.