Function sonic_rs::get_from_slice
source · pub fn get_from_slice<Path: IntoIterator>(
json: &[u8],
path: Path
) -> Result<LazyValue<'_>>where
Path::Item: Index,Expand description
Gets a field from a path. And return it as a Result<LazyValue>.
If not found, return an error. If the path is empty, return the whole JSON as a LazyValue.
The Item of the path should implement the [Index][crate::index::Index] trait.
§Examples
use sonic_rs::get_from_slice;
let lv = get_from_slice(br#"{"a": 1}"#, &["a"]).unwrap();
assert_eq!(lv.as_raw_str(), "1");
/// not found the field "a"
let lv = get_from_slice(br#"{"a": 1}"#, &["b"]);
assert!(lv.is_err());