Created
July 28, 2024 14:13
-
-
Save TwistingTwists/02a740648c1758caea818a4d408e61f8 to your computer and use it in GitHub Desktop.
testing alias properties of serde
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 std::hash::{Hash, Hasher}; | |
use serde::Deserialize; | |
use serde::Serialize; | |
#[derive(Debug, Deserialize, Serialize)] | |
pub struct TunTun{ | |
pub two: MyStruct, | |
// #[serde( alias = "two")] | |
// pub two_1: MyStruct | |
} | |
#[derive(Debug, Deserialize, PartialEq, Eq, Hash, Serialize)] | |
pub struct MyStruct(u64); | |
impl From<u32> for MyStruct { | |
fn from(value: u32) -> Self { | |
MyStruct(value as u64) | |
} | |
} | |
#[cfg(test)] | |
mod tests_1 { | |
use super::*; | |
use serde_json; | |
#[test] | |
fn test_tuntun_deserialization() { | |
// Test normal deserialization | |
let json = r#"{"two": 10}"#; | |
let tuntun: TunTun = serde_json::from_str(json).unwrap(); | |
// assert_eq!(tuntun.two, 10 ); | |
assert_eq!(tuntun.two, MyStruct(10)); | |
// assert_eq!(tuntun.two_1, MyStruct(10)); | |
// Test deserialization with alias | |
let json_with_alias = r#"{ "two": 20}"#; | |
let tuntun_alias: TunTun = serde_json::from_str(json_with_alias).unwrap(); | |
// assert_eq!(tuntun_alias.two, 20 ); | |
assert_eq!(tuntun_alias.two, MyStruct(20)); | |
// assert_eq!(tuntun_alias.two_1, MyStruct(20)); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment