Skip to content

Commit

Permalink
added consuming variants of the 'as_...' methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lasantosr committed Nov 6, 2024
1 parent 7cce517 commit 91c669d
Showing 1 changed file with 70 additions and 0 deletions.
70 changes: 70 additions & 0 deletions src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,24 @@ impl Value {
}
}

/// If the `Value` is an Object, returns the associated Map consuming it.
/// Returns None otherwise.
///
/// ```
/// # use serde_json::json;
/// #
/// let v = json!({ "a": { "nested": true }, "b": ["an", "array"] });
///
/// // The length of `{ "a": ..., "b": ... }` is 2 entries.
/// assert_eq!(v.into_object().unwrap().len(), 2);
/// ```
pub fn into_object(self) -> Option<Map<String, Value>> {
match self {
Value::Object(map) => Some(map),
_ => None,
}
}

/// Returns true if the `Value` is an Array. Returns false otherwise.
///
/// For any Value on which `is_array` returns true, `as_array` and
Expand Down Expand Up @@ -447,6 +465,24 @@ impl Value {
}
}

/// If the `Value` is an Array, returns the associated vector consuming it.
/// Returns None otherwise.
///
/// ```
/// # use serde_json::json;
/// #
/// let v = json!(["an", "array"]);
///
/// // The length of `["an", "array"]` is 2 elements.
/// assert_eq!(v.into_array().unwrap().len(), 2);
/// ```
pub fn into_array(self) -> Option<Vec<Value>> {
match self {
Value::Array(array) => Some(array),
_ => None,
}
}

/// Returns true if the `Value` is a String. Returns false otherwise.
///
/// For any Value on which `is_string` returns true, `as_str` is guaranteed
Expand Down Expand Up @@ -496,6 +532,23 @@ impl Value {
}
}

/// If the `Value` is a String, returns the associated String consuming it.
/// Returns None otherwise.
///
/// ```
/// # use serde_json::json;
/// #
/// let v = json!("some string");
///
/// assert_eq!(v.into_string().unwrap(), "some string");
/// ```
pub fn into_string(self) -> Option<String> {
match self {
Value::String(s) => Some(s),
_ => None,
}
}

/// Returns true if the `Value` is a Number. Returns false otherwise.
///
/// ```
Expand Down Expand Up @@ -537,6 +590,23 @@ impl Value {
}
}

/// If the `Value` is a Number, returns the associated [`Number`] consuming it.
/// Returns None otherwise.
///
/// ```
/// # use serde_json::{json, Number};
/// #
/// let v = json!(1);
///
/// assert_eq!(v.into_number(), Some(Number::from(1u64)));
/// ```
pub fn into_number(self) -> Option<Number> {
match self {
Value::Number(number) => Some(number),
_ => None,
}
}

/// Returns true if the `Value` is an integer between `i64::MIN` and
/// `i64::MAX`.
///
Expand Down

0 comments on commit 91c669d

Please sign in to comment.