Skip to content
This repository was archived by the owner on Dec 1, 2023. It is now read-only.
Merged
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
28 changes: 23 additions & 5 deletions src/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -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<Object> {
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 {
Expand All @@ -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 {
Expand All @@ -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<Array> {
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()
Expand Down Expand Up @@ -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();
Expand Down