Available on crate feature serde and (crate features serde-with-str or serde-with-float or serde-with-arbitrary-precision) and crate feature serde-with-float only.
Expand description

Serialize/deserialize optional Decimals as floats.


#[derive(Serialize, Deserialize)]
pub struct FloatExample {
    #[serde(with = "rust_decimal::serde::float_option")]
    value: Option<Decimal>,
}

let value = FloatExample { value: Some(Decimal::from_str("123.400").unwrap()) };
assert_eq!(
    &serde_json::to_string(&value).unwrap(),
    r#"{"value":123.4}"#
);

let value = FloatExample { value: None };
assert_eq!(
    &serde_json::to_string(&value).unwrap(),
    r#"{"value":null}"#
);

Functions§