Skip to main content

deserialize_decimal

Function deserialize_decimal 

Source
pub fn deserialize_decimal<'de, D>(deserializer: D) -> Result<Decimal, D::Error>
where D: Deserializer<'de>,
Expand description

Deserializes a Decimal from either a JSON string or number.

High-performance implementation using a custom visitor that avoids intermediate serde_json::Value allocations. Handles all JSON numeric representations:

  • JSON string: "123.456" → Decimal (zero-copy for borrowed strings)
  • JSON integer: 123 → Decimal (direct conversion, no string allocation)
  • JSON float: 123.456 → Decimal
  • JSON null: → Decimal::ZERO
  • Scientific notation: "1.5e-8" → Decimal

§Performance

This implementation is optimized for high-frequency trading scenarios:

  • Zero allocations for string values (uses borrowed &str)
  • Direct integer conversion without string intermediary
  • No intermediate serde_json::Value heap allocation

§Errors

Returns an error if the value cannot be parsed as a valid decimal.