https://github.com/jwodder/serde-jsonlines
Read & write JSON Lines documents
https://github.com/jwodder/serde-jsonlines
available-on-crates-io json json-lines jsonlines rust serde serde-json
Last synced: 3 months ago
JSON representation
Read & write JSON Lines documents
- Host: GitHub
- URL: https://github.com/jwodder/serde-jsonlines
- Owner: jwodder
- License: mit
- Created: 2022-10-27T16:08:16.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2025-02-09T21:51:37.000Z (5 months ago)
- Last Synced: 2025-03-31T06:07:37.337Z (3 months ago)
- Topics: available-on-crates-io, json, json-lines, jsonlines, rust, serde, serde-json
- Language: Rust
- Homepage:
- Size: 92.8 KB
- Stars: 28
- Watchers: 2
- Forks: 4
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://www.repostatus.org/#active)
[](https://github.com/jwodder/serde-jsonlines/actions/workflows/test.yml)
[](https://codecov.io/gh/jwodder/serde-jsonlines)
[](https://www.rust-lang.org)
[](https://opensource.org/licenses/MIT)[GitHub](https://github.com/jwodder/serde-jsonlines) | [crates.io](https://crates.io/crates/serde-jsonlines) | [Documentation](https://docs.rs/serde-jsonlines) | [Issues](https://github.com/jwodder/serde-jsonlines/issues) | [Changelog](https://github.com/jwodder/serde-jsonlines/blob/master/CHANGELOG.md)
[JSON Lines](https://jsonlines.org) (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
=======```rust
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::>>()?;
assert_eq!(values, values2);
Ok(())
}
```