Skip to content

Commit

Permalink
Merge pull request #733 from matklad/from-iter-of-pairs
Browse files Browse the repository at this point in the history
Allow collecting an iterator of pairs into JSON object
  • Loading branch information
dtolnay committed Dec 2, 2020
2 parents efc9104 + 0c4b4df commit b9598ce
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/value/from.rs
Expand Up @@ -215,6 +215,22 @@ impl<T: Into<Value>> FromIterator<T> for Value {
}
}

impl<K: Into<String>, V: Into<Value>> FromIterator<(K, V)> for Value {
/// Convert an iteratable type to a `Value`
///
/// # Examples
///
/// ```
/// use serde_json::Value;
///
/// let v: Vec<_> = vec![("lorem", 40), ("ipsum", 2)];
/// let x: Value = v.into_iter().collect();
/// ```
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
Value::Object(iter.into_iter().map(|(k, v)| (k.into(), v.into())).collect())
}
}

impl From<()> for Value {
/// Convert `()` to `Value`
///
Expand Down

0 comments on commit b9598ce

Please sign in to comment.