Skip to content
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
34 changes: 31 additions & 3 deletions src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -714,8 +714,8 @@ impl Value {

/// Returns true if the `Value` is a Null. Returns false otherwise.
///
/// For any Value on which `is_null` returns true, `as_null` is guaranteed
/// to return `Some(())`.
/// For any Value on which `is_null` returns true, `as_null` is guaranteed to
/// return `Some(())`.
///
/// ```
/// # use serde_json::json;
Expand All @@ -728,7 +728,35 @@ impl Value {
/// assert!(!v["b"].is_null());
/// ```
pub fn is_null(&self) -> bool {
self.as_null().is_some()
matches!(self, Value::Null)
}

/// Returns true if the `Value` is considered empty. A `Value` is considered
/// empty if it is `Null`, an empty string, an empty array, or an empty
/// object.
///
/// ```
/// # use serde_json::json;
/// #
/// assert!(json!(null).is_empty());
/// assert!(json!("").is_empty());
/// assert!(json!([]).is_empty());
/// assert!(json!({}).is_empty());
///
/// assert!(!json!("hello").is_empty());
/// assert!(!json!([1, 2, 3]).is_empty());
/// assert!(!json!({ "key": "value" }).is_empty());
/// assert!(!json!(true).is_empty());
/// assert!(!json!(42).is_empty());
/// ```
pub fn is_empty(&self) -> bool {
match self {
Value::Null => true,
Value::String(s) => s.is_empty(),
Value::Array(a) => a.is_empty(),
Value::Object(o) => o.is_empty(),
_ => false,
}
}

/// If the `Value` is a Null, returns (). Returns None otherwise.
Expand Down
Loading