JsonNumber structv0.1.0

Source
A JSON number, held as the text it was written with.
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.

NameTypeDescription
allocatorAllocatorwhere the storage comes from
textchar8[..]the text to act on

Returns: the number, holding the text it was given

Source

Methods

Text

pub func Text(self: &JsonNumber) -> StringView

The number exactly as written.

Returns: the number's text, exactly as the input spelled it

Source

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.

NameTypeDescription
result*var int64receives the value when this reports true

Returns: whether the text is an integer that fits

Source

AsUint64

pub func AsUint64(
    self: &JsonNumber,
    result: *var uint64
) -> bool

The value as an unsigned integer, or false if it does not fit one.

NameTypeDescription
result*var uint64receives the value when this reports true

Returns: whether the text is a non-negative integer that fits

Source

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.

NameTypeDescription
result*var float64receives the value when this reports true

Returns: whether the text converted; the value is the nearest representable one

Source

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

Source

Release

pub func Release(self: &var JsonNumber)

Gives the text back.

Source