sonic_rs

Trait JsonContainerTrait

Source
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§

Required Methods§

Source

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}));
Source

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]));

Implementations on Foreign Types§

Source§

impl<V: JsonContainerTrait> JsonContainerTrait for Option<V>

Source§

impl<V: JsonContainerTrait> JsonContainerTrait for &V

Source§

impl<V: JsonContainerTrait, E> JsonContainerTrait for Result<V, E>

Implementors§