Skip to content

Instantly share code, notes, and snippets.

@meetmangukiya
Created June 25, 2022 13:12
Show Gist options
  • Save meetmangukiya/40cad17bcb7d3196d33b072a3500fac7 to your computer and use it in GitHub Desktop.
Save meetmangukiya/40cad17bcb7d3196d33b072a3500fac7 to your computer and use it in GitHub Desktop.
U256 decimal string ser/de
pub mod u256_fromstr_radix_10 {
use super::*;
pub fn deserialize<'de, D>(deserializer: D) -> Result<U256, D::Error>
where
D: Deserializer<'de>,
{
struct Helper;
impl<'de> Visitor<'de> for Helper {
type Value = U256;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a string")
}
fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
U256::from_dec_str(value).map_err(serde::de::Error::custom)
}
}
deserializer.deserialize_str(Helper)
}
pub fn serialize<S>(value: &U256, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.collect_str(&value)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment