#json-parser #json #iterator #deserialize #parser #parse-json #streaming-json

jsonit

A way to parse Json Items using iterators from streams

9 releases

new 0.2.10 May 6, 2024
0.2.9 May 6, 2024
0.2.7 Mar 15, 2024
0.2.5 Feb 22, 2024
0.1.1 Nov 4, 2023

#8 in #streaming-json

Download history 135/week @ 2024-02-12 226/week @ 2024-02-19 61/week @ 2024-02-26 148/week @ 2024-03-04 113/week @ 2024-03-11 22/week @ 2024-03-18 58/week @ 2024-04-01

58 downloads per month

Apache-2.0

19KB
515 lines

JsonIT (Json Iterator)

This crate was created in order to make the streaming of Json Objects inside array in a (std::Read or std::Iterator) as easy as possible. It should ressemble the ijson package from python.

Like the ijson package you have to specify a prefix in order for the library to find the array you want to parse.

Using with iterator

In order to parse an std::Iterator you can use this function

pub fn stream_read_items_at<T>(iterator: impl Iterator<Item = String> + 'static, prefix: String) -> impl Iterator<Item = serde_json::Result<T>>
where
    T: DeserializeOwned,

as per the example:

fn load_as_chars() -> impl Iterator<Item = u8> {
    let f = File::open("./tests/test.json").expect("failed to read test file");
    let b = BufReader::new(f);
    let reader = ReaderIter::new(b);
    reader.map(|e| e.expect("failed to read file"))
}

Using with Read

as per the example:

// use ...
use jsonit::JsonItError;
use log::info;

type TestResult = Result<(), JsonItError>;

fn test_string_with_type_at<T: DeserializeOwned + std::fmt::Debug>(data: &str, at: &str) -> TestResult {
    setup_logging();
    let reader = data.as_bytes();
    let prefix = at.as_bytes();

    let iterator = JsonSeqIterator::new(reader, prefix);

    for res in iterator {
        let item: T = res?;
        println!("{:?}", item);
    }

    Ok(())
}

fn reader_number_option() -> TestResult {
    let data = r#"{"a": [ [1,2,null]] }"#;
    test_string_with_type_at::<Vec<Option<i32>>>(data, "a")
}

Dependencies

~0.7–1.4MB
~33K SLoC