pub struct JsonLinesReader<R> { /* private fields */ }
Expand description
A structure for reading JSON values from JSON Lines input.
A JsonLinesReader
wraps a std::io::BufRead
instance and parses each
line as a serde::de::DeserializeOwned
value in JSON.
§Example
use serde::Deserialize;
use serde_jsonlines::JsonLinesReader;
use std::fs::{write, File};
use std::io::BufReader;
#[derive(Debug, Deserialize, PartialEq)]
pub struct Structure {
pub name: String,
pub size: i32,
pub on: bool,
}
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",
),
)?;
let fp = BufReader::new(File::open("example.jsonl")?);
let reader = JsonLinesReader::new(fp);
let items = reader
.read_all::<Structure>()
.collect::<std::io::Result<Vec<_>>>()?;
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> JsonLinesReader<R>
impl<R> JsonLinesReader<R>
Sourcepub fn new(reader: R) -> Self
pub fn new(reader: R) -> Self
Construct a new JsonLinesReader
from a std::io::BufRead
instance
Sourcepub fn into_inner(self) -> R
pub fn into_inner(self) -> R
Consume the JsonLinesReader
and return the underlying reader
Sourcepub fn read_all<T>(self) -> JsonLinesIter<R, T> ⓘ
pub fn read_all<T>(self) -> JsonLinesIter<R, T> ⓘ
Consume the JsonLinesReader
and return an iterator over the
deserialized JSON values from each line.
The returned iterator 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.
Source§impl<R: BufRead> JsonLinesReader<R>
impl<R: BufRead> JsonLinesReader<R>
Sourcepub fn read<T>(&mut self) -> Result<Option<T>>where
T: DeserializeOwned,
pub fn read<T>(&mut self) -> Result<Option<T>>where
T: DeserializeOwned,
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 std::io::BufRead::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 JsonLinesReader
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.
Trait Implementations§
Source§impl<R: Clone> Clone for JsonLinesReader<R>
impl<R: Clone> Clone for JsonLinesReader<R>
Source§fn clone(&self) -> JsonLinesReader<R>
fn clone(&self) -> JsonLinesReader<R>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more