Struct Map

pub struct Map<K, V> { /* private fields */ }

Represents a JSON key/value type.

Implementations

impl Map<String, Value>

fn new() -> Self

Makes a new empty Map.

fn with_capacity(capacity: usize) -> Self

Makes a new empty Map with the given initial capacity.

fn clear(&mut self)

Clears the map, removing all values.

fn get<Q>(&self, key: &Q) -> Option<&Value>
where
    String: Borrow<Q>,
    Q: ?Sized + Ord + Eq + Hash,

Returns a reference to the value corresponding to the key.

The key may be any borrowed form of the map's key type, but the ordering on the borrowed form must match the ordering on the key type.

fn contains_key<Q>(&self, key: &Q) -> bool
where
    String: Borrow<Q>,
    Q: ?Sized + Ord + Eq + Hash,

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

The key may be any borrowed form of the map's key type, but the ordering on the borrowed form must match the ordering on the key type.

fn get_mut<Q>(&mut self, key: &Q) -> Option<&mut Value>
where
    String: Borrow<Q>,
    Q: ?Sized + Ord + Eq + Hash,

Returns a mutable reference to the value corresponding to the key.

The key may be any borrowed form of the map's key type, but the ordering on the borrowed form must match the ordering on the key type.

fn get_key_value<Q>(&self, key: &Q) -> Option<(&String, &Value)>
where
    String: Borrow<Q>,
    Q: ?Sized + Ord + Eq + Hash,

Returns the key-value pair matching the given key.

The key may be any borrowed form of the map's key type, but the ordering on the borrowed form must match the ordering on the key type.

fn insert(&mut self, k: String, v: Value) -> Option<Value>

Inserts a key-value pair into the map.

If the map did not have this key present, None is returned.

If the map did have this key present, the value is updated, and the old value is returned.

fn remove<Q>(&mut self, key: &Q) -> Option<Value>
where
    String: Borrow<Q>,
    Q: ?Sized + Ord + Eq + Hash,

Removes a key from the map, returning the value at the key if the key was previously in the map.

The key may be any borrowed form of the map's key type, but the ordering on the borrowed form must match the ordering on the key type.

If serde_json's "preserve_order" is enabled, .remove(key) is equivalent to [.swap_remove(key)][Self::swap_remove], replacing this entry's position with the last element. If you need to preserve the relative order of the keys in the map, use [.shift_remove(key)][Self::shift_remove] instead.

fn remove_entry<Q>(&mut self, key: &Q) -> Option<(String, Value)>
where
    String: Borrow<Q>,
    Q: ?Sized + Ord + Eq + Hash,

Removes a key from the map, returning the stored key and value if the key was previously in the map.

The key may be any borrowed form of the map's key type, but the ordering on the borrowed form must match the ordering on the key type.

If serde_json's "preserve_order" is enabled, .remove_entry(key) is equivalent to [.swap_remove_entry(key)][Self::swap_remove_entry], replacing this entry's position with the last element. If you need to preserve the relative order of the keys in the map, use [.shift_remove_entry(key)][Self::shift_remove_entry] instead.

fn append(&mut self, other: &mut Self)

Moves all elements from other into self, leaving other empty.

fn entry<S>(&mut self, key: S) -> Entry<'_>
where
    S: Into<String>,

Gets the given key's corresponding entry in the map for in-place manipulation.

fn len(&self) -> usize

Returns the number of elements in the map.

fn is_empty(&self) -> bool

Returns true if the map contains no elements.

fn iter(&self) -> Iter<'_>

Gets an iterator over the entries of the map.

fn iter_mut(&mut self) -> IterMut<'_>

Gets a mutable iterator over the entries of the map.

fn keys(&self) -> Keys<'_>

Gets an iterator over the keys of the map.

fn values(&self) -> Values<'_>

Gets an iterator over the values of the map.

fn values_mut(&mut self) -> ValuesMut<'_>

Gets an iterator over mutable values of the map.

fn into_values(self) -> IntoValues

Gets an iterator over the values of the map.

fn retain<F>(&mut self, f: F)
where
    F: FnMut(&String, &mut Value) -> bool,

Retains only the elements specified by the predicate.

In other words, remove all pairs (k, v) such that f(&k, &mut v) returns false.

fn sort_keys(&mut self)

Sorts this map's entries in-place using str's usual ordering.

If serde_json's "preserve_order" feature is not enabled, this method does no work because all JSON maps are always kept in a sorted state.

If serde_json's "preserve_order" feature is enabled, this method destroys the original source order or insertion order of this map in favor of an alphanumerical order that matches how a BTreeMap with the same contents would be ordered. This takes O(n log n + c) time where n is the length of the map and c is the capacity.

Other maps nested within the values of this map are not sorted. If you need the entire data structure to be sorted at all levels, you must also call map.values_mut().for_each([Value::sort_all_objects]).

Trait Implementations

impl Clone for Map<String, Value>

fn clone(&self) -> Self
fn clone_from(&mut self, source: &Self)

impl Debug for Map<String, Value>

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

impl Default for Map<String, Value>

fn default() -> Self

impl Eq for Map<String, Value>

impl Extend<(String, Value)> for Map<String, Value>

fn extend<T>(&mut self, iter: T)
where
    T: IntoIterator<Item = (String, Value)>,

impl FromIterator<(String, Value)> for Map<String, Value>

fn from_iter<T>(iter: T) -> Self
where
    T: IntoIterator<Item = (String, Value)>,

impl FromStr for Map<String, Value>

type Err = Error;
fn from_str(s: &str) -> Result<Self, Error>

impl Hash for Map<String, Value>

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

impl IntoIterator for Map<String, Value>

type Item = (String, Value);
type IntoIter = IntoIter;
fn into_iter(self) -> Self::IntoIter

impl PartialEq for Map<String, Value>

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

impl Serialize for Map<String, Value>

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

impl<'de> Deserialize<'de> for Map<String, Value>

fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
    D: Deserializer<'de>,

impl<'de> Deserializer<'de> for Map<String, Value>

type Error = Error;
fn deserialize_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
    V: Visitor<'de>,
fn deserialize_enum<V>(self, _name: &'static str, _variants: &'static [&'static str], visitor: V) -> Result<V::Value, Self::Error>
where
    V: Visitor<'de>,
fn deserialize_ignored_any<V>(self, visitor: V) -> Result<V::Value, Self::Error>
where
    V: Visitor<'de>,
fn deserialize_bool<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_i8<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_i16<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_i32<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_i64<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_i128<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_u8<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_u16<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_u32<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_u64<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_u128<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_f32<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_f64<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_char<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_str<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_string<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_bytes<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_byte_buf<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_option<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_unit<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_unit_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_newtype_struct<V>(self, name: &'static str, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_seq<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_tuple<V>(self, len: usize, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_tuple_struct<V>(self, name: &'static str, len: usize, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_map<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_struct<V>(self, name: &'static str, fields: &'static [&'static str], visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,
fn deserialize_identifier<V>(self, visitor: V) -> Result<V::Value, <Self as Deserializer<'de>>::Error>
where
    V: Visitor<'de>,

impl<'de> IntoDeserializer<'de, Error> for Map<String, Value>

type Deserializer = Map<String, Value>;
fn into_deserializer(self) -> Self::Deserializer

impl<Q> Index<&Q> for Map<String, Value> where String: Borrow<Q>, Q: ?Sized + Ord + Eq + Hash,

type Output = Value;
fn index(&self, index: &Q) -> &Value

impl<Q> IndexMut<&Q> for Map<String, Value> where String: Borrow<Q>, Q: ?Sized + Ord + Eq + Hash,

fn index_mut(&mut self, index: &Q) -> &mut Value

Auto Trait Implementations

impl<K, V> Freeze for Map<K, V> where BTreeMap<K, V>: Freeze,

impl<K, V> RefUnwindSafe for Map<K, V> where BTreeMap<K, V>: RefUnwindSafe,

impl<K, V> Send for Map<K, V> where BTreeMap<K, V>: Send,

impl<K, V> Sync for Map<K, V> where BTreeMap<K, V>: Sync,

impl<K, V> Unpin for Map<K, V> where BTreeMap<K, V>: Unpin,

impl<K, V> UnsafeUnpin for Map<K, V> where BTreeMap<K, V>: UnsafeUnpin,

impl<K, V> UnwindSafe for Map<K, V> where BTreeMap<K, V>: UnwindSafe,

Blanket Implementations

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

fn type_id(&self) -> TypeId

impl<T> Borrow<T> for Map<K, V> where T: ?Sized,

fn borrow(&self) -> &T

impl<T> BorrowMut<T> for Map<K, V> where T: ?Sized,

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

impl<T> CloneToUninit for Map<K, V> where T: Clone,

unsafe fn clone_to_uninit(&self, dest: *mut u8)

impl<T> DeserializeOwned for Map<K, V> where T: for<'de> Deserialize<'de>,

impl<T> From<T> for Map<K, V>

fn from(t: T) -> T

Returns the argument unchanged.

impl<T> ToOwned for Map<K, V> where T: Clone,

type Owned = T;
fn to_owned(&self) -> T
fn clone_into(&self, target: &mut T)

impl<T, U> Into<U> for Map<K, V> where U: From<T>,

fn into(self) -> U

Calls U::from(self).

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

impl<T, U> TryFrom<U> for Map<K, V> where U: Into<T>,

type Error = never;
fn try_from(value: U) -> Result<T, never>

impl<T, U> TryInto<U> for Map<K, V> where U: TryFrom<T>,

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