Skip to main content

deserialize_optional_decimal

Function deserialize_optional_decimal 

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

Deserializes an Option<Decimal> from a JSON string, number, or null.

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

  • JSON string: "123.456" → Some(Decimal) (zero-copy for borrowed strings)
  • JSON integer: 123 → Some(Decimal) (direct conversion)
  • JSON float: 123.456 → Some(Decimal)
  • JSON null: → None
  • Empty string: ""None
  • Scientific notation: "1.5e-8" → Some(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.