Last active
May 16, 2025 17:17
-
-
Save cmackenzie1/7f61fb1bf3256620f74d9259898127e8 to your computer and use it in GitHub Desktop.
Revisions
-
cmackenzie1 revised this gist
May 16, 2025 . 1 changed file with 3 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,3 @@ Message { payload: [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100] } {"payload":"aGVsbG8gd29ybGQ="} Message { payload: [104, 101, 108, 108, 111, 32, 119, 111, 114, 108, 100] } -
cmackenzie1 revised this gist
May 16, 2025 . 2 changed files with 20 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,4 @@ [dependencies] serde = { version = "1", features = ["derive"] } serde_json = "1" base64 = "0.22" This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,3 @@ mod base64 { use base64::prelude::*; use serde::{Deserialize, Serialize}; @@ -18,4 +14,20 @@ mod base64 { .decode(base64.as_bytes()) .map_err(serde::de::Error::custom) } } #[derive(Debug, Clone, serde::Serialize, serde::Deserialize)] pub struct Message { #[serde(with = "base64")] pub payload: Vec<u8>, } fn main() { let a = Message { payload: b"hello world".into() }; println!("{:?}", a); let a_json = serde_json::to_string(&a).unwrap(); println!("{}", a_json); let b: Message = serde_json::from_str(&a_json).unwrap(); println!("{:?}", b); assert_eq!(a.payload, b.payload); } -
cmackenzie1 revised this gist
May 16, 2025 . No changes.There are no files selected for viewing
-
cmackenzie1 revised this gist
May 16, 2025 . No changes.There are no files selected for viewing
-
cmackenzie1 created this gist
May 16, 2025 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,21 @@ // Cargo.toml // [dependencies] // base64 = "0.22" mod base64 { use base64::prelude::*; use serde::{Deserialize, Serialize}; use serde::{Deserializer, Serializer}; pub fn serialize<S: Serializer>(v: &Vec<u8>, s: S) -> Result<S::Ok, S::Error> { let base64 = BASE64_STANDARD.encode(v); String::serialize(&base64, s) } pub fn deserialize<'de, D: Deserializer<'de>>(d: D) -> Result<Vec<u8>, D::Error> { let base64 = String::deserialize(d)?; BASE64_STANDARD .decode(base64.as_bytes()) .map_err(serde::de::Error::custom) } }