pub struct Number<'a> { /* private fields */ }Expand description
A number literal from a text format.
This is a well-known borrowing extension (see ext) for
numbers that text formats parsed as floats (or integers too large for
the data model). It holds the text of the literal in the syntax of JSON
numbers together with its value as f64. The fallback is the f64, so
consumers that do not know about exact numbers are not affected.
Consumers that need the exact value (like Decimal
and BigInt and the types of rust_decimal,
bigdecimal and num-bigint) use the text.
use deser::ext::{Decimal, Number};
let number = Number::parse("0.10").unwrap();
assert_eq!(number.as_str(), "0.10");
assert_eq!(number.value(), 0.1);The text borrows from the input for formats that support it, extension
values of it are created with
ExtValue::borrowed_value::<Number> and
looked up with
ExtValue::downcast_value_ref::<Number>.
Implementations§
Source§impl<'a> Number<'a>
impl<'a> Number<'a>
Sourcepub fn new<T: Into<Cow<'a, str>>>(text: T, value: f64) -> Number<'a>
pub fn new<T: Into<Cow<'a, str>>>(text: T, value: f64) -> Number<'a>
Creates a number from its text and value.
The text has to follow the syntax of JSON numbers and the value has
to be the (possibly approximated) value of the text. This is not
checked in release builds. Use parse to validate the
text and compute the value.
Sourcepub fn parse<T: Into<Cow<'a, str>>>(text: T) -> Result<Number<'a>, Error>
pub fn parse<T: Into<Cow<'a, str>>>(text: T) -> Result<Number<'a>, Error>
Parses a number from its text.
The text has to follow the syntax of JSON numbers.
Sourcepub fn value(&self) -> f64
pub fn value(&self) -> f64
Returns the value of the number as f64.
This might be an approximation of the text.
Sourcepub fn is_integer(&self) -> bool
pub fn is_integer(&self) -> bool
Returns true if the text is an integer (no fraction or exponent).
Sourcepub fn into_owned(self) -> Number<'static>
pub fn into_owned(self) -> Number<'static>
Detaches the number from the data it borrows.
Trait Implementations§
Source§impl BorrowedExtension for Number<'static>
impl BorrowedExtension for Number<'static>
Source§fn name<'v>(_value: &'v Number<'_>) -> &'v str
fn name<'v>(_value: &'v Number<'_>) -> &'v str
Source§fn fallback<'v>(value: &'v Number<'_>) -> Atom<'v>
fn fallback<'v>(value: &'v Number<'_>) -> Atom<'v>
Source§impl<'de, 'a> Deserialize<'de> for Number<'a>
Numbers are deserialized from numbers (and number extension values) and
strings with the syntax of JSON numbers. The text is always owned.
impl<'de, 'a> Deserialize<'de> for Number<'a>
Numbers are deserialized from numbers (and number extension values) and strings with the syntax of JSON numbers. The text is always owned.