Skip to content

Commit

Permalink
feat: Add API for testing if a value is null
Browse files Browse the repository at this point in the history
  • Loading branch information
rspencer01 committed Jul 12, 2023
1 parent 0a9c853 commit aeac82a
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions src/lib.rs
Expand Up @@ -248,6 +248,23 @@ impl<'a> JSONValue<'a> {
))
}

/// Checks if the [`JSONValue`] is null
///
/// If the value is malformed, returns false.
///
/// ### Example
/// ```
/// # use microjson::{JSONValue, JSONParsingError};
/// let value = JSONValue::load("null");
/// assert_eq!(value.is_null(), true);
///
/// let value = JSONValue::load("1293");
/// assert_eq!(value.is_null(), false);
/// ```
pub fn is_null(&self) -> bool {
self.contents.trim_end() == "null"
}

/// Reads the [`JSONValue`] as a boolean
///
/// If the type is not a [`JSONValueType::Boolean`], returns an `Err`.
Expand Down Expand Up @@ -544,6 +561,13 @@ mod test {
use super::*;
extern crate std;

#[test]
fn is_null() {
assert_eq!(JSONValue::load("null").is_null(), true);
assert_eq!(JSONValue::load("1234").is_null(), false);
assert_eq!(JSONValue::load("<!or").is_null(), false);
}

#[test]
fn boolean() {
assert_eq!(JSONValue::load("true").read_boolean(), Ok(true));
Expand Down

0 comments on commit aeac82a

Please sign in to comment.