Skip to main content

JsonNumberPolicy

Enum JsonNumberPolicy 

Source
#[non_exhaustive]
pub enum JsonNumberPolicy { Preserve, ExactSafeInteger, }
Expand description

Selects how a validated source number is represented in the output tree.

ExactSafeInteger accepts only exact mathematical integer values in the inclusive interval [-9007199254740991, 9007199254740991]. It checks the decimal value directly, without rounding through f64, and projects an accepted value to i64. Thus, 1.00e2 is accepted as 100, while fractional values and integral values outside that interval are rejected as distinct JsonNumberTokenError categories.

After the scanner recognizes a valid number token, it calls the number policy before admitting that number’s resource measurements. It then checks the selected decode model’s range and applies the selected projection. A policy error returns immediately and can therefore precede a number-resource budget error.

Earlier input, depth, container, and validated-option checks can reject the document before the callback. A successful callback does not imply that resource admission or validation of the complete document succeeded; its side effects are not rolled back after a later failure. Selecting ExactSafeInteger does not bypass the decode model range check, so a spelling such as 1e400 can fail first as JsonValidatedDecodeError::Decode with JsonSyntaxErrorReason::FloatOutOfRange.

JsonNumberToken::exact_safe_integer exposes the same exact conversion for use inside a policy callback.

§Examples

use qubit_json::decode::{
    JsonDecoder, JsonNumberPolicy, JsonNumberTokenError,
    JsonSyntaxErrorReason, JsonValidatedDecodeError,
    JsonValidatedDecodeOptions,
};
use std::convert::Infallible;

let options = JsonValidatedDecodeOptions::builder().build();
let decode = |text| {
    JsonDecoder::unlimited().decode_unique_validated_str(
        text,
        &options,
        |_, _| Ok::<_, Infallible>(JsonNumberPolicy::ExactSafeInteger),
    )
};

let owner = decode("1.00e2").unwrap();
assert_eq!(owner.as_value().as_i64(), Some(100));
assert!(matches!(
    decode("0.1"),
    Err(JsonValidatedDecodeError::ExactSafeInteger {
        source: JsonNumberTokenError::Fractional,
        ..
    })
));
assert!(matches!(
    decode("1e400"),
    Err(JsonValidatedDecodeError::Decode { error, .. })
        if error.syntax_error().map(|syntax| syntax.reason())
            == Some(JsonSyntaxErrorReason::FloatOutOfRange)
));

Variants (Non-exhaustive)§

This enum is marked as non-exhaustive
Non-exhaustive enums could have additional variants added in future. Therefore, when matching against variants of non-exhaustive enums, an extra wildcard arm must be added to account for any future variants.
§

Preserve

Preserve the number according to the configured decode model.

§

ExactSafeInteger

Require an exact integer in JavaScript’s safe integer range.

The range is [-9007199254740991, 9007199254740991]. The decimal mathematical value is checked without an intermediate f64 rounding step, then projected to i64. Fractional values produce JsonNumberTokenError::Fractional; integral values outside the range produce JsonNumberTokenError::OutOfSafeIntegerRange.

Trait Implementations§

Source§

impl Clone for JsonNumberPolicy

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Copy for JsonNumberPolicy

Source§

impl Debug for JsonNumberPolicy

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Eq for JsonNumberPolicy

Source§

impl Hash for JsonNumberPolicy

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for JsonNumberPolicy

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for JsonNumberPolicy

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.