@@ -388,6 +388,24 @@ impl Value {
388
388
}
389
389
}
390
390
391
+ /// If the `Value` is an Object, returns the associated Map consuming it.
392
+ /// Returns None otherwise.
393
+ ///
394
+ /// ```
395
+ /// # use serde_json::json;
396
+ /// #
397
+ /// let v = json!({ "a": { "nested": true }, "b": ["an", "array"] });
398
+ ///
399
+ /// // The length of `{ "a": ..., "b": ... }` is 2 entries.
400
+ /// assert_eq!(v.into_object().unwrap().len(), 2);
401
+ /// ```
402
+ pub fn into_object ( self ) -> Option < Map < String , Value > > {
403
+ match self {
404
+ Value :: Object ( map) => Some ( map) ,
405
+ _ => None ,
406
+ }
407
+ }
408
+
391
409
/// Returns true if the `Value` is an Array. Returns false otherwise.
392
410
///
393
411
/// For any Value on which `is_array` returns true, `as_array` and
@@ -447,6 +465,24 @@ impl Value {
447
465
}
448
466
}
449
467
468
+ /// If the `Value` is an Array, returns the associated vector consuming it.
469
+ /// Returns None otherwise.
470
+ ///
471
+ /// ```
472
+ /// # use serde_json::json;
473
+ /// #
474
+ /// let v = json!(["an", "array"]);
475
+ ///
476
+ /// // The length of `["an", "array"]` is 2 elements.
477
+ /// assert_eq!(v.into_array().unwrap().len(), 2);
478
+ /// ```
479
+ pub fn into_array ( self ) -> Option < Vec < Value > > {
480
+ match self {
481
+ Value :: Array ( array) => Some ( array) ,
482
+ _ => None ,
483
+ }
484
+ }
485
+
450
486
/// Returns true if the `Value` is a String. Returns false otherwise.
451
487
///
452
488
/// For any Value on which `is_string` returns true, `as_str` is guaranteed
@@ -496,6 +532,23 @@ impl Value {
496
532
}
497
533
}
498
534
535
+ /// If the `Value` is a String, returns the associated String consuming it.
536
+ /// Returns None otherwise.
537
+ ///
538
+ /// ```
539
+ /// # use serde_json::json;
540
+ /// #
541
+ /// let v = json!("some string");
542
+ ///
543
+ /// assert_eq!(v.into_string().unwrap(), "some string");
544
+ /// ```
545
+ pub fn into_string ( self ) -> Option < String > {
546
+ match self {
547
+ Value :: String ( s) => Some ( s) ,
548
+ _ => None ,
549
+ }
550
+ }
551
+
499
552
/// Returns true if the `Value` is a Number. Returns false otherwise.
500
553
///
501
554
/// ```
@@ -537,6 +590,23 @@ impl Value {
537
590
}
538
591
}
539
592
593
+ /// If the `Value` is a Number, returns the associated [`Number`] consuming it.
594
+ /// Returns None otherwise.
595
+ ///
596
+ /// ```
597
+ /// # use serde_json::{json, Number};
598
+ /// #
599
+ /// let v = json!(1);
600
+ ///
601
+ /// assert_eq!(v.into_number(), Some(Number::from(1u64)));
602
+ /// ```
603
+ pub fn into_number ( self ) -> Option < Number > {
604
+ match self {
605
+ Value :: Number ( number) => Some ( number) ,
606
+ _ => None ,
607
+ }
608
+ }
609
+
540
610
/// Returns true if the `Value` is an integer between `i64::MIN` and
541
611
/// `i64::MAX`.
542
612
///
0 commit comments