Skip to content

Commit b9598ce

Browse files
authored
Merge pull request #733 from matklad/from-iter-of-pairs
Allow collecting an iterator of pairs into JSON object
2 parents efc9104 + 0c4b4df commit b9598ce

1 file changed

Lines changed: 16 additions & 0 deletions

File tree

‎src/value/from.rs‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,22 @@ impl<T: Into<Value>> FromIterator<T> for Value {
215215
}
216216
}
217217

218+
impl<K: Into<String>, V: Into<Value>> FromIterator<(K, V)> for Value {
219+
/// Convert an iteratable type to a `Value`
220+
///
221+
/// # Examples
222+
///
223+
/// ```
224+
/// use serde_json::Value;
225+
///
226+
/// let v: Vec<_> = vec![("lorem", 40), ("ipsum", 2)];
227+
/// let x: Value = v.into_iter().collect();
228+
/// ```
229+
fn from_iter<I: IntoIterator<Item = (K, V)>>(iter: I) -> Self {
230+
Value::Object(iter.into_iter().map(|(k, v)| (k.into(), v.into())).collect())
231+
}
232+
}
233+
218234
impl From<()> for Value {
219235
/// Convert `()` to `Value`
220236
///

0 commit comments

Comments
 (0)