10 releases

new 0.3.1 Apr 29, 2024
0.3.0 Apr 29, 2024
0.2.1 Apr 10, 2024
0.1.5 Apr 7, 2024

#601 in Text processing

Download history 33/week @ 2024-04-01 211/week @ 2024-04-08

244 downloads per month

MIT license

25KB
535 lines

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

Dependencies

~3–5MB
~97K SLoC