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_i128(&self) -> Option<i128>
pub fn as_i128(&self) -> Option<i128>
The number as an i128, if its text is an integer in i128’s range.
None otherwise: nothing is rounded or truncated.
§Examples
use jsonx_lib::Number;
let big = Number::from_text("-170141183460469231731687303715884105728").unwrap();
assert_eq!(big.as_i128(), Some(i128::MIN));
assert_eq!(Number::from_text("1e3").unwrap().as_i128(), None);Sourcepub fn as_u128(&self) -> Option<u128>
pub fn as_u128(&self) -> Option<u128>
The number as a u128, if its text is an integer in u128’s range.
-0 reads as 0.
§Examples
use jsonx_lib::Number;
let n = Number::from_text("340282366920938463463374607431768211455").unwrap();
assert_eq!(n.as_u128(), Some(u128::MAX));
assert_eq!(Number::from_text("-0").unwrap().as_u128(), Some(0));
assert_eq!(Number::from_text("-1").unwrap().as_u128(), None);Sourcepub fn whole_value(&self) -> Option<i128>
pub fn whole_value(&self) -> Option<i128>
The number’s value, if it is a whole number in i128’s range,
however it is written: 100, 1e2, 100.0, and 1000e-1 are all
Some(100), -0.0 is Some(0), and 1.5 or 1e40 are None. The
decimal digits and the exponent are worked exactly: nothing goes
through floating point, so nothing is rounded.
as_i64 and its siblings read integer text only;
this reads integer values, for data from writers that put whole
numbers in exponent or fraction form. See also
Numbers::WholeValues.
§Examples
use jsonx_lib::Number;
let n = |s| Number::from_text(s).unwrap();
assert_eq!(n("1e2").whole_value(), Some(100));
assert_eq!(n("2.50e1").whole_value(), Some(25));
assert_eq!(n("-12.000").whole_value(), Some(-12));
assert_eq!(n("0.5e1").whole_value(), Some(5));
assert_eq!(n("1.25").whole_value(), None);
assert_eq!(n("1e39").whole_value(), None); // past i128Sourcepub fn as_f32(&self) -> Option<f32>
pub fn as_f32(&self) -> Option<f32>
The number as the nearest f32 (correctly rounded from the text
itself, not through f64), or None if that is not finite (1e39).
§Examples
use jsonx_lib::Number;
assert_eq!(Number::from_text("0.1").unwrap().as_f32(), Some(0.1));
assert_eq!(Number::from_text("1e39").unwrap().as_f32(), None);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");