| Crates.io | incremental-writer |
| lib.rs | incremental-writer |
| version | 0.1.2 |
| created_at | 2020-08-11 20:01:45.063353+00 |
| updated_at | 2020-08-13 20:37:46.656881+00 |
| description | A wrapper around a file writer that appends json objects to an array in a file |
| homepage | |
| repository | https://github.com/BaronVonDrew/incremental-writer.git |
| max_upload_size | |
| id | 275472 |
| size | 11,197 |
incremental-writer is a library which provides writers to write in different formats to files on disk, currently only JSON is supported. incremental_writer::json::IncrementalJsonWriter provides a writer that takes a File writer and incrementally writes JSON objects to an array inside that file using serde_json.
Works on both Windows and Unix.
It implements the write trait's write(&[u8]) and flush() as well as write_json() which takes any serialisable object and writes it to the underlying array.
example:
extern crate incremental_writer;
use incremental_writer::json;
use serde::{Serialize, Deserialize};
fn main() {
let rows: Vec<Record> = vec![0, 1, 2, 3, 4, 5, 6, 7 , 8, 9, 10]
.iter()
.map(|num| Record { name: String::from("Test"), detail: *num})
.collect();
let out = std::fs::File::create("test.json").unwrap();
let mut writer = json::IncrementalJsonWriter::new(out);
for row in rows {
//the element is written to the file on each iteration
//if it stops before finishing, the JSON is still valid
writer.write_json(&row).unwrap();
}
}
#[derive(Serialize, Deserialize, Debug)]
struct Record {
name: String,
detail: u32
}