pub trait JsonContainerTrait {
type ObjectType;
type ArrayType;
// Required methods
fn as_object(&self) -> Option<&Self::ObjectType>;
fn as_array(&self) -> Option<&Self::ArrayType>;
}
Expand description
A trait for all JSON object or array values. Used by Value
.
The Option<V: JsonContainerTrait>
and Result<V: JsonContainerTrait, E>
also implement this
trait. The Option::None
or Result::Err(_)
will be viewed as a null value.
Required Associated Types§
type ObjectType
type ArrayType
Required Methods§
Sourcefn as_object(&self) -> Option<&Self::ObjectType>
fn as_object(&self) -> Option<&Self::ObjectType>
Returns the object if self
is an object
.
§Examples
use sonic_rs::{json, object, JsonContainerTrait, JsonValueTrait, Value};
let value: Value = sonic_rs::from_str(r#"{"a": 1, "b": true}"#).unwrap();
assert!(value.is_object());
assert_eq!(value.as_object(), Some(&object! {"a": 1, "b": true}));
Sourcefn as_array(&self) -> Option<&Self::ArrayType>
fn as_array(&self) -> Option<&Self::ArrayType>
Returns the array if self
is an array
.
§Examples
use sonic_rs::{array, json, JsonContainerTrait, JsonValueTrait, Value};
let value: Value = sonic_rs::from_str(r#"[1, 2, 3]"#).unwrap();
assert!(value.is_array());
assert_eq!(value.as_array(), Some(&array![1, 2, 3]));