pub struct AsyncJsonLinesWriter<W> { /* private fields */ }async only.Expand description
A structure for asynchronously writing JSON values as JSON Lines.
An AsyncJsonLinesWriter wraps a tokio::io::AsyncWrite instance
and writes serde::Serialize values to it by serializing each one as
a single line of JSON and appending a newline.
§Example
use serde::Serialize;
use serde_jsonlines::AsyncJsonLinesWriter;
use tokio::fs::{read_to_string, File};
#[derive(Serialize)]
pub struct Structure {
pub name: String,
pub size: i32,
pub on: bool,
}
#[tokio::main]
async fn main() -> std::io::Result<()> {
{
let fp = File::create("example.jsonl").await?;
let mut writer = AsyncJsonLinesWriter::new(fp);
writer
.write(&Structure {
name: "Foo Bar".into(),
size: 42,
on: true,
})
.await?;
writer
.write(&Structure {
name: "Quux".into(),
size: 23,
on: false,
})
.await?;
writer
.write(&Structure {
name: "Gnusto Cleesh".into(),
size: 17,
on: true,
})
.await?;
writer.flush().await?;
}
// End the block to close the writer
assert_eq!(
read_to_string("example.jsonl").await?,
concat!(
"{\"name\":\"Foo Bar\",\"size\":42,\"on\":true}\n",
"{\"name\":\"Quux\",\"size\":23,\"on\":false}\n",
"{\"name\":\"Gnusto Cleesh\",\"size\":17,\"on\":true}\n",
)
);
Ok(())
}Implementations§
Source§impl<W> AsyncJsonLinesWriter<W>
impl<W> AsyncJsonLinesWriter<W>
Sourcepub fn new(writer: W) -> Self
pub fn new(writer: W) -> Self
Construct a new AsyncJsonLinesWriter from a
tokio::io::AsyncWrite instance
Sourcepub fn into_inner(self) -> W
pub fn into_inner(self) -> W
Consume the AsyncJsonLinesWriter and return the underlying writer
Sourcepub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut W>
pub fn get_pin_mut(self: Pin<&mut Self>) -> Pin<&mut W>
Get a pinned mutable reference to the underlying writer
Sourcepub fn into_sink<T>(self) -> JsonLinesSink<W, T>
pub fn into_sink<T>(self) -> JsonLinesSink<W, T>
Consume the AsyncJsonLinesWriter and return an asynchronous sink
for serializing values as JSON and writing them to the underlying
writer.
The returned sink consumes T values and has an Error type of
std::io::Error. Each call to send() has the same error
conditions as write().
Note that all values sent to the sink must be of the same type. If you
wish to write values of varying types, use the
write() method.
Source§impl<W: AsyncWrite> AsyncJsonLinesWriter<W>
impl<W: AsyncWrite> AsyncJsonLinesWriter<W>
Sourcepub async fn write<T>(&mut self, value: &T) -> Result<()>
pub async fn write<T>(&mut self, value: &T) -> Result<()>
Serialize a value as a line of JSON and write it asynchronously to the underlying writer, followed by a newline.
Note that separate calls to this method may write different types of values.
§Errors
Has the same error conditions as serde_json::to_writer() and
tokio::io::AsyncWriteExt::write_all().
Trait Implementations§
Source§impl<W: Clone> Clone for AsyncJsonLinesWriter<W>
impl<W: Clone> Clone for AsyncJsonLinesWriter<W>
Source§fn clone(&self) -> AsyncJsonLinesWriter<W>
fn clone(&self) -> AsyncJsonLinesWriter<W>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more