nushell/crates/nu-protocol/src/value/serde_bigdecimal.rs
2021-01-09 12:53:59 +13:00

20 lines
686 B
Rust

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,
{
serde::Serialize::serialize(&big_decimal.to_string(), serializer)
}
/// 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>,
{
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"))?)
}