Skip to content

Commit

Permalink
normalize IssuedCurrency before serializing
Browse files Browse the repository at this point in the history
  • Loading branch information
LimpidCrypto committed Oct 12, 2024
1 parent 16e1139 commit 029dc71
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
7 changes: 0 additions & 7 deletions src/_serde/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -290,10 +290,3 @@ macro_rules! serde_with_tag {
}
};
}

/// Normally when using bigdecimal "serde_json" feature a `1` will be serialized as `1.000000000000000`.
/// This function serializes a `BigDecimal` to a string without the trailing zeros.
pub fn serialize_bigdecimal<S: Serializer>(value: &BigDecimal, s: S) -> Result<S::Ok, S::Error> {
let trimmed_str = value.normalized().to_string();
s.serialize_str(&trimmed_str)
}
8 changes: 7 additions & 1 deletion src/core/types/amount.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
//! See Amount Fields:
//! `<https://xrpl.org/serialization.html#amount-fields>`
use crate::_serde::serialize_bigdecimal;
use crate::core::binarycodec::exceptions::XRPLBinaryCodecException;
use crate::core::types::exceptions::XRPLTypeException;
use crate::core::types::*;
Expand Down Expand Up @@ -35,6 +34,13 @@ const _ZERO_CURRENCY_AMOUNT_HEX: u64 = 0x8000000000000000;
const _NATIVE_AMOUNT_BYTE_LENGTH: u8 = 8;
const _CURRENCY_AMOUNT_BYTE_LENGTH: u8 = 48;

/// Normally when using bigdecimal "serde_json" feature a `1` will be serialized as `1.000000000000000`.
/// This function normalizes a `BigDecimal` before serializing to a string.
pub fn serialize_bigdecimal<S: Serializer>(value: &BigDecimal, s: S) -> Result<S::Ok, S::Error> {
let trimmed_str = value.normalized().to_string();
s.serialize_str(&trimmed_str)
}

/// An Issued Currency object.
#[derive(Debug, Serialize, Deserialize, Clone)]
pub(crate) struct IssuedCurrency {
Expand Down

0 comments on commit 029dc71

Please sign in to comment.