Skip to content

Commit

Permalink
Merge pull request rust-num#218 from serde-rs/indexmap
Browse files Browse the repository at this point in the history
Index into Map
  • Loading branch information
oli-obk authored Jan 28, 2017
2 parents 6b70ae0 + b1865f2 commit 6caf995
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions json/src/map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::fmt::{self, Debug};
use value::Value;
use std::hash::Hash;
use std::borrow::Borrow;
use std::ops;

#[cfg(not(feature = "preserve_order"))]
use std::collections::{BTreeMap, btree_map};
Expand Down Expand Up @@ -165,6 +166,32 @@ impl PartialEq for Map<String, Value> {
}
}

/// Access an element of this map. Panics if the given key is not present in the
/// map.
///
/// ```rust
/// # use serde_json::Value;
/// # let val = &Value::String("".to_owned());
/// # let _ =
/// match *val {
/// Value::String(ref s) => Some(s.as_str()),
/// Value::Array(ref arr) => arr[0].as_str(),
/// Value::Object(ref map) => map["type"].as_str(),
/// _ => None,
/// }
/// # ;
/// ```
impl<'a, Q: ?Sized> ops::Index<&'a Q> for Map<String, Value>
where String: Borrow<Q>,
Q: Ord + Eq + Hash
{
type Output = Value;

fn index(&self, index: &Q) -> &Value {
self.0.index(index)
}
}

impl Debug for Map<String, Value> {
#[inline]
fn fmt(&self, formatter: &mut fmt::Formatter) -> Result<(), fmt::Error> {
Expand Down

0 comments on commit 6caf995

Please sign in to comment.