pub trait JsonStreamResponse {
// Required methods
fn json_array_stream<'a, 'b, T>(
self,
max_obj_len: usize,
) -> impl Stream<Item = StreamBodyResult<T>> + Send + 'b
where T: for<'de> Deserialize<'de> + Send + 'b;
fn json_array_stream_with_capacity<'a, 'b, T>(
self,
max_obj_len: usize,
buf_capacity: usize,
) -> impl Stream<Item = StreamBodyResult<T>> + Send + 'b
where T: for<'de> Deserialize<'de> + Send + 'b;
fn json_nl_stream<'a, 'b, T>(
self,
max_obj_len: usize,
) -> impl Stream<Item = StreamBodyResult<T>> + Send + 'b
where T: for<'de> Deserialize<'de> + Send + 'b;
fn json_nl_stream_with_capacity<'a, 'b, T>(
self,
max_obj_len: usize,
buf_capacity: usize,
) -> impl Stream<Item = StreamBodyResult<T>> + Send + 'b
where T: for<'de> Deserialize<'de> + Send + 'b;
}json only.Expand description
Extension trait for reqwest::Response that provides streaming support for the JSON array
and JSON Lines (NL/NewLines) formats.
Required Methods§
Sourcefn json_array_stream<'a, 'b, T>(
self,
max_obj_len: usize,
) -> impl Stream<Item = StreamBodyResult<T>> + Send + 'bwhere
T: for<'de> Deserialize<'de> + Send + 'b,
fn json_array_stream<'a, 'b, T>(
self,
max_obj_len: usize,
) -> impl Stream<Item = StreamBodyResult<T>> + Send + 'bwhere
T: for<'de> Deserialize<'de> + Send + 'b,
Streams the response as a JSON array.
The stream will Deserialize entries as type T with a maximum size of max_obj_len
bytes. If max_obj_len is usize::MAX, lines will be read until a newline (\n)
character is reached.
§Example
use futures::stream::BoxStream as _;
use reqwest_streams::JsonStreamResponse as _;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize)]
struct MyTestStructure {
some_test_field: String
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
const MAX_OBJ_LEN: usize = 64 * 1024;
let _stream = reqwest::get("http://localhost:8080/json-array")
.await?
.json_array_stream::<MyTestStructure>(MAX_OBJ_LEN);
Ok(())
}Sourcefn json_array_stream_with_capacity<'a, 'b, T>(
self,
max_obj_len: usize,
buf_capacity: usize,
) -> impl Stream<Item = StreamBodyResult<T>> + Send + 'bwhere
T: for<'de> Deserialize<'de> + Send + 'b,
fn json_array_stream_with_capacity<'a, 'b, T>(
self,
max_obj_len: usize,
buf_capacity: usize,
) -> impl Stream<Item = StreamBodyResult<T>> + Send + 'bwhere
T: for<'de> Deserialize<'de> + Send + 'b,
Streams the response as a JSON array.
The stream will Deserialize entries as type T with a maximum size of max_obj_len
bytes. If max_obj_len is usize::MAX, lines will be read until a newline (\n)
character is reached.
buf_capacity is the initial capacity of the stream’s decoding buffer.
§Example
use futures::stream::BoxStream as _;
use reqwest_streams::JsonStreamResponse as _;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize)]
struct MyTestStructure {
some_test_field: String
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
const MAX_OBJ_LEN: usize = 64 * 1024;
const INITIAL_BUF_CAPACITY: usize = 16 * 1024;
let _stream = reqwest::get("http://localhost:8080/json-array")
.await?
.json_array_stream_with_capacity::<MyTestStructure>(MAX_OBJ_LEN, INITIAL_BUF_CAPACITY);
Ok(())
}Sourcefn json_nl_stream<'a, 'b, T>(
self,
max_obj_len: usize,
) -> impl Stream<Item = StreamBodyResult<T>> + Send + 'bwhere
T: for<'de> Deserialize<'de> + Send + 'b,
fn json_nl_stream<'a, 'b, T>(
self,
max_obj_len: usize,
) -> impl Stream<Item = StreamBodyResult<T>> + Send + 'bwhere
T: for<'de> Deserialize<'de> + Send + 'b,
Streams the response as JSON lines (NL/NewLines), where each line contains a JSON object.
The stream will Deserialize entries as type T with a maximum size of max_obj_len
bytes. If max_obj_len is usize::MAX, lines will be read until a newline (\n)
character is reached.
§Example
use futures::stream::BoxStream as _;
use reqwest_streams::JsonStreamResponse as _;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize)]
struct MyTestStructure {
some_test_field: String
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
const MAX_OBJ_LEN: usize = 64 * 1024;
let _stream = reqwest::get("http://localhost:8080/json-nl")
.await?
.json_nl_stream::<MyTestStructure>(MAX_OBJ_LEN);
Ok(())
}Sourcefn json_nl_stream_with_capacity<'a, 'b, T>(
self,
max_obj_len: usize,
buf_capacity: usize,
) -> impl Stream<Item = StreamBodyResult<T>> + Send + 'bwhere
T: for<'de> Deserialize<'de> + Send + 'b,
fn json_nl_stream_with_capacity<'a, 'b, T>(
self,
max_obj_len: usize,
buf_capacity: usize,
) -> impl Stream<Item = StreamBodyResult<T>> + Send + 'bwhere
T: for<'de> Deserialize<'de> + Send + 'b,
Streams the response as JSON lines (NL/NewLines), where each line contains a JSON object.
The stream will Deserialize entries as type T with a maximum size of max_obj_len
bytes. If max_obj_len is usize::MAX, lines will be read until a \n character
is reached.
§Example
use futures::stream::BoxStream as _;
use reqwest_streams::JsonStreamResponse as _;
use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Deserialize)]
struct MyTestStructure {
some_test_field: String
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
const MAX_OBJ_LEN: usize = 64 * 1024;
const INITIAL_BUF_CAPACITY: usize = 16 * 1024;
let _stream = reqwest::get("http://localhost:8080/json-nl")
.await?
.json_nl_stream_with_capacity::<MyTestStructure>(MAX_OBJ_LEN, INITIAL_BUF_CAPACITY);
Ok(())
}Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.