Available on crate feature
serde only.Expand description
Custom Serde serializer/deserializer module that uses numerator as the serialized value.
The default Serialize/Deserialize implementation of DivInt serializes the struct as a
floating point number. With this serializer (and Serde’s with attribute) you can override
the built-in behavior and serialize DivInt as a numerator instead.
§Examples
use serde::{Deserialize, Serialize};
use div_int::{DivInt, div_int};
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq)]
struct Test {
#[serde(with="div_int::serde::as_numerator")]
num: DivInt<u16, 1024>,
}
assert_eq!(serde_json::to_string(&Test{num: div_int!(100 / 1024) }).unwrap(), r#"{"num":100}"#);
assert_eq!(serde_json::from_str::<Test>(r#"{"num": 123}"#).unwrap(), Test {num: div_int!(123 / 1024)});Functions§
- deserialize
- Serde deserializer function for
DivIntthat expects the numerator as the deserialized. - serialize
- Serde serializer function for
DivIntthat uses the numerator as the serialized value.