Struct serde_json_borrow::Map

source ·
pub struct Map<'ctx>(/* private fields */);
Expand description

Represents a JSON key/value type.

For performance reasons we use a Vec instead of a Hashmap. This comes with a tradeoff of slower key accesses as we need to iterate and compare.

The ObjectAsVec struct is a wrapper around a Vec of (&str, Value) pairs. It provides methods to make it easy to migrate from serde_json::Value::Object or serde_json::Map.

Implementations§

source§

impl<'ctx> ObjectAsVec<'ctx>

source

pub fn as_vec(&self) -> &Vec<(KeyStrType<'_>, Value<'_>)>

Access to the underlying Vec.

§Note

Since KeyStrType can be changed via a feature flag avoid using as_vec and use other methods instead. This could be a problem with feature unification, when one crate uses it as &str and another uses it as Cow, both will get Cow<str?

source

pub fn into_vec(self) -> Vec<(Cow<'ctx, str>, Value<'ctx>)>

Access to the underlying Vec. Keys are normalized to Cow.

source

pub fn get(&self, key: &str) -> Option<&Value<'_>>

Returns a reference to the value corresponding to the key.

§Performance

As this is backed by a Vec, this searches linearly through the Vec as may be much more expensive than a Hashmap for larger Objects.

source

pub fn get_mut(&mut self, key: &str) -> Option<&mut Value<'ctx>>

Returns a mutable reference to the value corresponding to the key, if it exists.

§Performance

As this is backed by a Vec, this searches linearly through the Vec as may be much more expensive than a Hashmap for larger Objects.

source

pub fn get_key_value(&self, key: &str) -> Option<(&str, &Value<'_>)>

Returns the key-value pair corresponding to the supplied key.

§Performance

As this is backed by a Vec, this searches linearly through the Vec as may be much more expensive than a Hashmap for larger Objects.

source

pub fn iter(&self) -> impl Iterator<Item = (&str, &Value<'_>)>

An iterator visiting all key-value pairs

source

pub fn len(&self) -> usize

Returns the number of elements in the object

source

pub fn is_empty(&self) -> bool

Returns true if the object contains no elements

source

pub fn keys(&self) -> impl Iterator<Item = &str>

An iterator visiting all keys

source

pub fn values(&self) -> impl Iterator<Item = &Value<'_>>

An iterator visiting all values

source

pub fn contains_key(&self, key: &str) -> bool

Returns true if the object contains a value for the specified key.

§Performance

As this is backed by a Vec, this searches linearly through the Vec as may be much more expensive than a Hashmap for larger Objects.

source

pub fn insert( &mut self, key: &'ctx str, value: Value<'ctx> ) -> Option<Value<'ctx>>

Inserts a key-value pair into the object. If the object did not have this key present, None is returned. If the object did have this key present, the value is updated, and the old value is returned.

§Performance

This operation is linear in the size of the Vec because it potentially requires iterating through all elements to find a matching key.

source

pub fn insert_or_get_mut( &mut self, key: &'ctx str, value: Value<'ctx> ) -> &mut Value<'ctx>

Inserts a key-value pair into the object if the key does not yet exist, otherwise returns a mutable reference to the existing value.

§Performance

This operation might be linear in the size of the Vec because it requires iterating through all elements to find a matching key, and might add to the end if not found.

source

pub fn insert_unchecked_and_get_mut( &mut self, key: &'ctx str, value: Value<'ctx> ) -> &mut Value<'ctx>

Inserts a key-value pair into the object and returns the mutable reference of the inserted value.

§Note

The key must not exist in the object. If the key already exists, the object will contain multiple keys afterwards.

§Performance

This operation is linear in the size of the Vec because it potentially requires iterating through all elements to find a matching key.

Trait Implementations§

source§

impl<'ctx> Clone for ObjectAsVec<'ctx>

source§

fn clone(&self) -> ObjectAsVec<'ctx>

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<'ctx> Debug for ObjectAsVec<'ctx>

source§

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

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

impl<'ctx> Default for ObjectAsVec<'ctx>

source§

fn default() -> ObjectAsVec<'ctx>

Returns the “default value” for a type. Read more
source§

impl<'ctx> From<&ObjectAsVec<'ctx>> for Map<String, Value>

source§

fn from(val: &ObjectAsVec<'ctx>) -> Self

Converts to this type from the input type.
source§

impl<'ctx> From<ObjectAsVec<'ctx>> for Map<String, Value>

source§

fn from(val: ObjectAsVec<'ctx>) -> Self

Converts to this type from the input type.
source§

impl<'ctx> From<Vec<(&'ctx str, Value<'ctx>)>> for ObjectAsVec<'ctx>

source§

fn from(vec: Vec<(&'ctx str, Value<'ctx>)>) -> Self

Converts to this type from the input type.
source§

impl<'ctx> Hash for ObjectAsVec<'ctx>

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<'ctx> PartialEq for ObjectAsVec<'ctx>

source§

fn eq(&self, other: &ObjectAsVec<'ctx>) -> 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<'ctx> Serialize for Map<'ctx>

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<'ctx> Eq for ObjectAsVec<'ctx>

source§

impl<'ctx> StructuralPartialEq for ObjectAsVec<'ctx>

Auto Trait Implementations§

§

impl<'ctx> Freeze for ObjectAsVec<'ctx>

§

impl<'ctx> RefUnwindSafe for ObjectAsVec<'ctx>

§

impl<'ctx> Send for ObjectAsVec<'ctx>

§

impl<'ctx> Sync for ObjectAsVec<'ctx>

§

impl<'ctx> Unpin for ObjectAsVec<'ctx>

§

impl<'ctx> UnwindSafe for ObjectAsVec<'ctx>

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.