Skip to main content

JsonDecodeNumberModel

Enum JsonDecodeNumberModel 

Source
#[non_exhaustive]
pub enum JsonDecodeNumberModel { Integer64, Integer64WithFiniteFallback, }
Expand description

Controls lexical admission of syntactically valid JSON number tokens.

Typed decoding still lets Serde construct the requested target type, so its numeric representation can depend on that type and the active Serde features. Validated decoding constructs its owned value according to this model and the selected number policy.

§Examples

The default model rejects an integer above u64::MAX. The finite fallback admits it as an f64, which rounds this value and loses integer precision. This example uses a controlled input and an unlimited decoder for illustration; use finite limits for untrusted input. Typed decoding still lets the target type and active features determine its representation. Use a string or a domain-specific format when exact large integers are needed. See JsonValidatedDecodeOptions and the JSON number contract.

use std::convert::Infallible;

use qubit_json::decode::JsonDecodeErrorKind;
use qubit_json::decode::JsonDecodeNumberModel;
use qubit_json::decode::JsonDecoder;
use qubit_json::decode::JsonNumberPolicy;
use qubit_json::decode::JsonValidatedDecodeError;
use qubit_json::decode::JsonValidatedDecodeOptions;

let input = "18446744073709551617";
let strict = JsonValidatedDecodeOptions::builder().build();
let error = JsonDecoder::unlimited()
    .decode_unique_validated_str(input, &strict, |_, _| {
        Ok::<_, Infallible>(JsonNumberPolicy::Preserve)
    })
    .expect_err("the default model rejects integers above u64::MAX");
match error {
    JsonValidatedDecodeError::Decode { error, .. } => {
        assert_eq!(error.kind(), JsonDecodeErrorKind::InvalidJson);
    }
    other => panic!("unexpected error: {other:?}"),
}

let fallback = JsonValidatedDecodeOptions::builder()
    .number_model(JsonDecodeNumberModel::Integer64WithFiniteFallback)
    .build();
let owner = JsonDecoder::unlimited()
    .decode_unique_validated_str(input, &fallback, |_, _| {
        Ok::<_, Infallible>(JsonNumberPolicy::Preserve)
    })
    .expect("finite fallback admits the token with rounding");
assert!(owner.as_value().is_f64());
assert_eq!(owner.as_value().as_f64(), Some(18446744073709551616_f64));

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.
§

Integer64

Accept signed and unsigned 64-bit integers and finite floating values.

§

Integer64WithFiniteFallback

Also admit out-of-range integer tokens when they convert to finite f64 values.

Validated decoding with JsonNumberPolicy::Preserve materializes these tokens as finite f64; typed decoding leaves their representation to Serde for the requested target type.

Trait Implementations§

Source§

impl Clone for JsonDecodeNumberModel

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 JsonDecodeNumberModel

Source§

impl Debug for JsonDecodeNumberModel

Source§

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

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

impl Default for JsonDecodeNumberModel

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Eq for JsonDecodeNumberModel

Source§

impl Hash for JsonDecodeNumberModel

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 JsonDecodeNumberModel

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 JsonDecodeNumberModel

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.