@@ -2,7 +2,7 @@ use std::collections::BTreeMap;
22use std:: ops:: { Index , IndexMut , Deref } ;
33use iterators:: { Members , MembersMut , Entries , EntriesMut } ;
44use { JsonResult , JsonError } ;
5- use std:: { usize, u8, u16, u32, u64, isize, i8, i16, i32, i64, f32 } ;
5+ use std:: { mem , usize, u8, u16, u32, u64, isize, i8, i16, i32, i64, f32 } ;
66
77macro_rules! f64_to_unsinged {
88 ( $unsigned: ident, $value: expr) => {
@@ -175,6 +175,29 @@ impl JsonValue {
175175 }
176176 }
177177
178+ /// Take over the ownership of the value, leaving `Null` in it's place.
179+ ///
180+ /// ## Example
181+ ///
182+ /// ```
183+ /// # #[macro_use] extern crate json;
184+ /// # fn main() {
185+ /// let mut data = array!["Foo", 42];
186+ ///
187+ /// let first = data[0].take();
188+ /// let second = data[1].take();
189+ ///
190+ /// assert!(first == "Foo");
191+ /// assert!(second == 42);
192+ ///
193+ /// assert!(data[0].is_null());
194+ /// assert!(data[1].is_null());
195+ /// # }
196+ /// ```
197+ pub fn take ( & mut self ) -> JsonValue {
198+ mem:: replace ( self , JsonValue :: Null )
199+ }
200+
178201 /// Works on `JsonValue::Array` - pushes a new value to the array.
179202 #[ must_use]
180203 pub fn push < T > ( & mut self , value : T ) -> JsonResult < ( ) >
@@ -290,6 +313,8 @@ impl JsonValue {
290313
291314/// Implements indexing by `usize` to easily access array members:
292315///
316+ /// ## Example
317+ ///
293318/// ```
294319/// # use json::JsonValue;
295320/// let mut array = JsonValue::new_array();
@@ -311,6 +336,8 @@ impl Index<usize> for JsonValue {
311336
312337/// Implements mutable indexing by `usize` to easily modify array members:
313338///
339+ /// ## Example
340+ ///
314341/// ```
315342/// # #[macro_use]
316343/// # extern crate json;
@@ -347,6 +374,8 @@ impl IndexMut<usize> for JsonValue {
347374
348375/// Implements indexing by `&str` to easily access object members:
349376///
377+ /// ## Example
378+ ///
350379/// ```
351380/// # #[macro_use]
352381/// # extern crate json;
@@ -391,6 +420,8 @@ impl<'a> Index<&'a String> for JsonValue {
391420
392421/// Implements mutable indexing by `&str` to easily modify object members:
393422///
423+ /// ## Example
424+ ///
394425/// ```
395426/// # #[macro_use]
396427/// # extern crate json;
0 commit comments