parson/
json_number.rs

1use std::fmt::Display;
2
3#[derive(Debug, Clone)]
4pub struct JSONNumber {
5    data: f64,
6}
7
8impl Display for JSONNumber {
9    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
10        write!(f, "{}", self.to_string())
11    }
12}
13
14impl JSONNumber {
15    /// Create a new empty JSON Number
16    pub fn new(data: f64) -> Self {
17        JSONNumber { data }
18    }
19
20    /// Convert JSON Number to a Rust owned string
21    ///
22    /// # Example
23    ///
24    /// ```
25    /// use parson::JSONNumber;
26    ///
27    /// assert_eq!(JSONNumber::new(0.0).to_string(), "0");
28    /// assert_eq!(JSONNumber::new(1.0).to_string(), "1");
29    /// assert_eq!(JSONNumber::new(-1.0).to_string(), "-1");
30    /// assert_eq!(JSONNumber::new(-1e1).to_string(), "-10");
31    /// assert_eq!(JSONNumber::new(-1e-1).to_string(), "-0.1");
32    /// ```
33    pub fn to_string(&self) -> String {
34        self.data.to_string()
35    }
36
37    /// Get a Rust f64 from the JSON Number
38    pub fn get_number(&self) -> f64 {
39        self.data
40    }
41}