pub fn json_lines<T, P: AsRef<Path>>(path: P) -> Result<JsonLinesFileIter<T>>
Expand description
Iterate over JSON Lines values from a file.
json_lines(path)
returns an iterator of values deserialized from the JSON
Lines in the file at path
.
The returned iterator has an Item
type of std::io::Result<T>
. Each
call to next()
has the same error conditions as
JsonLinesReader::read()
.
§Errors
Has the same error conditions as File::open()
.
§Example
use serde::Deserialize;
use serde_jsonlines::json_lines;
use std::fs::write;
use std::io::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 items = json_lines::<Structure, _>("example.jsonl")?.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(())
}