Last active
February 3, 2022 09:56
-
-
Save arc279/8a664dcafd2ae460a620b078bc86e1b8 to your computer and use it in GitHub Desktop.
generic serialize / deserialize
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 characters
[package] | |
name = "generic" | |
version = "0.1.0" | |
edition = "2021" | |
[dependencies] | |
serde = { version = "1.0", features = ["derive"] } | |
serde_json = "1.0" |
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 characters
use serde::{Deserialize, Serialize}; | |
use serde_json; | |
#[derive(Deserialize, Serialize, PartialEq, Debug)] | |
pub struct User { | |
pub name: String, | |
pub token: String, | |
pub hash: String, | |
} | |
#[derive(Deserialize, Serialize, PartialEq, Debug)] | |
pub struct Account { | |
pub id: u32, | |
pub token: String, | |
pub hash: String, | |
} | |
#[derive(Deserialize, Serialize, PartialEq, Debug)] | |
pub struct Message<T> { | |
pub utc: u64, | |
pub mtd: u64, | |
pub user: T, | |
} | |
pub fn serialize<T>(obj: &T) -> String | |
where | |
T: Serialize, | |
{ | |
let serialized = serde_json::to_string(&obj).unwrap(); | |
return serialized; | |
} | |
pub fn deserialize<'de, T>(msg: &'de String) -> T | |
where | |
T: Deserialize<'de>, | |
{ | |
let x: T = serde_json::from_str(msg).unwrap(); | |
return x; | |
} | |
fn main() { | |
let msg = Message::<User> { | |
utc: 123, | |
mtd: 333, | |
user: User { | |
name: "hoge".into(), | |
token: "tok".into(), | |
hash: "hs".into(), | |
}, | |
}; | |
let s = serialize(&msg); | |
println!("{}", s); | |
let v = deserialize::<Message<User>>(&s); | |
println!("{:?}", v); | |
// --------------------------------- | |
let msg = Message::<Account> { | |
utc: 123, | |
mtd: 333, | |
user: Account { | |
id: 123456, | |
token: "tok".into(), | |
hash: "hs".into(), | |
}, | |
}; | |
let s = serialize(&msg); | |
println!("{}", s); | |
let v = deserialize::<Message<Account>>(&s); | |
println!("{:?}", v); | |
} |
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 characters
$ cargo run | |
Finished dev [unoptimized + debuginfo] target(s) in 0.01s | |
Running `target/debug/generic` | |
{"utc":123,"mtd":333,"user":{"name":"hoge","token":"tok","hash":"hs"}} | |
Message { utc: 123, mtd: 333, user: User { name: "hoge", token: "tok", hash: "hs" } } | |
{"utc":123,"mtd":333,"user":{"id":123456,"token":"tok","hash":"hs"}} | |
Message { utc: 123, mtd: 333, user: Account { id: 123456, token: "tok", hash: "hs" } } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment