pub enum JValue {
Null,
Bool(bool),
Number(Number),
String(JsonString),
Array(Rc<[JValue]>),
Object(Rc<Map<JsonString, JValue>>),
}Expand description
Represents any valid JSON value with a cheap to clone Rc-based representation.
Variants§
Null
Represents a JSON null value.
Bool(bool)
Represents a JSON boolean.
Number(Number)
Represents a JSON number, whether integer or floating point.
String(JsonString)
Represents a JSON string.
Array(Rc<[JValue]>)
Represents a JSON array.
Object(Rc<Map<JsonString, JValue>>)
Represents a JSON object.
By default the map is backed by a BTreeMap. Enable the preserve_order
feature of serde_json to use IndexMap instead, which preserves
entries in the order they are inserted into the map. In particular, this
allows JSON data to be deserialized into a JValue and serialized to a
string while retaining the order of map keys in the input.
Implementations§
Source§impl JValue
impl JValue
pub fn string(s: impl Into<Rc<str>>) -> Self
pub fn array(vec: impl Into<Rc<[JValue]>>) -> Self
pub fn array_from_iter( into_iter: impl IntoIterator<Item = impl Into<JValue>>, ) -> Self
pub fn object(map: impl Into<Map<JsonString, JValue>>) -> Self
pub fn object_from_pairs( into_iter: impl IntoIterator<Item = (impl Into<JsonString>, impl Into<JValue>)>, ) -> Self
Sourcepub fn get<I: Index>(&self, index: I) -> Option<&JValue>
pub fn get<I: Index>(&self, index: I) -> Option<&JValue>
Index into a JSON array or map. A string index can be used to access a value in a map, and a usize index can be used to access an element of an array.
Returns None if the type of self does not match the type of the
index, for example if the index is a string and self is an array or a
number. Also returns None if the given key does not exist in the map
or the given index is not within the bounds of the array.
Square brackets can also be used to index into a value in a more concise
way. This returns JValue::Null in cases where get would have returned
None.
Sourcepub fn is_object(&self) -> bool
pub fn is_object(&self) -> bool
Returns true if the JValue is an Object. Returns false otherwise.
For any JValue on which is_object returns true, as_object and
as_object_mut are guaranteed to return the map representation of the
object.
Sourcepub fn as_object(&self) -> Option<&Map<JsonString, JValue>>
pub fn as_object(&self) -> Option<&Map<JsonString, JValue>>
If the JValue is an Object, returns the associated Map. Returns None
otherwise.
Sourcepub fn is_array(&self) -> bool
pub fn is_array(&self) -> bool
Returns true if the JValue is an Array. Returns false otherwise.
For any JValue on which is_array returns true, as_array and
as_array_mut are guaranteed to return the vector representing the
array.
Sourcepub fn as_array(&self) -> Option<&[JValue]>
pub fn as_array(&self) -> Option<&[JValue]>
If the JValue is an Array, returns the associated vector. Returns None
otherwise.
Sourcepub fn is_string(&self) -> bool
pub fn is_string(&self) -> bool
Returns true if the JValue is a String. Returns false otherwise.
For any JValue on which is_string returns true, as_str is guaranteed
to return the string slice.
Sourcepub fn as_str(&self) -> Option<&JsonString>
pub fn as_str(&self) -> Option<&JsonString>
If the JValue is a string, returns the associated str. Returns None
otherwise.
Sourcepub fn is_number(&self) -> bool
pub fn is_number(&self) -> bool
Returns true if the JValue is a Number. Returns false otherwise.
Sourcepub fn as_number(&self) -> Option<&Number>
pub fn as_number(&self) -> Option<&Number>
If the JValue is a Number, returns the associated Number. Returns
None otherwise.
Sourcepub fn is_i64(&self) -> bool
pub fn is_i64(&self) -> bool
Returns true if the JValue is an integer between i64::MIN and
i64::MAX.
For any JValue on which is_i64 returns true, as_i64 is guaranteed to
return the integer value.
Sourcepub fn is_u64(&self) -> bool
pub fn is_u64(&self) -> bool
Returns true if the JValue is an integer between zero and u64::MAX.
For any JValue on which is_u64 returns true, as_u64 is guaranteed to
return the integer value.
Sourcepub fn is_f64(&self) -> bool
pub fn is_f64(&self) -> bool
Returns true if the JValue is a number that can be represented by f64.
For any JValue on which is_f64 returns true, as_f64 is guaranteed to
return the floating point value.
Currently this function returns true if and only if both is_i64 and
is_u64 return false but this is not a guarantee in the future.
Sourcepub fn as_i64(&self) -> Option<i64>
pub fn as_i64(&self) -> Option<i64>
If the JValue is an integer, represent it as i64 if possible. Returns
None otherwise.
Sourcepub fn as_u64(&self) -> Option<u64>
pub fn as_u64(&self) -> Option<u64>
If the JValue is an integer, represent it as u64 if possible. Returns
None otherwise.
Sourcepub fn as_f64(&self) -> Option<f64>
pub fn as_f64(&self) -> Option<f64>
If the JValue is a number, represent it as f64 if possible. Returns
None otherwise.
Sourcepub fn is_boolean(&self) -> bool
pub fn is_boolean(&self) -> bool
Returns true if the JValue is a Boolean. Returns false otherwise.
For any JValue on which is_boolean returns true, as_bool is
guaranteed to return the boolean value.
Sourcepub fn as_bool(&self) -> Option<bool>
pub fn as_bool(&self) -> Option<bool>
If the JValue is a Boolean, returns the associated bool. Returns None
otherwise.
Sourcepub fn is_null(&self) -> bool
pub fn is_null(&self) -> bool
Returns true if the JValue is a Null. Returns false otherwise.
For any JValue on which is_null returns true, as_null is guaranteed
to return Some(()).
Sourcepub fn as_null(&self) -> Option<()>
pub fn as_null(&self) -> Option<()>
If the JValue is a Null, returns (). Returns None otherwise.
Sourcepub fn pointer(&self, pointer: &str) -> Option<&JValue>
pub fn pointer(&self, pointer: &str) -> Option<&JValue>
Looks up a value by a JSON Pointer.
JSON Pointer defines a string syntax for identifying a specific value within a JavaScript Object Notation (JSON) document.
A Pointer is a Unicode string with the reference tokens separated by /.
Inside tokens / is replaced by ~1 and ~ is replaced by ~0. The
addressed value is returned and if there is no such value None is
returned.
For more information read RFC6901.
Trait Implementations§
Source§impl<'de> Deserialize<'de> for JValue
impl<'de> Deserialize<'de> for JValue
Source§fn deserialize<D>(deserializer: D) -> Result<JValue, D::Error>where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<JValue, D::Error>where
D: Deserializer<'de>,
Source§impl<'a> From<Cow<'a, str>> for JValue
impl<'a> From<Cow<'a, str>> for JValue
Source§fn from(f: Cow<'a, str>) -> Self
fn from(f: Cow<'a, str>) -> Self
Convert copy-on-write string to JValue::String.
§Examples
use air_interpreter_value::JValue;
use std::borrow::Cow;
let s: Cow<str> = Cow::Borrowed("lorem");
let x: JValue = s.into();use air_interpreter_value::JValue;
use std::borrow::Cow;
let s: Cow<str> = Cow::Owned("lorem".to_string());
let x: JValue = s.into();Source§impl From<Rc<str>> for JValue
impl From<Rc<str>> for JValue
Source§fn from(f: JsonString) -> Self
fn from(f: JsonString) -> Self
Convert JsonString to JValue::String.
§Examples
use air_interpreter_value::JValue;
let s: String = "lorem".to_string();
let x: JValue = s.into();Source§impl<K: Into<JsonString>, V: Into<JValue>> FromIterator<(K, V)> for JValue
impl<K: Into<JsonString>, V: Into<JValue>> FromIterator<(K, V)> for JValue
Source§impl<T: Into<JValue>> FromIterator<T> for JValue
impl<T: Into<JValue>> FromIterator<T> for JValue
Source§fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
fn from_iter<I: IntoIterator<Item = T>>(iter: I) -> Self
Create a JValue::Array by collecting an iterator of array elements.
§Examples
use air_interpreter_value::JValue;
let v = std::iter::repeat(42).take(5);
let x: JValue = v.collect();use air_interpreter_value::JValue;
let v: Vec<_> = vec!["lorem", "ipsum", "dolor"];
let x: JValue = v.into_iter().collect();use std::iter::FromIterator;
use air_interpreter_value::JValue;
let x: JValue = JValue::from_iter(vec!["lorem", "ipsum", "dolor"]);Source§impl<I> Index<I> for JValuewhere
I: Index,
impl<I> Index<I> for JValuewhere
I: Index,
Source§fn index(&self, index: I) -> &JValue
fn index(&self, index: I) -> &JValue
Index into a air_interpreter_value::JValue using the syntax value[0] or
value["k"].
Returns JValue::Null if the type of self does not match the type of
the index, for example if the index is a string and self is an array
or a number. Also returns JValue::Null if the given key does not exist
in the map or the given index is not within the bounds of the array.
For retrieving deeply nested values, you should have a look at the
JValue::pointer method.