pub enum Val {
Null,
Bool(bool),
Int(isize),
Float(f64),
Num(Rc<String>),
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 machine-sized integers and 64-bit floating-point numbers. 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.
- Any other operation between two numbers yields a float.
Variants§
Null
Null
Bool(bool)
Boolean
Int(isize)
Integer
Float(f64)
Floating-point number
Num(Rc<String>)
Floating-point number or integer not fitting into Int
Str(Rc<String>)
String
Arr(Rc<Vec<Val>>)
Array
Obj(Rc<IndexMap<Rc<String>, Val, RandomState>>)
Object
Implementations§
Source§impl Val
impl Val
Sourcepub fn as_float(&self) -> Result<f64, Error>
pub fn as_float(&self) -> Result<f64, Error>
If the value is or can be converted to float, return it, else fail.
Sourcepub fn to_str(self) -> Result<Rc<String>, Error>
pub fn to_str(self) -> Result<Rc<String>, Error>
If the value is a string, return it, else fail.
Sourcepub fn as_str(&self) -> Result<&Rc<String>, Error>
pub fn as_str(&self) -> Result<&Rc<String>, Error>
If the value is a string, return it, else fail.
Sourcepub fn to_string_or_clone(&self) -> String
pub fn to_string_or_clone(&self) -> String
If the value is a Str, extract the inner string, else convert it to string.
Sourcepub fn into_arr(self) -> Result<Rc<Vec<Self>>, Error>
pub fn into_arr(self) -> Result<Rc<Vec<Self>>, Error>
If the value is an array, return it, else fail.
Sourcepub fn as_arr(&self) -> Result<&Rc<Vec<Self>>, Error>
pub fn as_arr(&self) -> Result<&Rc<Vec<Self>>, Error>
If the value is an array, return it, else fail.
Sourcepub fn from_dec_str(n: &str) -> Self
pub fn from_dec_str(n: &str) -> Self
Try to parse a string to a Self::Float, else return Self::Null.
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 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 arrays nor objects.
Sourcepub fn keys_unsorted(&self) -> Result<Vec<Self>, Error>
pub fn keys_unsorted(&self) -> Result<Vec<Self>, Error>
Return any key for which value | .[key] is defined.
Fail on values that are neither arrays nor objects.
Sourcepub fn try_into_iter(self) -> Result<Box<dyn Iterator<Item = Self>>, Error>
👎Deprecated since 1.3.0: use ValT::values instead
pub fn try_into_iter(self) -> Result<Box<dyn Iterator<Item = Self>>, Error>
ValT::values insteadReturn 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 aequalsb.
Sourcepub fn mutate_str(self, f: impl Fn(&mut String)) -> ValR
pub fn mutate_str(self, f: impl Fn(&mut String)) -> ValR
Apply a function to a string.
Sourcepub fn mutate_arr(self, f: impl Fn(&mut Vec<Self>)) -> ValR
pub fn mutate_arr(self, f: impl Fn(&mut Vec<Self>)) -> ValR
Apply a function to an array.
Sourcepub fn try_mutate_arr(
self,
f: impl Fn(&mut Vec<Self>) -> Result<(), Error>,
) -> ValR
pub fn try_mutate_arr( self, f: impl Fn(&mut Vec<Self>) -> Result<(), Error>, ) -> ValR
Apply a fallible function to an array.
Sourcepub fn parse(token: Token, lexer: &mut impl LexAlloc) -> Result<Self, Error>
pub fn parse(token: Token, lexer: &mut impl LexAlloc) -> Result<Self, Error>
Parse at least one JSON value, given an initial token and a lexer.
If the underlying lexer reads input fallibly (for example IterLexer),
the error returned by this function might be misleading.
In that case, always check whether the lexer contains an error.
Trait Implementations§
Source§impl FromIterator<Val> for Val
impl FromIterator<Val> for Val
Source§fn from_iter<T: IntoIterator<Item = Self>>(iter: T) -> Self
fn from_iter<T: IntoIterator<Item = Self>>(iter: T) -> Self
Source§impl Ord for Val
impl Ord for Val
Source§impl PartialOrd for Val
impl PartialOrd for Val
Source§impl ValT for Val
impl ValT for Val
Source§fn from_map<I: IntoIterator<Item = (Self, Self)>>(
iter: I,
) -> Result<Self, Error<Self>>
fn from_map<I: IntoIterator<Item = (Self, Self)>>( iter: I, ) -> Result<Self, Error<Self>>
Source§fn values(self) -> Box<dyn Iterator<Item = Result<Self, Error<Self>>>>
fn values(self) -> Box<dyn Iterator<Item = Result<Self, Error<Self>>>>
Source§fn range(self, range: Range<Option<&Self>>) -> ValR
fn range(self, range: Range<Option<&Self>>) -> ValR
Source§fn map_values<I: Iterator<Item = ValR>>(
self,
opt: Opt,
f: impl Fn(Self) -> I,
) -> ValR
fn map_values<I: Iterator<Item = ValR>>( self, opt: Opt, f: impl Fn(Self) -> I, ) -> ValR
Source§fn map_index<I: Iterator<Item = ValR>>(
self,
index: &Self,
opt: Opt,
f: impl Fn(Self) -> I,
) -> ValR
fn map_index<I: Iterator<Item = ValR>>( self, index: &Self, opt: Opt, f: impl Fn(Self) -> I, ) -> ValR
impl Eq for Val
Auto Trait Implementations§
impl Freeze for Val
impl RefUnwindSafe for Val
impl !Send for Val
impl !Sync for Val
impl Unpin for Val
impl UnwindSafe for Val
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.