pub trait BufReadExt: BufRead {
// Provided method
fn json_lines<T>(self) -> JsonLinesIter<Self, T> ⓘ
where Self: Sized { ... }
}
Expand description
An extension trait for the std::io::BufRead
trait that adds a
json_lines()
method
§Example
use serde::Deserialize;
use serde_jsonlines::BufReadExt;
use std::fs::{write, File};
use std::io::{BufReader, Result};
#[derive(Debug, Deserialize, PartialEq)]
pub struct Structure {
pub name: String,
pub size: i32,
pub on: bool,
}
fn main() -> 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 items = fp.json_lines::<Structure>().collect::<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(())
}
Provided Methods§
Sourcefn json_lines<T>(self) -> JsonLinesIter<Self, T> ⓘwhere
Self: Sized,
fn json_lines<T>(self) -> JsonLinesIter<Self, T> ⓘwhere
Self: Sized,
Consume the reader 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
JsonLinesReader::read()
.
Note that all deserialized values will be of the same type.