Skip to content

Commit 1f135b7

Browse files
authored
Merge pull request #162 from mominul/master
Add insert() function in JsonValue
2 parents 677a590 + 5064b8d commit 1f135b7

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/value/mod.rs

+15
Original file line numberDiff line numberDiff line change
@@ -482,6 +482,21 @@ impl JsonValue {
482482
}
483483
}
484484

485+
/// Works on `JsonValue::Object` - inserts a new entry, or override an existing
486+
/// one into the object. Note that `key` has to be a `&str` slice and not an owned
487+
/// `String`. The internals of `Object` will handle the heap allocation of the key
488+
/// if needed for better performance.
489+
pub fn insert<T>(&mut self, key: &str, value: T) -> Result<()>
490+
where T: Into<JsonValue> {
491+
match *self {
492+
JsonValue::Object(ref mut object) => {
493+
object.insert(key, value.into());
494+
Ok(())
495+
},
496+
_ => Err(Error::wrong_type("Object"))
497+
}
498+
}
499+
485500
/// Works on `JsonValue::Object` - remove a key and return the value it held.
486501
/// If the key was not present, the method is called on anything but an
487502
/// object, it will return a null.

0 commit comments

Comments
 (0)