pub enum Val {
Null,
Bool(bool),
Pos(usize),
Neg(usize),
Float(f64),
Num(Rc<Number>),
Str(Rc<String>),
Arr(Rc<Vec<Val>>),
Obj(Rc<IndexMap<Rc<String>, Val, RandomState>>),
}Expand description
JSON value with sharing.
The speciality of this type is that numbers are distinguished into positive integers, negative integers, and 64-bit floating-point numbers. All integers are machine-sized. This allows using integers to index arrays, while using floating-point numbers to do general math.
Operations on numbers follow a few principles:
- The sum, difference, product, and remainder of two integers is integer.
- The quotient of two integers is integer if the remainder of the integers is zero.
- Any other operation between two numbers yields a float.
Variants
Null
Bool(bool)
Boolean
Pos(usize)
Positive integer
Neg(usize)
Negative integer
Float(f64)
Floating-point number
Num(Rc<Number>)
Floating-point number or integer not fitting neither into Pos nor Neg
Str(Rc<String>)
String
Arr(Rc<Vec<Val>>)
Array
Obj(Rc<IndexMap<Rc<String>, Val, RandomState>>)
Order-preserving map
Implementations
sourceimpl Val
impl Val
sourcepub fn as_usize(&self) -> Result<usize, Error>
pub fn as_usize(&self) -> Result<usize, Error>
If the value is a positive integer, return it, else fail.
sourcepub fn as_int(&self) -> Result<(usize, bool), Error>
pub fn as_int(&self) -> Result<(usize, bool), Error>
If the value is integer, return its absolute value and whether its positive, else fail.
sourcepub fn as_obj_key(&self) -> Result<Rc<String>, Error>
pub fn as_obj_key(&self) -> Result<Rc<String>, Error>
If the value is a string, return it, else fail.
sourcepub fn len(&self) -> Result<Self, Error>
pub fn len(&self) -> Result<Self, Error>
Return 0 for null, the absolute value for numbers, and the length for strings, arrays, and objects.
Fail on booleans.
sourcepub fn round(&self, f: impl FnOnce(f64) -> f64) -> Result<Self, Error>
pub fn round(&self, f: impl FnOnce(f64) -> f64) -> Result<Self, Error>
Apply a rounding function to floating-point numbers, then convert them to integers.
Return integers unchanged, and fail on any other input.
sourcepub fn range(
&self,
other: &Self
) -> Result<Box<dyn Iterator<Item = Self>>, Error>
pub fn range(
&self,
other: &Self
) -> Result<Box<dyn Iterator<Item = Self>>, Error>
Return all numbers in the range [self, other) if both inputs are integer, else fail.
sourcepub fn has(&self, key: &Self) -> Result<bool, Error>
pub fn has(&self, key: &Self) -> Result<bool, Error>
Return true if value | .[key] is defined.
Fail on values that are neither null, arrays, nor objects.
sourcepub fn keys(&self) -> Result<Box<dyn Iterator<Item = Val> + '_>, Error>
pub fn keys(&self) -> Result<Box<dyn Iterator<Item = Val> + '_>, Error>
Return any key for which value | .[key] is defined.
Fail on values that are neither arrays nor objects.
sourcepub fn iter(&self) -> Result<Box<dyn Iterator<Item = Val> + '_>, Error>
pub fn iter(&self) -> Result<Box<dyn Iterator<Item = Val> + '_>, Error>
Return the elements of an array or the values of an object (omitting its keys).
Fail on any other value.
sourcepub fn contains(&self, other: &Self) -> bool
pub fn contains(&self, other: &Self) -> bool
a contains b iff either
- the string
bis a substring ofa, - every element in the array
bis contained in some element of the arraya, - for every key-value pair
k, vinb, there is a key-value pairk, v'inasuch thatv'containsv, or a equalsb`.
Trait Implementations
sourceimpl Ord for Val
impl Ord for Val
sourceimpl PartialOrd<Val> for Val
impl PartialOrd<Val> for Val
sourcefn partial_cmp(&self, other: &Self) -> Option<Ordering>
fn partial_cmp(&self, other: &Self) -> Option<Ordering>
This method returns an ordering between self and other values if one exists. Read more
1.0.0 · sourcefn lt(&self, other: &Rhs) -> bool
fn lt(&self, other: &Rhs) -> bool
This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · sourcefn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
This method tests less than or equal to (for self and other) and is used by the <=
operator. Read more
impl Eq for Val
Auto Trait Implementations
impl RefUnwindSafe for Val
impl !Send for Val
impl !Sync for Val
impl Unpin for Val
impl UnwindSafe for Val
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
impl<T> Container<T> for T where
T: Clone,
impl<T> Container<T> for T where
T: Clone,
sourceimpl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcefn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to key and return true if they are equal.
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into)Uses borrowed data to replace owned data, usually by cloning. Read more