forked from paradigmxyz/reth
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: use correct serde impl for Withdrawal (paradigmxyz#1616)
- Loading branch information
Showing
4 changed files
with
66 additions
and
6 deletions.
There are no files selected for viewing
This file contains 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
File renamed without changes.
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
//! Various serde utilities | ||
mod jsonu256; | ||
pub use jsonu256::*; | ||
|
||
/// serde functions for handling primitive `u64` as [U64](crate::U64) | ||
pub mod u64_hex { | ||
use crate::U64; | ||
use serde::{Deserialize, Deserializer, Serialize, Serializer}; | ||
|
||
/// Deserializes an `u64` from [U64] accepting a hex quantity string with optional 0x prefix | ||
pub fn deserialize<'de, D>(deserializer: D) -> Result<u64, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
U64::deserialize(deserializer).map(|val| val.as_u64()) | ||
} | ||
|
||
/// Serializes u64 as hex string | ||
pub fn serialize<S: Serializer>(value: &u64, s: S) -> Result<S::Ok, S::Error> { | ||
U64::from(*value).serialize(s) | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
#[test] | ||
fn test_hex_u64() { | ||
#[derive(Debug, Serialize, Deserialize, PartialEq, Eq)] | ||
struct Value { | ||
#[serde(with = "u64_hex")] | ||
inner: u64, | ||
} | ||
|
||
let val = Value { inner: 1000 }; | ||
let s = serde_json::to_string(&val).unwrap(); | ||
assert_eq!(s, "{\"inner\":\"0x3e8\"}"); | ||
|
||
let deserialized: Value = serde_json::from_str(&s).unwrap(); | ||
assert_eq!(val, deserialized); | ||
} | ||
} |
This file contains 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