serialize.rs
1 // Copyright (c) 2019-2025 Alpha-Delta Network Inc. 2 // This file is part of the alphavm library. 3 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at: 7 8 // http://www.apache.org/licenses/LICENSE-2.0 9 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 16 use super::*; 17 18 impl<N: Network> Serialize for Identifier<N> { 19 /// Serializes the identifier into string or bytes. 20 fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> { 21 match serializer.is_human_readable() { 22 true => serializer.collect_str(self), 23 false => ToBytesSerializer::serialize(self, serializer), 24 } 25 } 26 } 27 28 impl<'de, N: Network> Deserialize<'de> for Identifier<N> { 29 /// Deserializes the identifier from a string or bytes. 30 fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> { 31 match deserializer.is_human_readable() { 32 true => FromStr::from_str(&String::deserialize(deserializer)?).map_err(de::Error::custom), 33 false => FromBytesDeserializer::<Self>::deserialize_with_u8(deserializer, "identifier"), 34 } 35 } 36 } 37 38 #[cfg(test)] 39 mod tests { 40 use super::*; 41 use crate::data::identifier::tests::sample_identifier; 42 use alphavm_console_network::MainnetV0; 43 44 type CurrentNetwork = MainnetV0; 45 46 const ITERATIONS: u64 = 1000; 47 48 #[test] 49 fn test_serde_json() -> Result<()> { 50 let mut rng = TestRng::default(); 51 52 for _ in 0..ITERATIONS { 53 // Sample a random fixed-length alphanumeric identifier, that always starts with an alphabetic character. 54 let expected = sample_identifier::<CurrentNetwork>(&mut rng)?; 55 56 // Serialize 57 let expected_string = &expected.to_string(); 58 let candidate_string = serde_json::to_string(&expected)?; 59 assert_eq!(expected_string, serde_json::Value::from_str(&candidate_string)?.as_str().unwrap()); 60 61 // Deserialize 62 assert_eq!(expected, Identifier::from_str(expected_string)?); 63 assert_eq!(expected, serde_json::from_str(&candidate_string)?); 64 } 65 Ok(()) 66 } 67 68 #[test] 69 fn test_bincode() -> Result<()> { 70 let mut rng = TestRng::default(); 71 72 for _ in 0..ITERATIONS { 73 // Sample a random fixed-length alphanumeric identifier, that always starts with an alphabetic character. 74 let expected = sample_identifier::<CurrentNetwork>(&mut rng)?; 75 76 // Serialize 77 let expected_bytes = expected.to_bytes_le()?; 78 assert_eq!(&expected_bytes[..], &bincode::serialize(&expected)?[..]); 79 80 // Deserialize 81 assert_eq!(expected, Identifier::read_le(&expected_bytes[..])?); 82 assert_eq!(expected, bincode::deserialize(&expected_bytes[..])?); 83 } 84 Ok(()) 85 } 86 }