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

source

pub fn from_slice(data: &[u8]) -> Result<Self>

Validates &[u8] for utf-8 and parses it into a crate::Value.

source

pub fn from_str(json_str: &str) -> Result<Self>

Takes serialized JSON &str and parses it into a crate::Value.

Clones the passed str.

source

pub fn from_string(json_str: String) -> Result<Self>

Takes serialized JSON String and parses it into a crate::Value.

source

pub fn parse_from(json_str: String) -> Result<Self>

Takes serialized JSON String and parses it into a crate::Value.

source

pub fn get_value(&self) -> &Value<'_>

Returns the Value reference.

Methods from Deref<Target = Value<'static>>§

source

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);
source

pub fn is_null(&self) -> bool

Returns true if Value is Value::Null.

source

pub fn is_array(&self) -> bool

Returns true if Value is Value::Array.

source

pub fn is_object(&self) -> bool

Returns true if Value is Value::Object.

source

pub fn is_bool(&self) -> bool

Returns true if Value is Value::Bool.

source

pub fn is_number(&self) -> bool

Returns true if Value is Value::Number.

source

pub fn is_string(&self) -> bool

Returns true if Value is Value::Str.

source

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.

source

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.

source

pub fn is_f64(&self) -> bool

Returns true if the Value is a f64 number.

source

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.

source

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.

source

pub fn as_array(&self) -> Option<&[Value<'_>]>

If the Value is an Array, returns the associated Array. Returns None otherwise.

source

pub fn as_object(&self) -> Option<&ObjectAsVec<'_>>

If the Value is an Object, returns the associated Object. Returns None otherwise.

source

pub fn as_bool(&self) -> Option<bool>

If the Value is a Boolean, returns the associated bool. Returns None otherwise.

source

pub fn as_str(&self) -> Option<&str>

If the Value is a String, returns the associated str. Returns None otherwise.

source

pub fn as_i64(&self) -> Option<i64>

If the Value is an integer, represent it as i64 if possible. Returns None otherwise.

source

pub fn as_u64(&self) -> Option<u64>

If the Value is an integer, represent it as u64 if possible. Returns None otherwise.

source

pub fn as_f64(&self) -> Option<f64>

If the Value is a number, represent it as f64 if possible. Returns None otherwise.

Trait Implementations§

source§

impl Clone for OwnedValue

source§

fn clone(&self) -> OwnedValue

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for OwnedValue

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Deref for OwnedValue

§

type Target = Value<'static>

The resulting type after dereferencing.
source§

fn deref(&self) -> &Self::Target

Dereferences the value.
source§

impl Hash for OwnedValue

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl PartialEq for OwnedValue

source§

fn eq(&self, other: &OwnedValue) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for OwnedValue

source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl Eq for OwnedValue

source§

impl StructuralPartialEq for OwnedValue

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.