@@ -2,7 +2,7 @@ use std::collections::BTreeMap;
2
2
use std:: ops:: { Index , IndexMut , Deref } ;
3
3
use iterators:: { Members , MembersMut , Entries , EntriesMut } ;
4
4
use { 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 } ;
6
6
7
7
macro_rules! f64_to_unsinged {
8
8
( $unsigned: ident, $value: expr) => {
@@ -175,6 +175,29 @@ impl JsonValue {
175
175
}
176
176
}
177
177
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
+
178
201
/// Works on `JsonValue::Array` - pushes a new value to the array.
179
202
#[ must_use]
180
203
pub fn push < T > ( & mut self , value : T ) -> JsonResult < ( ) >
@@ -290,6 +313,8 @@ impl JsonValue {
290
313
291
314
/// Implements indexing by `usize` to easily access array members:
292
315
///
316
+ /// ## Example
317
+ ///
293
318
/// ```
294
319
/// # use json::JsonValue;
295
320
/// let mut array = JsonValue::new_array();
@@ -311,6 +336,8 @@ impl Index<usize> for JsonValue {
311
336
312
337
/// Implements mutable indexing by `usize` to easily modify array members:
313
338
///
339
+ /// ## Example
340
+ ///
314
341
/// ```
315
342
/// # #[macro_use]
316
343
/// # extern crate json;
@@ -347,6 +374,8 @@ impl IndexMut<usize> for JsonValue {
347
374
348
375
/// Implements indexing by `&str` to easily access object members:
349
376
///
377
+ /// ## Example
378
+ ///
350
379
/// ```
351
380
/// # #[macro_use]
352
381
/// # extern crate json;
@@ -391,6 +420,8 @@ impl<'a> Index<&'a String> for JsonValue {
391
420
392
421
/// Implements mutable indexing by `&str` to easily modify object members:
393
422
///
423
+ /// ## Example
424
+ ///
394
425
/// ```
395
426
/// # #[macro_use]
396
427
/// # extern crate json;
0 commit comments