GithubHelp home page GithubHelp logo

jwodder / serde-jsonlines Goto Github PK

View Code? Open in Web Editor NEW
19.0 2.0 2.0 111 KB

Read & write JSON Lines documents

License: MIT License

Rust 100.00%
json json-lines rust serde serde-json jsonlines available-on-crates-io

serde-jsonlines's Introduction

Project Status: Active โ€“ The project has reached a stable, usable state and is being actively developed. CI Status codecov.io Minimum Supported Rust Version MIT License

GitHub | crates.io | Documentation | Issues | Changelog

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(())
}

serde-jsonlines's People

Contributors

dependabot[bot] avatar jwodder avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

serde-jsonlines's Issues

Renovate Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Awaiting Schedule

These updates are awaiting their schedule. Click on a checkbox to get an update now.

  • [cargo] Autoupdate Cargo.lock

Detected dependencies

cargo
Cargo.toml
  • futures-core 0.3.29
  • futures-sink 0.3.29
  • pin-project-lite 0.2.9
  • serde 1.0.147
  • serde_json 1.0.87
  • tokio 1.21.2
  • assert_fs 1.0.7
  • futures-util 0.3.29
  • serde 1.0.147
  • tokio 1.21.2
github-actions
.github/workflows/test.yml
  • actions/checkout v4
  • Swatinem/rust-cache v2
  • actions/checkout v4
  • Swatinem/rust-cache v2
  • taiki-e/install-action v2
  • actions/checkout v4
  • Swatinem/rust-cache v2
  • taiki-e/install-action v2
  • codecov/codecov-action v4
  • actions/checkout v4
  • Swatinem/rust-cache v2
  • actions/checkout v4
  • Swatinem/rust-cache v2

  • Check this box to trigger a request for Renovate to run again on this repository

support writing to String instead of File?

Hi,

I do not have disk where this code runs and would ideally write out the raw JSON Lines data as a byte array to my code that separately handles uploading the bytes to a file in a server.

Would that be possible to support writing to a String in memory?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.