serde_jsonlines/lib.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742
#![cfg_attr(docsrs, feature(doc_cfg))]
//! 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()`][BufReadExt::json_lines]
//! or [`write_json_lines()`][WriteExt::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()`][JsonLinesReader::read]
//! or [`write()`][JsonLinesWriter::write] method, respectively.
//!
//! When the `async` feature is enabled, analogous types for working with JSON
//! Lines asynchronously under [`tokio`] become available.
//!
//! Example
//! =======
//!
//! ```no_run
//! 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(())
//! }
//! ```
use serde::{de::DeserializeOwned, Serialize};
use std::fs::{File, OpenOptions};
use std::io::{BufRead, BufReader, BufWriter, Result, Write};
use std::marker::PhantomData;
use std::path::Path;
#[cfg(feature = "async")]
mod asynclib;
#[cfg(feature = "async")]
pub use asynclib::*;
/// A type alias for a [`JsonLinesIter`] on a buffered file object.
///
/// This is the return type of [`json_lines()`].
pub type JsonLinesFileIter<T> = JsonLinesIter<BufReader<File>, T>;
/// A structure for writing JSON values as JSON Lines.
///
/// A `JsonLinesWriter` wraps a [`std::io::Write`] instance and writes
/// [`serde::Serialize`] values to it by serializing each one as a single line
/// of JSON and appending a newline.
///
/// # Example
///
/// ```no_run
/// use serde::Serialize;
/// use serde_jsonlines::JsonLinesWriter;
/// use std::fs::{read_to_string, File};
///
/// #[derive(Serialize)]
/// pub struct Structure {
/// pub name: String,
/// pub size: i32,
/// pub on: bool,
/// }
///
/// fn main() -> std::io::Result<()> {
/// {
/// let fp = File::create("example.jsonl")?;
/// let mut writer = JsonLinesWriter::new(fp);
/// writer.write_all([
/// 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,
/// },
/// ])?;
/// writer.flush()?;
/// }
/// // End the block to close the writer
/// assert_eq!(
/// read_to_string("example.jsonl")?,
/// concat!(
/// "{\"name\":\"Foo Bar\",\"size\":42,\"on\":true}\n",
/// "{\"name\":\"Quux\",\"size\":23,\"on\":false}\n",
/// "{\"name\":\"Gnusto Cleesh\",\"size\":17,\"on\":true}\n",
/// )
/// );
/// Ok(())
/// }
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct JsonLinesWriter<W> {
inner: W,
}
impl<W> JsonLinesWriter<W> {
/// Construct a new `JsonLinesWriter` from a [`std::io::Write`] instance
pub fn new(writer: W) -> Self {
JsonLinesWriter { inner: writer }
}
/// Consume the `JsonLinesWriter` and return the underlying writer
pub fn into_inner(self) -> W {
self.inner
}
/// Get a reference to the underlying writer
pub fn get_ref(&self) -> &W {
&self.inner
}
/// Get a mutable reference to the underlying writer
pub fn get_mut(&mut self) -> &mut W {
&mut self.inner
}
}
impl<W: Write> JsonLinesWriter<W> {
/// Serialize a value as a line of JSON and write it to the underlying
/// writer, followed by a newline.
///
/// Note that separate calls to this method may write different types of
/// values.
///
/// # Errors
///
/// Has the same error conditions as [`serde_json::to_writer()`] and
/// [`std::io::Write::write_all()`].
pub fn write<T>(&mut self, value: &T) -> Result<()>
where
T: ?Sized + Serialize,
{
serde_json::to_writer(&mut self.inner, value)?;
self.inner.write_all(b"\n")?;
Ok(())
}
/// Serialize each item in an iterator as a line of JSON, and write out
/// each one followed by a newline to the underlying writer.
///
/// All values in a single call to `write_all()` must be the same type, but
/// separate calls may write different types.
///
/// # Errors
///
/// Has the same error conditions as [`write()`][JsonLinesWriter::write].
pub fn write_all<T, I>(&mut self, items: I) -> Result<()>
where
I: IntoIterator<Item = T>,
T: Serialize,
{
for value in items {
self.write(&value)?;
}
Ok(())
}
/// Flush the underlying writer.
///
/// Neither [`write()`][JsonLinesWriter::write] nor
/// [`write_all()`][JsonLinesWriter::write_all] flush the writer, so you
/// must explicitly call this method if you need output flushed.
///
/// # Errors
///
/// Has the same error conditions as [`std::io::Write::flush()`].
pub fn flush(&mut self) -> Result<()> {
self.inner.flush()
}
}
/// A structure for reading JSON values from JSON Lines input.
///
/// A `JsonLinesReader` wraps a [`std::io::BufRead`] instance and parses each
/// line as a [`serde::de::DeserializeOwned`] value in JSON.
///
/// # Example
///
/// ```no_run
/// use serde::Deserialize;
/// use serde_jsonlines::JsonLinesReader;
/// use std::fs::{write, File};
/// use std::io::BufReader;
///
/// #[derive(Debug, Deserialize, PartialEq)]
/// pub struct Structure {
/// pub name: String,
/// pub size: i32,
/// pub on: bool,
/// }
///
/// fn main() -> std::io::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 reader = JsonLinesReader::new(fp);
/// let items = reader
/// .read_all::<Structure>()
/// .collect::<std::io::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(())
/// }
/// ```
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct JsonLinesReader<R> {
inner: R,
}
impl<R> JsonLinesReader<R> {
/// Construct a new `JsonLinesReader` from a [`std::io::BufRead`] instance
pub fn new(reader: R) -> Self {
JsonLinesReader { inner: reader }
}
/// Consume the `JsonLinesReader` and return the underlying reader
pub fn into_inner(self) -> R {
self.inner
}
/// Get a reference to the underlying reader
pub fn get_ref(&self) -> &R {
&self.inner
}
/// Get a mutable reference to the underlying reader
pub fn get_mut(&mut self) -> &mut R {
&mut self.inner
}
/// Consume the `JsonLinesReader` 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
/// [`read()`][JsonLinesReader::read].
///
/// Note that all deserialized values will be of the same type. If you
/// wish to read lines of varying types, use the
/// [`read()`][JsonLinesReader::read] method instead.
pub fn read_all<T>(self) -> JsonLinesIter<R, T> {
JsonLinesIter {
reader: self,
_output: PhantomData,
}
}
}
impl<R: BufRead> JsonLinesReader<R> {
/// Read & deserialize a line of JSON from the underlying reader.
///
/// If end-of-file is reached, this method returns `Ok(None)`.
///
/// Note that separate calls to this method may read different types of
/// values.
///
/// # Errors
///
/// Has the same error conditions as [`std::io::BufRead::read_line()`] and
/// [`serde_json::from_str()`]. Note that, in the latter case (which can
/// be identified by the [`std::io::Error`] having a [`serde_json::Error`]
/// value as its payload), continuing to read from the `JsonLinesReader`
/// afterwards will pick up on the next line as though the error never
/// happened, so invalid JSON can be easily ignored if you so wish.
pub fn read<T>(&mut self) -> Result<Option<T>>
where
T: DeserializeOwned,
{
let mut s = String::new();
let r = self.inner.read_line(&mut s)?;
if r == 0 {
Ok(None)
} else {
Ok(Some(serde_json::from_str::<T>(&s)?))
}
}
}
/// An iterator over the lines of a [`BufRead`] value `R` that decodes each
/// line as JSON of type `T`.
///
/// This iterator yields items of type `Result<T, std::io::Error>`. Errors
/// occurr under the same conditions as for [`JsonLinesReader::read()`].
///
/// Iterators of this type are returned by [`JsonLinesReader::read_all()`],
/// [`BufReadExt::json_lines()`], and [`json_lines()`].
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct JsonLinesIter<R, T> {
reader: JsonLinesReader<R>,
_output: PhantomData<T>,
}
impl<R, T> Iterator for JsonLinesIter<R, T>
where
T: DeserializeOwned,
R: BufRead,
{
type Item = Result<T>;
fn next(&mut self) -> Option<Result<T>> {
self.reader.read().transpose()
}
}
/// An extension trait for the [`std::io::Write`] trait that adds a
/// `write_json_lines()` method
///
/// # Example
///
/// ```no_run
/// use serde::Serialize;
/// use serde_jsonlines::WriteExt;
/// use std::fs::{read_to_string, File};
/// use std::io::Write;
///
/// #[derive(Serialize)]
/// pub struct Structure {
/// pub name: String,
/// pub size: i32,
/// pub on: bool,
/// }
///
/// fn main() -> std::io::Result<()> {
/// {
/// let mut fp = File::create("example.jsonl")?;
/// fp.write_json_lines([
/// 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,
/// },
/// ])?;
/// fp.flush()?;
/// }
/// // End the block to close the writer
/// assert_eq!(
/// read_to_string("example.jsonl")?,
/// concat!(
/// "{\"name\":\"Foo Bar\",\"size\":42,\"on\":true}\n",
/// "{\"name\":\"Quux\",\"size\":23,\"on\":false}\n",
/// "{\"name\":\"Gnusto Cleesh\",\"size\":17,\"on\":true}\n",
/// )
/// );
/// Ok(())
/// }
/// ```
pub trait WriteExt: Write {
/// Serialize each item in an iterator as a line of JSON, and write out
/// each one followed by a newline.
///
/// All values in a single call to `write_json_lines()` must be the same
/// type, but separate calls may write different types.
///
/// This method does not flush.
///
/// # Errors
///
/// Has the same error conditions as [`serde_json::to_writer()`] and
/// [`std::io::Write::write_all()`].
fn write_json_lines<T, I>(&mut self, items: I) -> Result<()>
where
I: IntoIterator<Item = T>,
T: Serialize,
{
for value in items {
serde_json::to_writer(&mut *self, &value)?;
self.write_all(b"\n")?;
}
Ok(())
}
}
impl<W: Write> WriteExt for W {}
/// An extension trait for the [`std::io::BufRead`] trait that adds a
/// `json_lines()` method
///
/// # Example
///
/// ```no_run
/// 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(())
/// }
/// ```
pub trait BufReadExt: BufRead {
/// 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.
fn json_lines<T>(self) -> JsonLinesIter<Self, T>
where
Self: Sized,
{
JsonLinesReader::new(self).read_all()
}
}
impl<R: BufRead> BufReadExt for R {}
/// Write an iterator of values to the file at `path` as JSON Lines.
///
/// If the file does not already exist, it is created. If it does exist, any
/// contents are discarded.
///
/// # Errors
///
/// Has the same error conditions as [`File::create()`],
/// [`serde_json::to_writer()`], [`std::io::Write::write_all()`], and
/// [`std::io::Write::flush()`].
///
/// # Example
///
/// ```no_run
/// use serde::Serialize;
/// use serde_jsonlines::write_json_lines;
/// use std::fs::read_to_string;
///
/// #[derive(Serialize)]
/// pub struct Structure {
/// pub name: String,
/// pub size: i32,
/// pub on: bool,
/// }
///
/// fn main() -> std::io::Result<()> {
/// write_json_lines(
/// "example.jsonl",
/// [
/// 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,
/// },
/// ],
/// )?;
/// assert_eq!(
/// read_to_string("example.jsonl")?,
/// concat!(
/// "{\"name\":\"Foo Bar\",\"size\":42,\"on\":true}\n",
/// "{\"name\":\"Quux\",\"size\":23,\"on\":false}\n",
/// "{\"name\":\"Gnusto Cleesh\",\"size\":17,\"on\":true}\n",
/// )
/// );
/// Ok(())
/// }
/// ```
pub fn write_json_lines<P, I, T>(path: P, items: I) -> Result<()>
where
P: AsRef<Path>,
I: IntoIterator<Item = T>,
T: Serialize,
{
let mut fp = BufWriter::new(File::create(path)?);
fp.write_json_lines(items)?;
fp.flush()
}
/// Append an iterator of values to the file at `path` as JSON Lines.
///
/// If the file does not already exist, it is created. If it does exist, the
/// new lines are added after any lines that are already present.
///
/// # Errors
///
/// Has the same error conditions as [`File::create()`],
/// [`serde_json::to_writer()`], [`std::io::Write::write_all()`], and
/// [`std::io::Write::flush()`].
///
/// # Example
///
/// ```no_run
/// use serde::Serialize;
/// use serde_jsonlines::append_json_lines;
/// use std::fs::read_to_string;
///
/// #[derive(Serialize)]
/// pub struct Structure {
/// pub name: String,
/// pub size: i32,
/// pub on: bool,
/// }
///
/// fn main() -> std::io::Result<()> {
/// append_json_lines(
/// "example.jsonl",
/// [
/// Structure {
/// name: "Foo Bar".into(),
/// size: 42,
/// on: true,
/// },
/// Structure {
/// name: "Quux".into(),
/// size: 23,
/// on: false,
/// },
/// ],
/// )?;
/// assert_eq!(
/// read_to_string("example.jsonl")?,
/// concat!(
/// "{\"name\":\"Foo Bar\",\"size\":42,\"on\":true}\n",
/// "{\"name\":\"Quux\",\"size\":23,\"on\":false}\n",
/// )
/// );
/// append_json_lines(
/// "example.jsonl",
/// [
/// Structure {
/// name: "Gnusto Cleesh".into(),
/// size: 17,
/// on: true,
/// },
/// Structure {
/// name: "baz".into(),
/// size: 69105,
/// on: false,
/// },
/// ],
/// )?;
/// assert_eq!(
/// read_to_string("example.jsonl")?,
/// concat!(
/// "{\"name\":\"Foo Bar\",\"size\":42,\"on\":true}\n",
/// "{\"name\":\"Quux\",\"size\":23,\"on\":false}\n",
/// "{\"name\":\"Gnusto Cleesh\",\"size\":17,\"on\":true}\n",
/// "{\"name\":\"baz\",\"size\":69105,\"on\":false}\n",
/// )
/// );
/// Ok(())
/// }
/// ```
pub fn append_json_lines<P, I, T>(path: P, items: I) -> Result<()>
where
P: AsRef<Path>,
I: IntoIterator<Item = T>,
T: Serialize,
{
let mut fp = BufWriter::new(OpenOptions::new().append(true).create(true).open(path)?);
fp.write_json_lines(items)?;
fp.flush()
}
/// 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
///
/// ```no_run
/// 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(())
/// }
/// ```
pub fn json_lines<T, P: AsRef<Path>>(path: P) -> Result<JsonLinesFileIter<T>> {
let fp = BufReader::new(File::open(path)?);
Ok(fp.json_lines())
}