
Rust: Convert a data structure into a JSON string
We’ve already done most of the heavy-lifting for turning a structure into JSON:
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)
}
Instantiate an instance of our Person
, parse it into the serde via calling to_string
. Done.
Published: 24/03/2022
Hi, I'm Glenn! 👋
I've spent most of my career working with or at startups. I'm currently the VP of Product / GTM @ Ockam where I'm helping developers build applications and systems that are secure-by-design. It's time we started securely connecting apps, not networks.
Previously I led the Terraform product team @ HashiCorp, where we launched Terraform 1.0, Terraform Cloud, and a whole host of amazing capabilities that set the stage for a successful IPO. Prior to that I was part of the Startup Team @ AWS, and earlier still an early employee @ Heroku. I've also invested in a couple of dozen early stage startups.
I've spent most of my career working with or at startups. I'm currently the VP of Product / GTM @ Ockam where I'm helping developers build applications and systems that are secure-by-design. It's time we started securely connecting apps, not networks.
Previously I led the Terraform product team @ HashiCorp, where we launched Terraform 1.0, Terraform Cloud, and a whole host of amazing capabilities that set the stage for a successful IPO. Prior to that I was part of the Startup Team @ AWS, and earlier still an early employee @ Heroku. I've also invested in a couple of dozen early stage startups.