Enum air_interpreter_value::JValue
source · 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.
source§impl PartialEq<&str> for JValue
impl PartialEq<&str> for JValue
source§impl PartialEq<JValue> for &str
impl PartialEq<JValue> for &str
source§impl PartialEq<JValue> for String
impl PartialEq<JValue> for String
source§impl PartialEq<JValue> for bool
impl PartialEq<JValue> for bool
source§impl PartialEq<JValue> for f32
impl PartialEq<JValue> for f32
source§impl PartialEq<JValue> for f64
impl PartialEq<JValue> for f64
source§impl PartialEq<JValue> for i16
impl PartialEq<JValue> for i16
source§impl PartialEq<JValue> for i32
impl PartialEq<JValue> for i32
source§impl PartialEq<JValue> for i64
impl PartialEq<JValue> for i64
source§impl PartialEq<JValue> for i8
impl PartialEq<JValue> for i8
source§impl PartialEq<JValue> for isize
impl PartialEq<JValue> for isize
source§impl PartialEq<JValue> for str
impl PartialEq<JValue> for str
source§impl PartialEq<JValue> for u16
impl PartialEq<JValue> for u16
source§impl PartialEq<JValue> for u32
impl PartialEq<JValue> for u32
source§impl PartialEq<JValue> for u64
impl PartialEq<JValue> for u64
source§impl PartialEq<JValue> for u8
impl PartialEq<JValue> for u8
source§impl PartialEq<JValue> for usize
impl PartialEq<JValue> for usize
source§impl PartialEq<String> for JValue
impl PartialEq<String> for JValue
source§impl<'a> PartialEq<bool> for &'a JValue
impl<'a> PartialEq<bool> for &'a JValue
source§impl PartialEq<bool> for JValue
impl PartialEq<bool> for JValue
source§impl<'a> PartialEq<isize> for &'a JValue
impl<'a> PartialEq<isize> for &'a JValue
source§impl PartialEq<isize> for JValue
impl PartialEq<isize> for JValue
source§impl<'a> PartialEq<usize> for &'a JValue
impl<'a> PartialEq<usize> for &'a JValue
source§impl PartialEq<usize> for JValue
impl PartialEq<usize> for JValue
source§impl PartialEq for JValue
impl PartialEq for JValue
impl Eq for JValue
impl StructuralPartialEq for JValue
Auto Trait Implementations§
impl Freeze for JValue
impl RefUnwindSafe for JValue
impl !Send for JValue
impl !Sync for JValue
impl Unpin for JValue
impl UnwindSafe for JValue
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§default unsafe fn clone_to_uninit(&self, dst: *mut T)
default unsafe fn clone_to_uninit(&self, dst: *mut T)
clone_to_uninit
)