Git Product home page Git Product logo

serdeio's Introduction

SerdeIO

Tiny IO utility library for Rust to serialize/deserialize Serde compatible structs

Install

cargo add serdeio

SerdeIO supports JSON and JSON Lines formats. Additional formats are supported by enabling corresponding features.

  • yaml
  • csv

How to use

  • read_record_from_reader is used to read a deserializable type T from std::io::Read. Data format must be specified by DataFormat enum.
  • read_records_from_reader always tries to deserialize the data as Vec<T>.
  • read_record_from_file accepts an AsRef<Path>. Data format is automatically determined by file extension.
  • write_* functions follow the same rules as read_*.

Note that some data format like CSV and JSON Lines support only reading records Vec<T>.

Examples

The following code read a JSON file and parse it as Vec<User>. Then it encodes the data into YAML format and write it to STDOUT.

use anyhow::{anyhow, Context, Result as AnyResult};
use serde::{Deserialize, Serialize};
use serdeio::{read_record_from_file, write_records_to_writer, DataFormat};

#[derive(Debug, Deserialize, Serialize)]
struct User {
    id: u32,
    name: String,
    items: Vec<String>,
}

pub fn main() -> AnyResult<()> {
    // get input file path from argv
    let args: Vec<String> = std::env::args().collect();
    let input_file_path = &args[1];

    // read json file to memory
    let users: Vec<User> = read_record_from_file(input_file_path)
        .context("Failed to read records from file")?;

    // write to stdout in json lines format
    let writer = std::io::stdout();
    write_records_to_writer(writer, DataFormat::JsonLines, &users).unwrap();

    Ok(())
}

serdeio's People

Contributors

lucidfrontier45 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

serdeio's Issues

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.