Skip to content

Commit fa65a48

Browse files
Seulgi Kimsgkim126
authored andcommitted
Change a way to serialize the hash
And this commit also adds a test whether the serialized value is correct.
1 parent ad0f157 commit fa65a48

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

json/src/hash.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
//! Lenient hash json deserialization for test json files.
1919
2020
use primitives::{H256 as Hash256, H520 as Hash520};
21-
use rustc_hex::ToHex;
2221
use serde::de::{Error, Visitor};
2322
use serde::{Deserialize, Deserializer, Serialize, Serializer};
2423
use std::fmt;
@@ -85,9 +84,7 @@ macro_rules! impl_hash {
8584
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
8685
where
8786
S: Serializer, {
88-
let mut hex = "0x".to_string();
89-
hex.push_str(&self.0.to_hex());
90-
serializer.serialize_str(&hex)
87+
serializer.serialize_str(&format!("0x{:x}", self.0))
9188
}
9289
}
9390
};
@@ -113,6 +110,21 @@ mod test {
113110
]);
114111
}
115112

113+
#[test]
114+
fn hash_serialization() {
115+
let hash1 = H256(primitives::H256::zero());
116+
let hash2 =
117+
primitives::H256::from_str("5a39ed1020c04d4d84539975b893a4e7c53eab6c2965db8bc3468093a31bc5ae").unwrap();
118+
assert_eq!(
119+
"\"0x0000000000000000000000000000000000000000000000000000000000000000\"",
120+
serde_json::to_string(&hash1).unwrap()
121+
);
122+
assert_eq!(
123+
"\"0x5a39ed1020c04d4d84539975b893a4e7c53eab6c2965db8bc3468093a31bc5ae\"",
124+
serde_json::to_string(&hash2).unwrap()
125+
);
126+
}
127+
116128
#[test]
117129
fn hash_into() {
118130
assert_eq!(primitives::H256::zero(), H256(primitives::H256::zero()).into());

0 commit comments

Comments
 (0)