diff --git a/src/json.rs b/src/json.rs index 44bf6a2..6026273 100644 --- a/src/json.rs +++ b/src/json.rs @@ -1023,7 +1023,7 @@ impl Json { self.as_object().is_some() } - /// If the Json value is an Object, returns the associated BTreeMap. + /// If the Json value is an Object, returns a reference to the associated BTreeMap. /// Returns None otherwise. pub fn as_object<'a>(&'a self) -> Option<&'a Object> { match self { @@ -1032,7 +1032,7 @@ impl Json { } } - /// If the Json value is an Object, returns the associated mutable BTreeMap. + /// If the Json value is an Object, returns a mutable reference to the associated BTreeMap. /// Returns None otherwise. pub fn as_object_mut<'a>(&'a mut self) -> Option<&'a mut Object> { match self { @@ -1041,12 +1041,21 @@ impl Json { } } + /// If the Json value is an Object, returns the associated BTreeMap. + /// Returns None otherwise. + pub fn into_object(self) -> Option { + match self { + Json::Object(map) => Some(map), + _ => None + } + } + /// Returns true if the Json value is an Array. Returns false otherwise. pub fn is_array<'a>(&'a self) -> bool { self.as_array().is_some() } - /// If the Json value is an Array, returns the associated vector. + /// If the Json value is an Array, returns a reference to the associated vector. /// Returns None otherwise. pub fn as_array<'a>(&'a self) -> Option<&'a Array> { match self { @@ -1055,7 +1064,7 @@ impl Json { } } - /// If the Json value is an Array, returns the associated mutable vector. + /// If the Json value is an Array, returns a mutable reference to the associated vector. /// Returns None otherwise. pub fn as_array_mut<'a>(&'a mut self) -> Option<&'a mut Array> { match self { @@ -1064,6 +1073,15 @@ impl Json { } } + /// If the Json value is an Array, returns the associated vector. + /// Returns None otherwise. + pub fn into_array(self) -> Option { + match self { + Json::Array(array) => Some(array), + _ => None + } + } + /// Returns true if the Json value is a String. Returns false otherwise. pub fn is_string<'a>(&'a self) -> bool { self.as_string().is_some() @@ -3426,7 +3444,7 @@ mod tests { _ => {} // it parsed and we are good to go } } - + #[test] fn test_negative_zero() { Json::from_str("{\"test\":-0}").unwrap();