JsonNumber structv0.1.0
pub struct JsonNumber {
// private fields
}
JSON does not say what precision a number has, so nothing here decides on the caller's behalf. Ask for the
shape wanted — AsInt64, AsUint64, AsFloat64 — and each reports whether the value fits. Keeping the
text is also what lets a document round-trip exactly, which a parser converting to a double cannot do.
Constructors
JsonNumber
pub func JsonNumber(
allocator: Allocator,
text: char8[..]
) -> JsonNumber throws JsonError
A number from its text, which is checked against JSON's number grammar and copied.
The text must be the whole number as RFC 8259 spells it: an optional minus, an integer part with no leading
zero, and an optional fraction and exponent, each with at least one digit. Anything else, such as 01, +1,
.5, 5., 1e, NaN or an empty text, is refused with MalformedNumber before anything is allocated, so a
number that exists is one every JSON reader accepts. Terminates the program when the copy cannot be made.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
text | char8[..] | the number's text, borrowed for the call |
Returns: the number, holding a copy of the text; MalformedNumber when the text is not a JSON number
Methods
Text
pub func Text(&self) -> StringView
The number exactly as written.
Returns: the number's text, exactly as the input spelled it
AsInt64
pub func AsInt64(&self) -> int64?
The value as a signed integer, or none if it does not fit one.
A number with a fraction or an exponent does not fit, even when its value happens to be whole: 1.0 is
not an integer in JSON's grammar and pretending otherwise loses the distinction a document was making.
Returns: the value, or none when the text is not an integer that fits
AsUint64
pub func AsUint64(&self) -> uint64?
The value as an unsigned integer, or none if it does not fit one.
Returns: the value, or none when the text is not a non-negative integer that fits
AsFloat64
pub func AsFloat64(&self) -> float64?
The value as a float, or none if the text is not a number at all.
Succeeds for values a float cannot hold exactly, since that is what a float is for; a caller who needs
exactness should ask for an integer or read the text.
Returns: the nearest representable value, or none when the text did not convert
IsIntegral
pub func IsIntegral(&self) -> bool
Whether the text has no fraction and no exponent, so it denotes an integer.
Returns: whether the text has no fraction and no exponent
Release
pub func Release(&var self)
Gives the text back.