sonic_rs::lazyvalue

Function get_from_bytes

Source
pub fn get_from_bytes<Path: IntoIterator>(
    json: &Bytes,
    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 trait.

ยงExamples

use bytes::Bytes;

let bs = Bytes::from(r#"{"a": 1}"#);
let lv = get_from_bytes(&bs, &["a"]).unwrap();

assert_eq!(lv.as_raw_str(), "1");

/// not found the field "a"
let lv = get_from_bytes(&bs, &["b"]);
assert!(lv.is_err());