pub struct JsonNumberToken<'call> { /* private fields */ }Expand description
A syntactically validated number token borrowed from the input document.
The token is only valid during the synchronous validated-decoding callback
that supplied it; its source spelling cannot outlive that callback. Offsets
are byte offsets from the beginning of the source document. The exact
safe-integer range is the closed interval
[-9007199254740991, 9007199254740991]. Decimal or exponent spellings are
interpreted exactly, without an intermediate floating-point conversion.
§Examples
use qubit_json::decode::{JsonDecoder, JsonNumberPolicy, JsonValidatedDecodeOptions};
let options = JsonValidatedDecodeOptions::builder().build();
let owner = JsonDecoder::unlimited().decode_unique_validated_str(
"1.00e2", &options, |path, token| {
assert!(path.is_empty());
assert_eq!(token.raw(), "1.00e2");
assert_eq!(token.offset(), 0);
assert_eq!(token.exact_safe_integer(), Ok(100));
Ok::<_, std::convert::Infallible>(JsonNumberPolicy::ExactSafeInteger)
})?;
assert_eq!(owner.as_value().as_i64(), Some(100));Implementations§
Source§impl<'call> JsonNumberToken<'call>
impl<'call> JsonNumberToken<'call>
Sourcepub const fn raw(&self) -> &'call str
pub const fn raw(&self) -> &'call str
Returns the original number spelling.
§Returns
The validated source spelling, borrowed for the callback duration.
Sourcepub const fn offset(&self) -> usize
pub const fn offset(&self) -> usize
Returns the zero-based byte offset of the token in its source document.
Sourcepub fn exact_safe_integer(&self) -> Result<i64, JsonNumberTokenError>
pub fn exact_safe_integer(&self) -> Result<i64, JsonNumberTokenError>
Converts an exact integral token in the IEEE-754 safe-integer range without converting through floating point.
§Returns
The represented integer when its exact mathematical value is integral
and lies in [-9007199254740991, 9007199254740991].
§Errors
Returns JsonNumberTokenError::Fractional when the exact value has a
fractional part, and JsonNumberTokenError::OutOfSafeIntegerRange
when the exact integral value lies outside the safe-integer interval.