Skip to content

Instantly share code, notes, and snippets.

@patrickelectric
Created January 30, 2024 14:23
Show Gist options
  • Save patrickelectric/ebafbea5e3afcdffe495f1fe2099d54f to your computer and use it in GitHub Desktop.
Save patrickelectric/ebafbea5e3afcdffe495f1fe2099d54f to your computer and use it in GitHub Desktop.
Rust gists
extern crate serde; // 1.0.79
#[macro_use]
extern crate serde_derive; // 1.0.79
extern crate serde_json; // 1.0.30, features = ["raw_value"]
#[derive(Serialize, Deserialize, Debug)]
struct DataBlob<'a> {
id: &'a str,
priority: u8,
payload: &'a serde_json::value::RawValue,
}
fn main() {
let input = r#"{
"id": "cat",
"priority": 42,
"payload": [1, 2, 3, 4]
}"#;
let parsed = serde_json::from_str::<DataBlob>(input).expect("Could not deserialize");
let output = serde_json::to_string(&parsed).expect("Could not serialize");
assert!(output.contains("payload"));
dbg!(parsed);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment