asyncjsonstream 0.1.0

Async JSON stream reader for selective parsing of large payloads
Documentation

asyncjsonstream

Async JSON stream reader for selective parsing of large payloads. This is the standalone home of Extract's AsyncJsonStreamReader implementation.

crates.io docs.rs license ci

Why asyncjsonstream

  • Stream through large JSON without deserializing the full payload.
  • Selectively read keys, values, and arrays using a tokenized reader.
  • Handles chunk boundaries and escaped strings correctly.
  • Built on Tokio AsyncRead.
  • No unsafe code.

Install

cargo add asyncjsonstream

Quick start

use asyncjsonstream::AsyncJsonStreamReader;
use std::io::Cursor;

#[tokio::main]
async fn main() -> Result<(), asyncjsonstream::AsyncJsonStreamReaderError> {
    let data = r#"{"status":"success","results":[{"id":1},{"id":2}]}"#;
    let mut reader = AsyncJsonStreamReader::new(Cursor::new(data.as_bytes().to_vec()));

    while let Some(key) = reader.next_object_entry().await? {
        match key.as_str() {
            "status" => {
                let status = reader.read_string().await?;
                println!("status={status}");
            }
            "results" => {
                while reader.start_array_item().await? {
                    let obj = reader.deserialize_object().await?;
                    println!("id={}", obj["id"]);
                }
            }
            _ => {}
        }
    }

    Ok(())
}

Common patterns

  • Read object entries with next_object_entry.
  • Skip values by calling next_object_entry again without consuming the value.
  • Stream arrays with start_array_item.
  • Parse string/number/bool with read_string, read_number, read_boolean.
  • Deserialize a sub-object with deserialize_object.

Error handling

All fallible operations return AsyncJsonStreamReaderError:

  • Io for reader failures
  • JsonError for malformed JSON
  • UnexpectedToken when the stream doesn't match the expected structure

MSRV

Minimum supported Rust version is 1.74.

License

Licensed under either of:

  • Apache License, Version 2.0 (LICENSE-APACHE)
  • MIT license (LICENSE-MIT)

at your option.