Crate http_json_stream

Crate http_json_stream 

Source
Expand description

An asynchronous JSON streamer for HTTP network requests.

See the README for additional information, Installation and Features.

github-com
crates-io
docs-rs

§Example

use futures_util::stream::{StreamExt, TryStreamExt};
use http_json_stream::{JsonPart, JsonStream};
use serde::de::DeserializeOwned;
use std::fmt::Debug;

async fn log_json_list<T: Debug + DeserializeOwned>(url: &str) {
    let fut = Box::pin(reqwest::get(url));
    // expected JSON response: '[{}, {}, {}]'
    let mut stream = JsonStream::<_, _, T>::request(fut, JsonPart::level(1));
    while let Some(item) = stream.try_next().await.unwrap() {
        println!("{:?}", item);
    }
}

async fn log_error_response<T>(resp: reqwest::Response) {
    if resp.status().is_client_error() {
        // expected JSON response: '{ "errors": [{}, {}, {}] }'
        let mut stream = JsonStream::process(resp, JsonPart::level(2).group(0));
        while let Some(item) = stream.next().await {
            let item: serde_json::Value = item.unwrap();
            println!("{:?}", item);
        }
    }
}

See JsonStream and JsonPart for more information.

Structs§

JsonPart
A filter for PartialJson to determine which items to deserialize.
JsonStream
An asynchronous JSON streamer for HTTP network requests.
PartialJson
A Write or Extend interface to push JSON data.

Enums§

BodyDecoder
A streaming decoder that abstracts over supported content encodings.
ContentEncoding
Supported HTTP Content-Encoding headers.
Error
Errors which can occur while streaming an HTTP JSON response.

Type Aliases§

Result
Type alias for the http_json_stream::Error result.