Skip to content

Commit

Permalink
Allow collecting an iterator of pairs into JSON object
Browse files Browse the repository at this point in the history
  • Loading branch information
matklad committed Dec 2, 2020
1 parent efc9104 commit 0c4b4df
Showing 1 changed file with 16 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/value/from.rs
Original file line number Diff line number Diff line change
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 0c4b4df

Please sign in to comment.