Created
January 30, 2024 14:23
-
-
Save patrickelectric/ebafbea5e3afcdffe495f1fe2099d54f to your computer and use it in GitHub Desktop.
Rust gists
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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