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.
Associated functions
New
pub func New(
allocator: Allocator,
text: char8[..]
) -> JsonNumber ! TextError
A number from its text, which is copied.
| Name | Type | Description |
|---|---|---|
allocator | Allocator | where the storage comes from |
text | char8[..] | the text to act on |
Returns: the number, holding the text it was given
Methods
Text
pub func Text(self: &JsonNumber) -> StringView
The number exactly as written.
Returns: the number's text, exactly as the input spelled it
AsInt64
pub func AsInt64(
self: &JsonNumber,
result: *var int64
) -> bool
The value as a signed integer, or false 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.
| Name | Type | Description |
|---|---|---|
result | *var int64 | receives the value when this reports true |
Returns: whether the text is an integer that fits
AsUint64
pub func AsUint64(
self: &JsonNumber,
result: *var uint64
) -> bool
The value as an unsigned integer, or false if it does not fit one.
| Name | Type | Description |
|---|---|---|
result | *var uint64 | receives the value when this reports true |
Returns: whether the text is a non-negative integer that fits
AsFloat64
pub func AsFloat64(
self: &JsonNumber,
result: *var float64
) -> bool
The value as a float, or false 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.
| Name | Type | Description |
|---|---|---|
result | *var float64 | receives the value when this reports true |
Returns: whether the text converted; the value is the nearest representable one
IsIntegral
pub func IsIntegral(self: &JsonNumber) -> 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(self: &var JsonNumber)
Gives the text back.