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>
impl<'ctx> ObjectAsVec<'ctx>
sourcepub fn as_vec(&self) -> &Vec<(KeyStrType<'_>, Value<'_>)>
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
sourcepub fn into_vec(self) -> Vec<(Cow<'ctx, str>, Value<'ctx>)>
pub fn into_vec(self) -> Vec<(Cow<'ctx, str>, Value<'ctx>)>
Access to the underlying Vec. Keys are normalized to Cow.
sourcepub fn get(&self, key: &str) -> Option<&Value<'_>>
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.
sourcepub fn get_mut(&mut self, key: &str) -> Option<&mut Value<'ctx>>
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.
sourcepub fn get_key_value(&self, key: &str) -> Option<(&str, &Value<'_>)>
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.
sourcepub fn iter(&self) -> impl Iterator<Item = (&str, &Value<'_>)>
pub fn iter(&self) -> impl Iterator<Item = (&str, &Value<'_>)>
An iterator visiting all key-value pairs
sourcepub fn contains_key(&self, key: &str) -> bool
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.
sourcepub fn insert(
&mut self,
key: &'ctx str,
value: Value<'ctx>
) -> Option<Value<'ctx>>
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.
sourcepub fn insert_or_get_mut(
&mut self,
key: &'ctx str,
value: Value<'ctx>
) -> &mut Value<'ctx>
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.
sourcepub fn insert_unchecked_and_get_mut(
&mut self,
key: &'ctx str,
value: Value<'ctx>
) -> &mut Value<'ctx>
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>
impl<'ctx> Clone for ObjectAsVec<'ctx>
source§fn clone(&self) -> ObjectAsVec<'ctx>
fn clone(&self) -> ObjectAsVec<'ctx>
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl<'ctx> Debug for ObjectAsVec<'ctx>
impl<'ctx> Debug for ObjectAsVec<'ctx>
source§impl<'ctx> Default for ObjectAsVec<'ctx>
impl<'ctx> Default for ObjectAsVec<'ctx>
source§fn default() -> ObjectAsVec<'ctx>
fn default() -> ObjectAsVec<'ctx>
source§impl<'ctx> From<&ObjectAsVec<'ctx>> for Map<String, Value>
impl<'ctx> From<&ObjectAsVec<'ctx>> for Map<String, Value>
source§fn from(val: &ObjectAsVec<'ctx>) -> Self
fn from(val: &ObjectAsVec<'ctx>) -> Self
source§impl<'ctx> From<ObjectAsVec<'ctx>> for Map<String, Value>
impl<'ctx> From<ObjectAsVec<'ctx>> for Map<String, Value>
source§fn from(val: ObjectAsVec<'ctx>) -> Self
fn from(val: ObjectAsVec<'ctx>) -> Self
source§impl<'ctx> Hash for ObjectAsVec<'ctx>
impl<'ctx> Hash for ObjectAsVec<'ctx>
source§impl<'ctx> PartialEq for ObjectAsVec<'ctx>
impl<'ctx> PartialEq for ObjectAsVec<'ctx>
source§fn eq(&self, other: &ObjectAsVec<'ctx>) -> bool
fn eq(&self, other: &ObjectAsVec<'ctx>) -> bool
self
and other
values to be equal, and is used
by ==
.