pub struct AsyncJsonLinesReader<R> { /* private fields */ }
async
only.Expand description
A structure for asynchronously reading JSON values from JSON Lines input.
An AsyncJsonLinesReader
wraps a tokio::io::AsyncBufRead
instance
and parses each line as a serde::de::DeserializeOwned
value in
JSON.
§Example
use futures_util::TryStreamExt;
use serde::Deserialize;
use serde_jsonlines::AsyncJsonLinesReader;
use tokio::fs::{write, File};
use tokio::io::BufReader;
#[derive(Debug, Deserialize, PartialEq)]
pub struct Structure {
pub name: String,
pub size: i32,
pub on: bool,
}
#[tokio::main]
async fn main() -> std::io::Result<()> {
write(
"example.jsonl",
concat!(
"{\"name\": \"Foo Bar\", \"on\":true,\"size\": 42 }\n",
"{ \"name\":\"Quux\", \"on\" : false ,\"size\": 23}\n",
" {\"name\": \"Gnusto Cleesh\" , \"on\": true, \"size\": 17}\n",
),
)
.await?;
let fp = BufReader::new(File::open("example.jsonl").await?);
let reader = AsyncJsonLinesReader::new(fp);
let items = reader
.read_all::<Structure>()
.try_collect::<Vec<_>>()
.await?;
assert_eq!(
items,
[
Structure {
name: "Foo Bar".into(),
size: 42,
on: true,
},
Structure {
name: "Quux".into(),
size: 23,
on: false,
},
Structure {
name: "Gnusto Cleesh".into(),
size: 17,
on: true,
},
]
);
Ok(())
}
Implementations§
Source§impl<R> AsyncJsonLinesReader<R>
impl<R> AsyncJsonLinesReader<R>
Sourcepub fn new(reader: R) -> Self
pub fn new(reader: R) -> Self
Construct a new AsyncJsonLinesReader
from a
tokio::io::AsyncBufRead
instance
Sourcepub fn into_inner(self) -> R
pub fn into_inner(self) -> R
Consume the AsyncJsonLinesReader
and return the underlying reader
Sourcepub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut R>
pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut R>
Get a pinned mutable reference to the underlying reader
Source§impl<R: AsyncBufRead> AsyncJsonLinesReader<R>
impl<R: AsyncBufRead> AsyncJsonLinesReader<R>
Sourcepub async fn read<T>(&mut self) -> Result<Option<T>>where
T: DeserializeOwned,
R: Unpin,
pub async fn read<T>(&mut self) -> Result<Option<T>>where
T: DeserializeOwned,
R: Unpin,
Asynchronously read & deserialize a line of JSON from the underlying reader.
If end-of-file is reached, this method returns Ok(None)
.
Note that separate calls to this method may read different types of values.
§Errors
Has the same error conditions as
tokio::io::AsyncBufReadExt::read_line()
and
serde_json::from_str()
. Note that, in the latter case (which can
be identified by the std::io::Error
having a serde_json::Error
value as its payload), continuing to read from the
AsyncJsonLinesReader
afterwards will pick up on the next line as
though the error never happened, so invalid JSON can be easily ignored
if you so wish.
Sourcepub fn read_all<T>(self) -> JsonLinesStream<R, T>
pub fn read_all<T>(self) -> JsonLinesStream<R, T>
Consume the AsyncJsonLinesReader
and return an asynchronous stream
over the deserialized JSON values from each line.
The returned stream has an Item
type of std::io::Result<T>
. Each
call to next()
has the same error conditions as
read()
.
Note that all deserialized values will be of the same type. If you
wish to read lines of varying types, use the
read()
method instead.
Trait Implementations§
Source§impl<R: Clone> Clone for AsyncJsonLinesReader<R>
impl<R: Clone> Clone for AsyncJsonLinesReader<R>
Source§fn clone(&self) -> AsyncJsonLinesReader<R>
fn clone(&self) -> AsyncJsonLinesReader<R>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more