Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds as_str to Number if arbitrary_precision is enabled #1067

Merged
merged 4 commits into from
Sep 9, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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