Expand description
JSON Lines (a.k.a. newline-delimited JSON) is a
simple format for storing sequences of JSON values in which each value is
serialized on a single line and terminated by a newline sequence. The
serde-jsonlines
crate provides functionality for reading & writing these
documents (whether all at once or line by line) using serde
’s
serialization & deserialization features.
Basic usage involves simply importing the BufReadExt
or WriteExt
extension trait and then using the json_lines()
or write_json_lines()
method on a BufRead
or Write
value to read or write a sequence of JSON Lines values.
Convenience functions are also provided for the common case of reading or
writing a JSON Lines file given as a filepath.
At a lower level, values can be read or written one at a time (which is
useful if, say, different lines are different types) by wrapping a
BufRead
or Write
value in a JsonLinesReader
or JsonLinesWriter
and then calling the wrapped structure’s read()
or write()
method, respectively.
When the async
feature is enabled, analogous types for working with JSON
Lines asynchronously under tokio
become available.
§Example
use serde::{Deserialize, Serialize};
use serde_jsonlines::{json_lines, write_json_lines};
use std::io::Result;
#[derive(Debug, Deserialize, Eq, PartialEq, Serialize)]
pub struct Structure {
pub name: String,
pub size: i32,
pub on: bool,
}
fn main() -> Result<()> {
let values = vec![
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,
},
];
write_json_lines("example.jsonl", &values)?;
let values2 = json_lines("example.jsonl")?.collect::<Result<Vec<Structure>>>()?;
assert_eq!(values, values2);
Ok(())
}
Structs§
- Async
Json Lines Reader async
- A structure for asynchronously reading JSON values from JSON Lines input.
- Async
Json Lines Writer async
- A structure for asynchronously writing JSON values as JSON Lines.
- Json
Lines Iter - An iterator over the lines of a
BufRead
valueR
that decodes each line as JSON of typeT
. - Json
Lines Reader - A structure for reading JSON values from JSON Lines input.
- Json
Lines Sink async
- An asynchronous sink that serializes input values of type
T
as JSON and writes them to the underlyingAsyncWrite
valueW
. - Json
Lines Stream async
- An asynchronous stream over the lines of an
AsyncBufRead
valueR
that decodes each line as JSON of typeT
. - Json
Lines Writer - A structure for writing JSON values as JSON Lines.
Traits§
- Async
BufRead Json Lines async
- An extension trait for the
tokio::io::AsyncBufRead
trait that adds ajson_lines()
method - Async
Write Json Lines async
- An extension trait for the
tokio::io::AsyncWrite
trait that adds aninto_json_lines_sink()
method - BufRead
Ext - An extension trait for the
std::io::BufRead
trait that adds ajson_lines()
method - Write
Ext - An extension trait for the
std::io::Write
trait that adds awrite_json_lines()
method
Functions§
- append_
json_ lines - Append an iterator of values to the file at
path
as JSON Lines. - json_
lines - Iterate over JSON Lines values from a file.
- write_
json_ lines - Write an iterator of values to the file at
path
as JSON Lines.
Type Aliases§
- Json
Lines File Iter - A type alias for a
JsonLinesIter
on a buffered file object.