rust struct to json
Cargo.toml
:
[dependencies]
serde = {version = "1.0.204", features = ["derive"]}
serde_json = "1.0.120"
main.rs
:
use serde::{Deserialize, Serialize};
use serde_json;
#[derive(Serialize, Deserialize)]
struct Person {
first_name: String,
last_name: String,
age: u8,
}
fn main() {
let first_name = String::from("Glenn");
let last_name = String::from("Gillen");
let age = 40;
let p = Person{ first_name, last_name, age };
let data = serde_json::to_string(&p).expect("Not a serializable type");
println!("{}", data)
}