astro_float_num/for_3rd/
ser.rs

1//! Serialization of BigFloat.
2//! Serialization to a string uses decimal radix.
3
4use crate::BigFloat;
5use serde::{Serialize, Serializer};
6
7impl Serialize for BigFloat {
8    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
9        serializer.serialize_str(&self.to_string())
10    }
11}
12
13#[cfg(test)]
14mod tests {
15    use serde_json::to_string;
16
17    use crate::BigFloat;
18
19    #[test]
20    fn to_json() {
21        assert_eq!(to_string(&BigFloat::new(0)).unwrap(), "\"0.0\"");
22        assert_eq!(
23            to_string(&BigFloat::from_f32(0.3, 64 + 1)).unwrap(),
24            "\"3.00000011920928955078125e-1\""
25        );
26    }
27}