

▲ 3 r/learnrust
JsonOnTheFly
I made a simple helper tool years ago, but now i restructured it and put it on crates.io for anyone to use.
JsonOnTheFly lets you derive a macro on any struct you tinker with and gives it methods for writing to a file as a json and reading that file again. Just call write() and load() on it. This way, prototyping in rust becomes even faster, simpler and less boilerplaty.
How you do it:
use json_on_the_fly::JsonOnTheFly;
use serde::{Deserialize, Serialize};
#[derive(JsonOnTheFly, Serialize, Deserialize, Default, Clone, Debug)]
struct Color {
red: u8,
green: u8,
blue: u8,
}
fn main() {
let color = Color {
red: 0,
green: 0,
blue: 255,
};
color.write().unwrap(); // Writing the file does not require any more boilerplate.
Color::backup_db_file().unwrap(); // Create a backup of the file associated with the struct
let new_color = Color::load().unwrap(); // Initialize the struct from the .json file. The errors provided give more information about whether it was created beforehand.
println!("{:?}", new_color);
}
Repo: https://github.com/JonasFocke01/json_on_the_fly
Crate: https://crates.io/crates/json-on-the-fly
u/Jonrrrs — 17 days ago