Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added consuming variants of the 'as_...' methods #1210

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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