pub struct Number<'a> { /* private fields */ }Expand description
A JSON number, kept as the exact text of the document.
Nothing is converted while parsing: the number borrows its digits from the
input, so a 30-digit integer or a decimal with more precision than f64
survives a read and a write unchanged. Conversion to a Rust number happens
only when it is asked for, with as_i64,
as_u64, or as_f64, each of which says
exactly when it succeeds.
Two numbers are equal when their text is: 1, 1.0, and 1e0 are three
different numbers here, as they are three different documents. Compare
as_f64 results to compare values.
§Examples
use jsonx_lib::{parse, Number};
let doc = parse("[12345678901234567890123, 2.50, -7]")?;
let big = doc[0].as_number().unwrap();
assert_eq!(big.as_str(), "12345678901234567890123"); // exact
assert_eq!(big.as_u64(), None); // does not fit
assert_eq!(doc[1].as_number().unwrap().as_str(), "2.50"); // trailing zero kept
assert_eq!(doc[2].as_i64(), Some(-7));
assert_eq!(Number::from(42u8).as_str(), "42");
assert_eq!(Number::from_f64(0.1).unwrap().as_str(), "0.1");Implementations§
Source§impl<'a> Number<'a>
impl<'a> Number<'a>
Sourcepub fn from_text(text: &'a str) -> Option<Self>
pub fn from_text(text: &'a str) -> Option<Self>
Makes a number from its JSON text, which must be exactly one number in RFC 8259 syntax with nothing around it. The text is borrowed, not copied, so arbitrary-precision values pass through unchanged.
Returns None for anything else: +1, 01, .5, 1., NaN,
whitespace, or an empty string.
§Examples
use jsonx_lib::{to_string, Number, Value};
let price = Number::from_text("19.990").unwrap();
assert_eq!(to_string(&Value::Number(price)), "19.990");
assert!(Number::from_text("1.").is_none());
assert!(Number::from_text("0x10").is_none());Sourcepub fn from_f64(value: f64) -> Option<Number<'static>>
pub fn from_f64(value: f64) -> Option<Number<'static>>
Makes a number from an f64, written as the shortest text that reads
back as the same f64. Returns None for NaN and the infinities,
which JSON cannot represent.
The text has a fraction or an exponent, so it reads back as a float:
1.0 is written 1.0, not 1. Values from 1e-5 up to below 1e16 are
written as decimals (0.00001, 123.456, 1000000000000000.0); the
rest in exponent form (1e16, 1.5e-7, -2.2250738585072014e-308).
§Examples
use jsonx_lib::Number;
assert_eq!(Number::from_f64(1.0).unwrap().as_str(), "1.0");
assert_eq!(Number::from_f64(-0.0).unwrap().as_str(), "-0.0");
assert_eq!(Number::from_f64(1e100).unwrap().as_str(), "1e100");
assert_eq!(Number::from_f64(0.1 + 0.2).unwrap().as_str(), "0.30000000000000004");
assert!(Number::from_f64(f64::NAN).is_none());Sourcepub fn as_str(&self) -> &str
pub fn as_str(&self) -> &str
The number’s text, exactly as written in the document (or as this crate wrote it, for a number made from a Rust value).
§Examples
use jsonx_lib::parse;
assert_eq!(parse("1E+02")?.as_number().unwrap().as_str(), "1E+02");Sourcepub fn is_integer(&self) -> bool
pub fn is_integer(&self) -> bool
Whether the text is an integer: no fraction and no exponent. 2.0 and
2e3 are not integers by this test, though their values are whole.
§Examples
use jsonx_lib::Number;
assert!(Number::from(-3).is_integer());
assert!(!Number::from_text("2.0").unwrap().is_integer());Sourcepub fn as_i64(&self) -> Option<i64>
pub fn as_i64(&self) -> Option<i64>
The number as an i64, if its text is an integer (see
is_integer) in i64’s range. None otherwise:
nothing is rounded or truncated.
§Examples
use jsonx_lib::Number;
let n = |s| Number::from_text(s).unwrap();
assert_eq!(n("-9223372036854775808").as_i64(), Some(i64::MIN));
assert_eq!(n("9223372036854775808").as_i64(), None); // out of range
assert_eq!(n("3.0").as_i64(), None); // not an integer's textSourcepub fn as_u64(&self) -> Option<u64>
pub fn as_u64(&self) -> Option<u64>
The number as a u64, if its text is an integer (see
is_integer) in u64’s range. -0 reads as 0.
None otherwise: nothing is rounded or truncated.
§Examples
use jsonx_lib::Number;
let n = |s| Number::from_text(s).unwrap();
assert_eq!(n("18446744073709551615").as_u64(), Some(u64::MAX));
assert_eq!(n("-1").as_u64(), None);
assert_eq!(n("-0").as_u64(), Some(0));Sourcepub fn as_f64(&self) -> Option<f64>
pub fn as_f64(&self) -> Option<f64>
The number as the nearest f64 (correctly rounded), or None if it is
too large in magnitude to be finite (1e400). A value too small to
represent rounds to zero, as IEEE 754 does. Integers beyond 2^53 lose
precision; use as_i64, as_u64, or
the exact as_str when that matters.
§Examples
use jsonx_lib::Number;
let n = |s| Number::from_text(s).unwrap();
assert_eq!(n("2.5e-3").as_f64(), Some(0.0025));
assert_eq!(n("7").as_f64(), Some(7.0));
assert_eq!(n("1e400").as_f64(), None);Sourcepub fn into_owned(self) -> Number<'static>
pub fn into_owned(self) -> Number<'static>
Copies borrowed text so the number no longer borrows from the input.
§Examples
use jsonx_lib::Number;
let owned: Number<'static> = {
let text = String::from("12.5");
Number::from_text(&text).unwrap().into_owned()
};
assert_eq!(owned.as_str(), "12.5");