- refactor (de)serialization to JSON/String

This commit is contained in:
sam 2024-04-19 18:04:54 +02:00
parent 0e6d8104e6
commit 2c430758f1
2 changed files with 20 additions and 9 deletions

View file

@ -10,7 +10,6 @@ env_logger = "0.11.3"
tokio = "1.37.0"
axum = "0.7.5"
tower = "0.4.13"
utoipa = "4.2.0"
[workspace.lints.clippy]
uninlined_format_args = "warn"

View file

@ -11,9 +11,16 @@ mod tests {
let result = add(2, 2);
assert_eq!(result, 4);
}
#[test]
fn print_serialized() {
let temp_secret = TempSecret { content: String::from("Hallo"), version: 12 };
let serialized = serialize_secret_json(&temp_secret);
println!("string serialized: {:?}", serialized);
let deserialized = deserialize_secret_struct(&serialized);
println!("Struct field from deserialized: {}", deserialized.content)
}
}
use serde_json::Result;
use serde::{Serialize, Deserialize};
#[derive(Serialize, Deserialize)]
@ -22,12 +29,17 @@ pub struct TempSecret {
pub version: i64
}
/// serialize secret to JSON byte vector
pub fn serialize_secret_json(secret: &TempSecret) -> Result<Vec<u8>> {
serde_json::to_vec(&secret)
// /// serialize secret to JSON byte vector
// pub fn serialize_secret_json(secret: &TempSecret) -> Result<Vec<u8>> {
// serde_json::to_vec(&secret)
// }
/// serialize secret to JSON String
pub fn serialize_secret_json(secret: &TempSecret) -> String {
serde_json::to_string(&secret).unwrap()
}
// /// deserialize JSON byte vector to secret
// pub fn deserialize_secret_struct(raw: &String) -> Result<TempSecret> {
// serde_json::from_str(raw)
// }
/// deserialize JSON String to secret
pub fn deserialize_secret_struct(raw: &String) -> TempSecret{
serde_json::from_str(&raw).unwrap()
}