Home

Awesome

Jsonxf

A JSON transformer, written in Rust.

Provides fast pretty-printing and minimizing of JSON-encoded strings and streams, at the command line or within Rust programs.

Jsonxf is built for speed, and does not attempt to perform any input validation whatsoever. Valid input produces valid output, but no guarantees are offered around the detection and rejection of invalid input.

Crate docs: https://docs.rs/jsonxf/

Installation

cargo install jsonxf

Command-line Examples

Pretty-print a string to the terminal, using two spaces to indent:

jsonxf -s '{"a": {"b": 2, "c": false}}'

Pretty-print and read a JSON file, using a tab character to indent:

jsonxf -t $'\t' <foo.json | less

Minimize a file and gzip it:

jsonxf -m <foo.json | gzip -c >foo-min.json.gz

Run jsonxf -h to see all configuration options.

Rust Example

In your Cargo.toml:

[dependencies]
jsonxf = "1.1"

In your code:

extern crate jsonxf;
let ugly_json = "{\"hello\":\"world\"}";
let pretty_json = jsonxf::pretty_print(ugly_json).unwrap();
assert_eq!(pretty_json, "{\n  \"hello\": \"world\"\n}\n");

Performance

Here are some benchmarks comparing Jsonxf 0.9's performance to several of its counterparts:

Test platform: MBP (early 2013), macOS 10.13.2, 3GHz i7, 8GB RAM.

See benchmark.rb for testing procedure.

Pretty-print test, 600MB minimized input (1M objects):

commandtime (s)relative timenotes
cat2.260.3xcat is a bad pretty-printer
jsonxf7.761x
serdexf9.331.2xno newlines between objects 🙁
jsonpp25.03.2x
jq -M .67.08.6x

Minimize test, 850MB pretty-printed input (1M objects):

commandtime (s)relative timenotes
cat1.450.2xcat is a bad minimizer
jsonxf -m6.781x
serdexf -m7.301.1x
jsonpp--minimizing is not supported 😭
jq -cM .78.012x

Authorship and License

Copyright 2017-2020, Pete Gamache.

Jsonxf is released under the MIT License.