From aeac82a18878e9c2a8e1ddcc928fc133dced086b Mon Sep 17 00:00:00 2001 From: Robert Spencer Date: Wed, 12 Jul 2023 18:22:27 +0100 Subject: [PATCH] feat: Add API for testing if a value is null --- src/lib.rs | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/lib.rs b/src/lib.rs index bea842c..df3e8d3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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`. @@ -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("