Crates.io | formatjson |
lib.rs | formatjson |
version | 0.3.1 |
source | src |
created_at | 2024-04-07 19:22:06.500462 |
updated_at | 2024-04-29 18:48:41.69136 |
description | Formats JSON files |
homepage | https://github.com/tusharsadhwani/formatjson |
repository | https://github.com/tusharsadhwani/formatjson |
max_upload_size | |
id | 1199443 |
size | 43,077 |
A simple JSON validator and formatter written in Rust.
For documentation check the docs.rs page.
cargo install formatjson
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"
}
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