Skip to content

Commit 91c669d

Browse files
committed
added consuming variants of the 'as_...' methods
1 parent 7cce517 commit 91c669d

File tree

1 file changed

+70
-0
lines changed

1 file changed

+70
-0
lines changed

src/value/mod.rs

+70
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,24 @@ impl Value {
388388
}
389389
}
390390

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+
391409
/// Returns true if the `Value` is an Array. Returns false otherwise.
392410
///
393411
/// For any Value on which `is_array` returns true, `as_array` and
@@ -447,6 +465,24 @@ impl Value {
447465
}
448466
}
449467

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+
450486
/// Returns true if the `Value` is a String. Returns false otherwise.
451487
///
452488
/// For any Value on which `is_string` returns true, `as_str` is guaranteed
@@ -496,6 +532,23 @@ impl Value {
496532
}
497533
}
498534

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+
499552
/// Returns true if the `Value` is a Number. Returns false otherwise.
500553
///
501554
/// ```
@@ -537,6 +590,23 @@ impl Value {
537590
}
538591
}
539592

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+
540610
/// Returns true if the `Value` is an integer between `i64::MIN` and
541611
/// `i64::MAX`.
542612
///

0 commit comments

Comments
 (0)