formatjson

Crates.ioformatjson
lib.rsformatjson
version0.3.1
sourcesrc
created_at2024-04-07 19:22:06.500462
updated_at2024-04-29 18:48:41.69136
descriptionFormats JSON files
homepagehttps://github.com/tusharsadhwani/formatjson
repositoryhttps://github.com/tusharsadhwani/formatjson
max_upload_size
id1199443
size43,077
Tushar Sadhwani (tusharsadhwani)

documentation

https://docs.rs/formatjson

README

formatjson

A simple JSON validator and formatter written in Rust.

For documentation check the docs.rs page.

Installation

cargo install formatjson

Usage

  • As a command-line tool:

    $ formatjson example.json
    Successfully formatted example.json
    $ echo '{"foo": "bar"}' | formatjson
    {
      "foo": "bar"
    }
    
  • As a library:

    fn main() {
        let json = formatjson::format_json(r#"{"foo":"bar"}"#).unwrap();
        println!("{}", json);
    }
    
    $ cargo run
    {
      "foo": "bar"
    }
    

Speed

Running it on this 25MB JSON file gave the following results:

$ hyperfine 'formatjson large.json'
Benchmark 1: formatjson large.json
  Time (mean ± σ):     400.1 ms ±   9.3 ms    [User: 359.4 ms, System: 31.9 ms]
  Range (min … max):   390.1 ms … 419.6 ms    10 runs

Averaging 400 milliseconds on my M1 MacBook Air, which is about 38 times faster than pretter, which took 15.27 seconds:

$ time npx prettier --check large.json
Checking formatting...
[warn] large.json
[warn] Code style issues found in the above file. Run Prettier to fix.
npx prettier --check large.json  26.14s user 11.57s system 246% cpu 15.279 total
Commit count: 30

cargo fmt