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 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 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 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<Vec<Val>, Error>
pub fn keys(&self) -> Result<Vec<Val>, Error>
Return any key for which value | .[key] is defined.
Fail on values that are neither arrays nor objects.
sourcepub fn into_iter(self) -> Result<Box<dyn Iterator<Item = Val>>, Error>
pub fn into_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 walk<'a>(
self,
f: &impl Fn(Val) -> Box<dyn Iterator<Item = ValR> + 'a>
) -> Box<dyn Iterator<Item = ValR> + 'a>
pub fn walk<'a>( self, f: &impl Fn(Val) -> Box<dyn Iterator<Item = ValR> + 'a> ) -> Box<dyn Iterator<Item = ValR> + 'a>
Recursively apply a function to each 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`.
sourcepub fn from_json(&self) -> ValR
pub fn from_json(&self) -> ValR
Convert string to a single JSON value.
Fail on any other value.
sourcepub fn explode(&self) -> Result<Vec<Val>, Error>
pub fn explode(&self) -> Result<Vec<Val>, Error>
Convert a string into an array of its Unicode codepoints.
sourcepub fn implode(&self) -> Result<String, Error>
pub fn implode(&self) -> Result<String, Error>
Convert an array of Unicode codepoints into a string.
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 sort_by<'a>(
self,
f: impl Fn(Val) -> Box<dyn Iterator<Item = ValR> + 'a>
) -> ValR
pub fn sort_by<'a>( self, f: impl Fn(Val) -> Box<dyn Iterator<Item = ValR> + 'a> ) -> ValR
Sort array by the given function.
Fail on any other value.
sourcepub fn split(&self, sep: &Self) -> Result<Vec<Val>, Error>
pub fn split(&self, sep: &Self) -> Result<Vec<Val>, Error>
Split a string by a given separator string.
Fail if any of the two given values is not a string.
sourcepub fn regex(
&self,
re: &Self,
flags: &Self,
sm: (bool, bool)
) -> Result<Vec<Val>, Error>
pub fn regex( &self, re: &Self, flags: &Self, sm: (bool, bool) ) -> Result<Vec<Val>, Error>
Apply a regular expression to the given input value.
sm indicates whether to
- output strings that do not match the regex, and
- output the matches.
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 Ord for Val
impl Ord for Val
source§impl PartialEq<Val> for Val
impl PartialEq<Val> for Val
source§impl PartialOrd<Val> for Val
impl PartialOrd<Val> for Val
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self and other) and is used by the <=
operator. Read moreimpl 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§
§impl<T> Chain<T> for T
impl<T> Chain<T> for T
source§impl<Q, K> Equivalent<K> for Qwhere
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Qwhere Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,
source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.