| Crates.io | large-json-array |
| lib.rs | large-json-array |
| version | 0.1.1 |
| created_at | 2025-06-06 10:39:19.524059+00 |
| updated_at | 2025-06-08 02:30:44.8282+00 |
| description | A streaming JSONโarray parser that yields one object at a time |
| homepage | |
| repository | https://github.com/bambolelooo/large-json-array |
| max_upload_size | |
| id | 1702800 |
| size | 31,528 |
Stream large JSON arrays in Rust using an efficient iterator-based approach.
Serde's default behavior loads entire JSON documents into memory. When dealing with very large JSON arrays, this
becomes inefficient or outright impossible. This crate provides a custom JsonStream<R: Read> iterator that streams and
parses objects from a JSON array one-by-one without allocating the entire array in memory.
Read source (e.g., files, network streams)serde_json::Valueuse std::fs::File;
use std::io::BufReader;
use large_json_array::json_stream::JsonStream;
use serde_json::Value;
fn main() -> Result<(), Box<dyn std::error::Error>> {
let file = File::open("huge.json")?;
let reader = BufReader::new(file);
let stream = JsonStream::new(reader);
for value in stream {
match value {
Ok(json_value) => println!("{:?}", json_value),
Err(e) => eprintln!("Error: {}", e),
}
}
Ok(())
}
/examples directory:Generate a large json file first:
cargo run --example generate_large_json
Then you can find all the Johns in the json (with --release flag it will run faster):
cargo run --release --example find_johns users_5.0_gb.json
The core struct is:
pub struct JsonStream<R: Read> {
...
}
It implements Iterator<Item = Result<Value, JsonError>> and maintains:
Parsing logic handles array delimiters, quoted strings, escape sequences, and ensures objects are emitted only when fully formed.
In your Cargo.toml:
[dependencies]
large-json-array = "0.1.0"
[ {...}, {...}, ... ])This project is licensed under the terms of the MIT license.
For more advanced use cases or improvements (e.g., error recovery, typed deserialization, or multi-threaded parsing), contributions and ideas are welcome!