- 
                Notifications
    
You must be signed in to change notification settings  - Fork 615
 
Open
Description
At application level, it is often useful to check if a value is empty, in the broader sense, i.e. if it is null, "",  [], or {}.
Is there any particular reason why there is no general is_empty method for Value?
Is this simple addition worth considering?
For example:
/// A trait of [serde_json::Value] to check if it is empty.
pub trait IsEmpty {
    /// Check if the value is empty.
    fn is_empty(&self) -> bool;
}
impl IsEmpty for Value {
    fn is_empty(&self) -> bool {
        match self {
            Value::Null => true, // This may be a stretch, but in practice I often don't want to deal with a null.
            Value::String(s) if s.is_empty() => true, // These just call the underlying ones...
            Value::Array(a) if a.is_empty() => true,
            Value::Object(o) if o.is_empty() => true,
            _ => false,
        }
    }
}
Thanks
rxdiscovery
Metadata
Metadata
Assignees
Labels
No labels