-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(exporter): serde: represent
u128
/i128
as strings
See serde-rs/json#625. This patch is highly inspired by txpipe/oura#712.
- Loading branch information
Lucas Franceschino
committed
Jul 1, 2024
1 parent
7be21f5
commit 6a116e1
Showing
4 changed files
with
110 additions
and
3 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
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
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
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,91 @@ | ||
use serde::{de::Visitor, ser::Serialize, Deserializer, Serializer}; | ||
|
||
pub mod unsigned { | ||
use super::*; | ||
pub fn serialize<S>(value: &u128, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
value.to_string().serialize(serializer) | ||
} | ||
|
||
pub fn deserialize<'de, D>(deserializer: D) -> Result<u128, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
deserializer.deserialize_any(IntScalarVisitor) | ||
} | ||
|
||
#[derive(Debug)] | ||
struct IntScalarVisitor; | ||
impl<'de> Visitor<'de> for IntScalarVisitor { | ||
type Value = u128; | ||
|
||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
dbg!(self); | ||
formatter.write_str("expect to receive integer") | ||
} | ||
|
||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> | ||
where | ||
E: serde::de::Error, | ||
{ | ||
Ok(v.parse().map_err(serde::de::Error::custom)?) | ||
} | ||
|
||
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E> | ||
where | ||
E: serde::de::Error, | ||
{ | ||
Ok(v as u128) | ||
} | ||
} | ||
} | ||
pub mod signed { | ||
use super::*; | ||
pub fn serialize<S>(value: &i128, serializer: S) -> Result<S::Ok, S::Error> | ||
where | ||
S: Serializer, | ||
{ | ||
value.to_string().serialize(serializer) | ||
} | ||
|
||
pub fn deserialize<'de, D>(deserializer: D) -> Result<i128, D::Error> | ||
where | ||
D: Deserializer<'de>, | ||
{ | ||
deserializer.deserialize_any(IntScalarVisitor) | ||
} | ||
|
||
#[derive(Debug)] | ||
struct IntScalarVisitor; | ||
impl<'de> Visitor<'de> for IntScalarVisitor { | ||
type Value = i128; | ||
|
||
fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result { | ||
dbg!(self); | ||
formatter.write_str("expect to receive integer") | ||
} | ||
|
||
fn visit_str<E>(self, v: &str) -> Result<Self::Value, E> | ||
where | ||
E: serde::de::Error, | ||
{ | ||
Ok(v.parse().map_err(serde::de::Error::custom)?) | ||
} | ||
|
||
fn visit_u64<E>(self, v: u64) -> Result<Self::Value, E> | ||
where | ||
E: serde::de::Error, | ||
{ | ||
Ok(v as i128) | ||
} | ||
|
||
fn visit_i64<E>(self, v: i64) -> Result<Self::Value, E> | ||
where | ||
E: serde::de::Error, | ||
{ | ||
Ok(v as i128) | ||
} | ||
} | ||
} |