2020-10-13 04:44:28 +02:00
|
|
|
use bigdecimal::BigDecimal;
|
|
|
|
|
|
|
|
/// Enable big decimal serialization by providing a `serialize` function
|
|
|
|
pub fn serialize<S>(big_decimal: &BigDecimal, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: serde::Serializer,
|
|
|
|
{
|
2021-01-09 00:53:59 +01:00
|
|
|
serde::Serialize::serialize(&big_decimal.to_string(), serializer)
|
2020-10-13 04:44:28 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Enable big decimal deserialization by providing a `deserialize` function
|
|
|
|
pub fn deserialize<'de, D>(deserializer: D) -> Result<BigDecimal, D::Error>
|
|
|
|
where
|
|
|
|
D: serde::Deserializer<'de>,
|
|
|
|
{
|
2021-01-09 00:53:59 +01:00
|
|
|
let x: String = serde::Deserialize::deserialize(deserializer)?;
|
|
|
|
Ok(BigDecimal::parse_bytes(x.as_bytes(), 10)
|
|
|
|
.ok_or_else(|| serde::de::Error::custom("expected a bigdecimal"))?)
|
2020-10-13 04:44:28 +02:00
|
|
|
}
|