Skip to content

Commit

Permalink
Merge pull request #1067 from chanced/add-as_str-to-number
Browse files Browse the repository at this point in the history
adds `as_str` to `Number` if `arbitrary_precision` is enabled
  • Loading branch information
dtolnay authored Sep 9, 2023
2 parents db75c22 + 1786de2 commit 028b643
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/number.rs
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,25 @@ impl Number {
}
}

#[cfg(feature = "arbitrary_precision")]
/// Returns the `&str` representation of the `Number`.
/// ```
/// # use serde_json::Number;
/// for value in [
/// "7",
/// "12.34",
/// "34e-56789",
/// "0.0123456789000000012345678900000001234567890000123456789",
/// "343412345678910111213141516171819202122232425262728293034",
/// "-343412345678910111213141516171819202122232425262728293031",
/// ] {
/// let number: Number = serde_json::from_str(value).unwrap();
/// assert_eq!(number.as_str(), value);
/// }
pub fn as_str(&self) -> &str {
&self.n
}

pub(crate) fn as_f32(&self) -> Option<f32> {
#[cfg(not(feature = "arbitrary_precision"))]
match self.n {
Expand Down

0 comments on commit 028b643

Please sign in to comment.