Struct serde_json_borrow::OwnedValue
source · pub struct OwnedValue { /* private fields */ }
Expand description
Parses a String
into Value
, by taking ownership of String
and reference slices from it in
contrast to copying the contents.
This is done to mitigate lifetime issues.
Implementations§
source§impl OwnedValue
impl OwnedValue
sourcepub fn from_slice(data: &[u8]) -> Result<Self>
pub fn from_slice(data: &[u8]) -> Result<Self>
Validates &[u8]
for utf-8 and parses it into a crate::Value.
sourcepub fn from_str(json_str: &str) -> Result<Self>
pub fn from_str(json_str: &str) -> Result<Self>
Takes serialized JSON &str
and parses it into a crate::Value.
Clones the passed str.
sourcepub fn from_string(json_str: String) -> Result<Self>
pub fn from_string(json_str: String) -> Result<Self>
Takes serialized JSON String
and parses it into a crate::Value.
sourcepub fn parse_from(json_str: String) -> Result<Self>
pub fn parse_from(json_str: String) -> Result<Self>
Takes serialized JSON String
and parses it into a crate::Value.
Methods from Deref<Target = Value<'static>>§
sourcepub fn get<I: Index<'ctx>>(&'ctx self, index: I) -> &'ctx Value<'ctx>
pub fn get<I: Index<'ctx>>(&'ctx self, index: I) -> &'ctx Value<'ctx>
Index into a serde_json_borrow::Value
using the syntax value.get(0)
or
value.get("k")
.
Returns Value::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 Value::Null
if the given key does not exist
in the map or the given index is not within the bounds of the array.
§Examples
let json_obj = r#"
{
"x": {
"y": ["z", "zz"]
}
}
"#;
let data: Value = serde_json::from_str(json_obj).unwrap();
assert_eq!(data.get("x").get("y").get(0), &Value::Str("z".into()));
assert_eq!(data.get("x").get("y").get(1), &Value::Str("zz".into()));
assert_eq!(data.get("x").get("y").get(2), &Value::Null);
assert_eq!(data.get("a"), &Value::Null);
assert_eq!(data.get("a").get("b"), &Value::Null);
sourcepub fn is_i64(&self) -> bool
pub fn is_i64(&self) -> bool
Returns true if the Value is an integer between i64::MIN and i64::MAX. For any Value 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 Value is an integer between zero and u64::MAX. For any Value on which is_u64 returns true, as_u64 is guaranteed to return the integer value.
sourcepub fn iter_array(&self) -> Option<impl Iterator<Item = &Value<'_>>>
pub fn iter_array(&self) -> Option<impl Iterator<Item = &Value<'_>>>
If the Value is an Array, returns an iterator over the elements in the array.
sourcepub fn iter_object(&self) -> Option<impl Iterator<Item = (&str, &Value<'_>)>>
pub fn iter_object(&self) -> Option<impl Iterator<Item = (&str, &Value<'_>)>>
If the Value is an Object, returns an iterator over the elements in the object.
sourcepub fn as_array(&self) -> Option<&[Value<'_>]>
pub fn as_array(&self) -> Option<&[Value<'_>]>
If the Value is an Array, returns the associated Array. Returns None otherwise.
sourcepub fn as_object(&self) -> Option<&ObjectAsVec<'_>>
pub fn as_object(&self) -> Option<&ObjectAsVec<'_>>
If the Value is an Object, returns the associated Object. Returns None otherwise.
sourcepub fn as_bool(&self) -> Option<bool>
pub fn as_bool(&self) -> Option<bool>
If the Value is a Boolean, returns the associated bool. Returns None otherwise.
sourcepub fn as_str(&self) -> Option<&str>
pub fn as_str(&self) -> Option<&str>
If the Value is a String, returns the associated str. Returns None otherwise.
sourcepub fn as_i64(&self) -> Option<i64>
pub fn as_i64(&self) -> Option<i64>
If the Value is an integer, represent it as i64 if possible. Returns None otherwise.
Trait Implementations§
source§impl Clone for OwnedValue
impl Clone for OwnedValue
source§fn clone(&self) -> OwnedValue
fn clone(&self) -> OwnedValue
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for OwnedValue
impl Debug for OwnedValue
source§impl Deref for OwnedValue
impl Deref for OwnedValue
source§impl Hash for OwnedValue
impl Hash for OwnedValue
source§impl PartialEq for OwnedValue
impl PartialEq for OwnedValue
source§fn eq(&self, other: &OwnedValue) -> bool
fn eq(&self, other: &OwnedValue) -> bool
self
and other
values to be equal, and is used
by ==
.