serde_json::map

Struct Map

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

Represents a JSON key/value type.

Implementations§

Source§

impl Map<String, Value>

Source

pub fn new() -> Self

Makes a new empty Map.

Source

pub fn with_capacity(capacity: usize) -> Self

Makes a new empty Map with the given initial capacity.

Source

pub fn clear(&mut self)

Clears the map, removing all values.

Source

pub 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.

Source

pub 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.

Source

pub 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.

Source

pub 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.

Source

pub 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.

Source

pub 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.

Source

pub 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.

Source

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

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

Source

pub 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.

Source

pub fn len(&self) -> usize

Returns the number of elements in the map.

Source

pub fn is_empty(&self) -> bool

Returns true if the map contains no elements.

Source

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

Gets an iterator over the entries of the map.

Source

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

Gets a mutable iterator over the entries of the map.

Source

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

Gets an iterator over the keys of the map.

Source

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

Gets an iterator over the values of the map.

Source

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

Gets an iterator over mutable values of the map.

Source

pub 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.

Trait Implementations§

Source§

impl Clone for Map<String, Value>

Source§

fn clone(&self) -> Self

Returns a copy of the value. Read more
Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Map<String, Value>

Source§

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

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

impl Default for Map<String, Value>

Source§

fn default() -> Self

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

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

Source§

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

Deserialize this value from the given Serde deserializer. Read more
Source§

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

Source§

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

Extends a collection with the contents of an iterator. Read more
Source§

fn extend_one(&mut self, item: A)

🔬This is a nightly-only experimental API. (extend_one)
Extends a collection with exactly one element.
Source§

fn extend_reserve(&mut self, additional: usize)

🔬This is a nightly-only experimental API. (extend_one)
Reserves capacity in a collection for the given number of additional elements. Read more
Source§

impl From<Map<String, Value>> for Value

Source§

fn from(f: Map<String, Value>) -> Self

Convert map (with string keys) to Value::Object.

§Examples
use serde_json::{Map, Value};

let mut m = Map::new();
m.insert("Lorem".to_string(), "ipsum".into());
let x: Value = m.into();
Source§

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

Source§

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

Creates a value from an iterator. Read more
Source§

impl Hash for Map<String, Value>

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<'a, Q> Index<&'a Q> for Map<String, Value>
where String: Borrow<Q>, Q: ?Sized + Ord + Eq + Hash,

Access an element of this map. Panics if the given key is not present in the map.

match val {
    Value::String(s) => Some(s.as_str()),
    Value::Array(arr) => arr[0].as_str(),
    Value::Object(map) => map["type"].as_str(),
    _ => None,
}
Source§

type Output = Value

The returned type after indexing.
Source§

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

Performs the indexing (container[index]) operation. Read more
Source§

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

Mutably access an element of this map. Panics if the given key is not present in the map.

map["key"] = json!("value");
Source§

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

Performs the mutable indexing (container[index]) operation. Read more
Source§

impl<'a> IntoIterator for &'a Map<String, Value>

Source§

type Item = (&'a String, &'a Value)

The type of the elements being iterated over.
Source§

type IntoIter = Iter<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl<'a> IntoIterator for &'a mut Map<String, Value>

Source§

type Item = (&'a String, &'a mut Value)

The type of the elements being iterated over.
Source§

type IntoIter = IterMut<'a>

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl IntoIterator for Map<String, Value>

Source§

type Item = (String, Value)

The type of the elements being iterated over.
Source§

type IntoIter = IntoIter

Which kind of iterator are we turning this into?
Source§

fn into_iter(self) -> Self::IntoIter

Creates an iterator from a value. Read more
Source§

impl PartialEq for Map<String, Value>

Source§

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

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

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

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Map<String, Value>

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 Map<String, Value>

Auto Trait Implementations§

§

impl<K, V> Freeze for Map<K, V>

§

impl<K, V> RefUnwindSafe for Map<K, V>

§

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

§

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

§

impl<K, V> Unpin for Map<K, V>

§

impl<K, V> UnwindSafe for Map<K, V>

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut T)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. 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,

Source§

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>,

Source§

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>,

Source§

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.
Source§

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