sonic_rs::lazyvalue

Function to_object_iter

Source
pub fn to_object_iter<'de, I: JsonInput<'de>>(json: I) -> ObjectJsonIter<'de> 
Expand description

Traverse the JSON object text through a lazy iterator. The JSON parsing will doing when iterating.

The item of the iterator is a key-value pair: (FastStr, Result<LazyValue>).

§Errors

If the JSON is empty, not a object or parse error, the result will be Err and the next() will return None.

§Examples

use faststr::FastStr;
use sonic_rs::JsonValueTrait;

let json = FastStr::from(r#"{"a": null, "b":[1, 2, 3]}"#);
for ret in to_object_iter(&json) {
    assert!(ret.is_ok());
    let (k, v) = ret.unwrap();
    if k == "a" {
        assert!(v.is_null());
    } else if k == "b" {
        assert_eq!(v.as_raw_str(), "[1, 2, 3]");
    }
}

// the JSON is invalid, will report error when encountering the error
for (i, ret) in to_object_iter(r#"{"a": null, "b":[1, 2, 3"#).enumerate() {
    if i == 0 {
        assert!(ret.is_ok());
    }
    if i == 1 {
        assert!(ret.is_err());
    }
}