@@ -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,33 @@ 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+ let mut placeholder = JsonValue :: Null ;
199+
200+ mem:: swap ( self , & mut placeholder) ;
201+
202+ placeholder
203+ }
204+
178205 /// Works on `JsonValue::Array` - pushes a new value to the array.
179206 #[ must_use]
180207 pub fn push < T > ( & mut self , value : T ) -> JsonResult < ( ) >
@@ -290,6 +317,8 @@ impl JsonValue {
290317
291318/// Implements indexing by `usize` to easily access array members:
292319///
320+ /// ## Example
321+ ///
293322/// ```
294323/// # use json::JsonValue;
295324/// let mut array = JsonValue::new_array();
@@ -311,6 +340,8 @@ impl Index<usize> for JsonValue {
311340
312341/// Implements mutable indexing by `usize` to easily modify array members:
313342///
343+ /// ## Example
344+ ///
314345/// ```
315346/// # #[macro_use]
316347/// # extern crate json;
@@ -347,6 +378,8 @@ impl IndexMut<usize> for JsonValue {
347378
348379/// Implements indexing by `&str` to easily access object members:
349380///
381+ /// ## Example
382+ ///
350383/// ```
351384/// # #[macro_use]
352385/// # extern crate json;
@@ -391,6 +424,8 @@ impl<'a> Index<&'a String> for JsonValue {
391424
392425/// Implements mutable indexing by `&str` to easily modify object members:
393426///
427+ /// ## Example
428+ ///
394429/// ```
395430/// # #[macro_use]
396431/// # extern crate json;
0 commit comments