Compare commits

...

25 commits
v1.1 ... master

Author SHA1 Message Date
7d287c2769 cli-1.0.0 2025-03-21 18:26:17 +01:00
25837cf4db 2.1.0 2025-03-21 18:23:56 +01:00
8f5b2ce07a cleanup 2025-03-21 18:22:58 +01:00
99c215c7ed update lockfile 2025-03-21 18:20:49 +01:00
d81777b8a3
Merge pull request #7 from JDepooter/handle_crlf_input
Handle carriage returns in source JSON whitespace
2025-03-21 18:19:47 +01:00
Joel Depooter
78180884c6 Handle carriage returns in source JSON whitespace
Previously any carriage return (\r) bytes in the JSON whitespace were not ignored. Instead they were included in the formatted output. This led to very bad formatting, as the indentation would include the carriage return.

With this change, carriage returns are handled the same way that newlines are: they are ignored in the source JSON whitespace.

It might be better to detect if the source JSON is using Windows (\r\n) newlines, and preserve that on the output. However, that is a much bigger change. For now I think it is sufficient to simply produce necely formatted output which uses only \n newlines.
2025-02-25 11:35:11 -08:00
84fd27e557
Delete benches/large-file.json 2024-10-06 22:42:08 +02:00
2a5432d7a6
Update README.md 2023-09-22 22:00:45 +02:00
cb1b33f065 add bench to include 2022-04-28 21:21:45 +02:00
a84ca2dc5a prepare for release 2 2022-04-28 21:19:17 +02:00
7aed458197 prepare for release 2022-04-28 21:16:43 +02:00
35332a1b26 binary cleanup 2022-04-28 21:09:50 +02:00
c5e63a743a binary cleanup 2022-04-28 20:51:56 +02:00
28c691da5b cleanup 2022-04-28 20:35:33 +02:00
61e1e5d6fb make separate binary crate 2022-04-28 19:56:10 +02:00
48c10805b7
Update Cargo.toml 2021-09-04 17:15:42 +02:00
69832423b4
Merge pull request #4 from Kryptos-FR/buffered
Refactor using a buffer for reading and a buffer for writing
2021-09-04 17:07:16 +02:00
Nicolas Musset
9652a46435
On-stack dynamic dispatch to avoid boxing 2021-08-26 22:08:28 +09:00
Nicolas Musset
8832d617a3
No need to use crate utf8-chars, we can read/write at the byte level 2021-08-22 12:01:30 +09:00
Nicolas Musset
a78e6d3e4b
Format code with rustfmt 2021-08-22 11:53:55 +09:00
Nicolas Musset
f8c49ca661
Refactor using a buffer for reading and a buffer for writing 2021-08-19 13:40:10 +09:00
db2be0a36c
Update README.md 2021-08-18 22:23:08 +02:00
18fd8700c7 clap hotfix 2021-08-18 19:24:12 +02:00
426fb16d7e change the api 2021-08-18 19:16:16 +02:00
376a9c53bc ready for publishing 2021-08-18 19:01:57 +02:00
13 changed files with 668 additions and 407 deletions

6
.gitignore vendored
View file

@ -1,9 +1,3 @@
/target /target
.idea .idea
*.iml *.iml
# test data
*.json
# local install script
install.sh

3
.rustfmt.toml Normal file
View file

@ -0,0 +1,3 @@
imports_granularity = "Crate"
newline_style = "Unix"
group_imports = "StdExternalCrate"

11
CHANGELOG.md Normal file
View file

@ -0,0 +1,11 @@
# 2.1.0
- Strip `\r` from source (https://github.com/Noratrieb/jsonformat/pull/7)
- Various project cleanups
# 2.0.0
There are many changes, the two formatting functions have been renamed, `format_reader_writer` now takes
a `W` and `R` instead of `&mut BufReader<W>`, it now always adds a trailing newline. `Indentation::Default` was
renamed to `Indentation::TwoSpaces` and `FourSpaces` and `Tab` were added. There may be a few more
small changes.

528
Cargo.lock generated
View file

@ -1,5 +1,22 @@
# This file is automatically @generated by Cargo. # This file is automatically @generated by Cargo.
# It is not intended for manual editing. # It is not intended for manual editing.
version = 4
[[package]]
name = "aho-corasick"
version = "1.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
dependencies = [
"memchr",
]
[[package]]
name = "anyhow"
version = "1.0.97"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dcfed56ad506cb2c684a14971b8861fdc3baaaae314b9e5f9bb532cbe3ba7a4f"
[[package]] [[package]]
name = "atty" name = "atty"
version = "0.2.14" version = "0.2.14"
@ -13,42 +30,27 @@ dependencies = [
[[package]] [[package]]
name = "autocfg" name = "autocfg"
version = "1.0.1" version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a" checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
[[package]] [[package]]
name = "bitflags" name = "bitflags"
version = "1.2.1" version = "1.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
[[package]]
name = "bstr"
version = "0.2.16"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "90682c8d613ad3373e66de8c6411e0ae2ab2571e879d2efbf73558cc66f21279"
dependencies = [
"lazy_static",
"memchr",
"regex-automata",
"serde",
]
[[package]] [[package]]
name = "bumpalo" name = "bumpalo"
version = "3.7.0" version = "3.17.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c59e7af012c713f529e7a3ee57ce9b31ddd858d4b512923602f74608b009631" checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf"
[[package]] [[package]]
name = "cast" name = "cast"
version = "0.2.7" version = "0.3.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4c24dab4283a142afa2fdca129b80ad2c6284e073930f964c3a1293c225ee39a" checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
dependencies = [
"rustc_version",
]
[[package]] [[package]]
name = "cfg-if" name = "cfg-if"
@ -58,9 +60,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]] [[package]]
name = "clap" name = "clap"
version = "2.33.3" version = "2.34.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002" checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
dependencies = [ dependencies = [
"bitflags", "bitflags",
"textwrap 0.11.0", "textwrap 0.11.0",
@ -69,48 +71,55 @@ dependencies = [
[[package]] [[package]]
name = "clap" name = "clap"
version = "3.0.0-beta.2" version = "3.2.25"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4bd1061998a501ee7d4b6d449020df3266ca3124b941ec56cf2005c3779ca142" checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123"
dependencies = [ dependencies = [
"atty", "atty",
"bitflags", "bitflags",
"clap_derive", "clap_derive",
"clap_lex",
"indexmap", "indexmap",
"lazy_static", "once_cell",
"os_str_bytes",
"strsim", "strsim",
"termcolor", "termcolor",
"textwrap 0.12.1", "textwrap 0.16.2",
"unicode-width",
"vec_map",
] ]
[[package]] [[package]]
name = "clap_derive" name = "clap_derive"
version = "3.0.0-beta.2" version = "3.2.25"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "370f715b81112975b1b69db93e0b56ea4cd4e5002ac43b2da8474106a54096a1" checksum = "ae6371b8bdc8b7d3959e9cf7b22d4435ef3e79e138688421ec654acf8c81b008"
dependencies = [ dependencies = [
"heck", "heck",
"proc-macro-error", "proc-macro-error",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 1.0.109",
]
[[package]]
name = "clap_lex"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2850f2f5a82cbf437dd5af4d49848fbdfc27c157c3d010345776f952765261c5"
dependencies = [
"os_str_bytes",
] ]
[[package]] [[package]]
name = "criterion" name = "criterion"
version = "0.3.4" version = "0.3.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab327ed7354547cc2ef43cbe20ef68b988e70b4b593cbd66a2a61733123a3d23" checksum = "b01d6de93b2b6c65e17c634a26653a29d107b3c98c607c765bf38d041531cd8f"
dependencies = [ dependencies = [
"atty", "atty",
"cast", "cast",
"clap 2.33.3", "clap 2.34.0",
"criterion-plot", "criterion-plot",
"csv", "csv",
"itertools 0.10.1", "itertools",
"lazy_static", "lazy_static",
"num-traits", "num-traits",
"oorandom", "oorandom",
@ -127,65 +136,45 @@ dependencies = [
[[package]] [[package]]
name = "criterion-plot" name = "criterion-plot"
version = "0.4.3" version = "0.4.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e022feadec601fba1649cfa83586381a4ad31c6bf3a9ab7d408118b05dd9889d" checksum = "2673cc8207403546f45f5fd319a974b1e6983ad1a3ee7e6041650013be041876"
dependencies = [ dependencies = [
"cast", "cast",
"itertools 0.9.0", "itertools",
]
[[package]]
name = "crossbeam-channel"
version = "0.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06ed27e177f16d65f0f0c22a213e17c696ace5dd64b14258b52f9417ccb52db4"
dependencies = [
"cfg-if",
"crossbeam-utils",
] ]
[[package]] [[package]]
name = "crossbeam-deque" name = "crossbeam-deque"
version = "0.8.0" version = "0.8.6"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9" checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51"
dependencies = [ dependencies = [
"cfg-if",
"crossbeam-epoch", "crossbeam-epoch",
"crossbeam-utils", "crossbeam-utils",
] ]
[[package]] [[package]]
name = "crossbeam-epoch" name = "crossbeam-epoch"
version = "0.9.5" version = "0.9.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ec02e091aa634e2c3ada4a392989e7c3116673ef0ac5b72232439094d73b7fd" checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
dependencies = [ dependencies = [
"cfg-if",
"crossbeam-utils", "crossbeam-utils",
"lazy_static",
"memoffset",
"scopeguard",
] ]
[[package]] [[package]]
name = "crossbeam-utils" name = "crossbeam-utils"
version = "0.8.5" version = "0.8.21"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d82cfc11ce7f2c3faef78d8a684447b40d503d9681acebed6cb728d45940c4db" checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
dependencies = [
"cfg-if",
"lazy_static",
]
[[package]] [[package]]
name = "csv" name = "csv"
version = "1.1.6" version = "1.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22813a6dc45b335f9bade10bf7271dc477e81113e89eb251a0bc2a8a81c536e1" checksum = "acdc4883a9c96732e4733212c01447ebd805833b7275a73ca3ee080fd77afdaf"
dependencies = [ dependencies = [
"bstr",
"csv-core", "csv-core",
"itoa", "itoa",
"ryu", "ryu",
@ -194,39 +183,36 @@ dependencies = [
[[package]] [[package]]
name = "csv-core" name = "csv-core"
version = "0.1.10" version = "0.1.12"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90" checksum = "7d02f3b0da4c6504f86e9cd789d8dbafab48c2321be74e9987593de5a894d93d"
dependencies = [ dependencies = [
"memchr", "memchr",
] ]
[[package]] [[package]]
name = "either" name = "either"
version = "1.6.1" version = "1.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457" checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
[[package]] [[package]]
name = "half" name = "half"
version = "1.7.1" version = "1.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "62aca2aba2d62b4a7f5b33f3712cb1b0692779a56fb510499d5c0aa594daeaf3" checksum = "1b43ede17f21864e81be2fa654110bf1e793774238d86ef8555c37e6519c0403"
[[package]] [[package]]
name = "hashbrown" name = "hashbrown"
version = "0.11.2" version = "0.12.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e" checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
[[package]] [[package]]
name = "heck" name = "heck"
version = "0.3.3" version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
dependencies = [
"unicode-segmentation",
]
[[package]] [[package]]
name = "hermit-abi" name = "hermit-abi"
@ -239,9 +225,9 @@ dependencies = [
[[package]] [[package]]
name = "indexmap" name = "indexmap"
version = "1.7.0" version = "1.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc633605454125dec4b66843673f01c7df2b89479b32e0ed634e43a91cff62a5" checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99"
dependencies = [ dependencies = [
"autocfg", "autocfg",
"hashbrown", "hashbrown",
@ -249,117 +235,101 @@ dependencies = [
[[package]] [[package]]
name = "itertools" name = "itertools"
version = "0.9.0" version = "0.10.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b" checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
dependencies = [
"either",
]
[[package]]
name = "itertools"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf"
dependencies = [ dependencies = [
"either", "either",
] ]
[[package]] [[package]]
name = "itoa" name = "itoa"
version = "0.4.7" version = "1.0.15"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736" checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
[[package]] [[package]]
name = "js-sys" name = "js-sys"
version = "0.3.51" version = "0.3.77"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "83bdfbace3a0e81a4253f73b49e960b053e396a11012cbd49b9b74d6a2b67062" checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f"
dependencies = [ dependencies = [
"once_cell",
"wasm-bindgen", "wasm-bindgen",
] ]
[[package]] [[package]]
name = "jsonformat" name = "jsonformat"
version = "0.1.0" version = "2.1.0"
dependencies = [ dependencies = [
"clap 3.0.0-beta.2",
"criterion", "criterion",
] ]
[[package]]
name = "jsonformat-cli"
version = "1.0.0"
dependencies = [
"anyhow",
"clap 3.2.25",
"jsonformat",
]
[[package]] [[package]]
name = "lazy_static" name = "lazy_static"
version = "1.4.0" version = "1.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
[[package]] [[package]]
name = "libc" name = "libc"
version = "0.2.98" version = "0.2.171"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790" checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6"
[[package]] [[package]]
name = "log" name = "log"
version = "0.4.14" version = "0.4.26"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710" checksum = "30bde2b3dc3671ae49d8e2e9f044c7c005836e7a023ee57cffa25ab82764bb9e"
dependencies = [
"cfg-if",
]
[[package]] [[package]]
name = "memchr" name = "memchr"
version = "2.4.0" version = "2.7.4"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
[[package]]
name = "memoffset"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59accc507f1338036a0477ef61afdae33cde60840f4dfe481319ce3ad116ddf9"
dependencies = [
"autocfg",
]
[[package]] [[package]]
name = "num-traits" name = "num-traits"
version = "0.2.14" version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290" checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [ dependencies = [
"autocfg", "autocfg",
] ]
[[package]] [[package]]
name = "num_cpus" name = "once_cell"
version = "1.13.0" version = "1.21.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3" checksum = "d75b0bedcc4fe52caa0e03d9f1151a323e4aa5e2d78ba3580400cd3c9e2bc4bc"
dependencies = [
"hermit-abi",
"libc",
]
[[package]] [[package]]
name = "oorandom" name = "oorandom"
version = "11.1.3" version = "11.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575" checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e"
[[package]] [[package]]
name = "os_str_bytes" name = "os_str_bytes"
version = "2.4.0" version = "6.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "afb2e1c3ee07430c2cf76151675e583e0f19985fa6efae47d6848a3e2c824f85" checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1"
[[package]] [[package]]
name = "plotters" name = "plotters"
version = "0.3.1" version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a3fd9ec30b9749ce28cd91f255d569591cdf937fe280c312143e3c4bad6f2a" checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747"
dependencies = [ dependencies = [
"num-traits", "num-traits",
"plotters-backend", "plotters-backend",
@ -370,15 +340,15 @@ dependencies = [
[[package]] [[package]]
name = "plotters-backend" name = "plotters-backend"
version = "0.3.2" version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d88417318da0eaf0fdcdb51a0ee6c3bed624333bff8f946733049380be67ac1c" checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a"
[[package]] [[package]]
name = "plotters-svg" name = "plotters-svg"
version = "0.3.1" version = "0.3.7"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "521fa9638fa597e1dc53e9412a4f9cefb01187ee1f7413076f9e6749e2885ba9" checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670"
dependencies = [ dependencies = [
"plotters-backend", "plotters-backend",
] ]
@ -392,7 +362,7 @@ dependencies = [
"proc-macro-error-attr", "proc-macro-error-attr",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 1.0.109",
"version_check", "version_check",
] ]
@ -409,82 +379,82 @@ dependencies = [
[[package]] [[package]]
name = "proc-macro2" name = "proc-macro2"
version = "1.0.27" version = "1.0.94"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038" checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84"
dependencies = [ dependencies = [
"unicode-xid", "unicode-ident",
] ]
[[package]] [[package]]
name = "quote" name = "quote"
version = "1.0.9" version = "1.0.40"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7" checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
] ]
[[package]] [[package]]
name = "rayon" name = "rayon"
version = "1.5.1" version = "1.10.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c06aca804d41dbc8ba42dfd964f0d01334eceb64314b9ecf7c5fad5188a06d90" checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
dependencies = [ dependencies = [
"autocfg",
"crossbeam-deque",
"either", "either",
"rayon-core", "rayon-core",
] ]
[[package]] [[package]]
name = "rayon-core" name = "rayon-core"
version = "1.9.1" version = "1.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d78120e2c850279833f1dd3582f730c4ab53ed95aeaaaa862a2a5c71b1656d8e" checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
dependencies = [ dependencies = [
"crossbeam-channel",
"crossbeam-deque", "crossbeam-deque",
"crossbeam-utils", "crossbeam-utils",
"lazy_static",
"num_cpus",
] ]
[[package]] [[package]]
name = "regex" name = "regex"
version = "1.5.4" version = "1.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461" checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
dependencies = [ dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax", "regex-syntax",
] ]
[[package]] [[package]]
name = "regex-automata" name = "regex-automata"
version = "0.1.10" version = "0.4.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132" checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
[[package]]
name = "regex-syntax"
version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "rustc_version"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366"
dependencies = [ dependencies = [
"semver", "aho-corasick",
"memchr",
"regex-syntax",
] ]
[[package]] [[package]]
name = "ryu" name = "regex-syntax"
version = "1.0.5" version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e" checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
[[package]]
name = "rustversion"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2"
[[package]]
name = "ryu"
version = "1.0.20"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
[[package]] [[package]]
name = "same-file" name = "same-file"
@ -495,29 +465,20 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "semver"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f3aac57ee7f3272d8395c6e4f502f434f0e289fcd62876f70daa008c20dcabe"
[[package]] [[package]]
name = "serde" name = "serde"
version = "1.0.126" version = "1.0.219"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec7505abeacaec74ae4778d9d9328fe5a5d04253220a85c4ee022239fc996d03" checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6"
dependencies = [
"serde_derive",
]
[[package]] [[package]]
name = "serde_cbor" name = "serde_cbor"
version = "0.11.1" version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e18acfa2f90e8b735b2836ab8d538de304cbb6729a7360729ea5a895d15a622" checksum = "2bef2ebfde456fb76bbcf9f59315333decc4fda0b2b44b420243c11e0f5ec1f5"
dependencies = [ dependencies = [
"half", "half",
"serde", "serde",
@ -525,22 +486,23 @@ dependencies = [
[[package]] [[package]]
name = "serde_derive" name = "serde_derive"
version = "1.0.126" version = "1.0.219"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "963a7dbc9895aeac7ac90e74f34a5d5261828f79df35cbed41e10189d3804d43" checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.100",
] ]
[[package]] [[package]]
name = "serde_json" name = "serde_json"
version = "1.0.64" version = "1.0.140"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79" checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373"
dependencies = [ dependencies = [
"itoa", "itoa",
"memchr",
"ryu", "ryu",
"serde", "serde",
] ]
@ -553,20 +515,31 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]] [[package]]
name = "syn" name = "syn"
version = "1.0.73" version = "1.0.109"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f71489ff30030d2ae598524f61326b902466f72a0fb1a8564c001cc63425bcc7" checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"unicode-xid", "unicode-ident",
]
[[package]]
name = "syn"
version = "2.0.100"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b09a44accad81e1ba1cd74a32461ba89dee89095ba17b32f5d03683b1b1fc2a0"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
] ]
[[package]] [[package]]
name = "termcolor" name = "termcolor"
version = "1.1.2" version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4" checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755"
dependencies = [ dependencies = [
"winapi-util", "winapi-util",
] ]
@ -582,12 +555,9 @@ dependencies = [
[[package]] [[package]]
name = "textwrap" name = "textwrap"
version = "0.12.1" version = "0.16.2"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "203008d98caf094106cfaba70acfed15e18ed3ddb7d94e49baec153a2b462789" checksum = "c13547615a44dc9c452a8a534638acdf07120d4b6847c8178705da06306a3057"
dependencies = [
"unicode-width",
]
[[package]] [[package]]
name = "tinytemplate" name = "tinytemplate"
@ -600,76 +570,64 @@ dependencies = [
] ]
[[package]] [[package]]
name = "unicode-segmentation" name = "unicode-ident"
version = "1.8.0" version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b" checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
[[package]] [[package]]
name = "unicode-width" name = "unicode-width"
version = "0.1.8" version = "0.1.14"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3" checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af"
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "vec_map"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191"
[[package]] [[package]]
name = "version_check" name = "version_check"
version = "0.9.3" version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe" checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
[[package]] [[package]]
name = "walkdir" name = "walkdir"
version = "2.3.2" version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56" checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
dependencies = [ dependencies = [
"same-file", "same-file",
"winapi",
"winapi-util", "winapi-util",
] ]
[[package]] [[package]]
name = "wasm-bindgen" name = "wasm-bindgen"
version = "0.2.74" version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d54ee1d4ed486f78874278e63e4069fc1ab9f6a18ca492076ffb90c5eb2997fd" checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5"
dependencies = [ dependencies = [
"cfg-if", "cfg-if",
"once_cell",
"rustversion",
"wasm-bindgen-macro", "wasm-bindgen-macro",
] ]
[[package]] [[package]]
name = "wasm-bindgen-backend" name = "wasm-bindgen-backend"
version = "0.2.74" version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3b33f6a0694ccfea53d94db8b2ed1c3a8a4c86dd936b13b9f0a15ec4a451b900" checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6"
dependencies = [ dependencies = [
"bumpalo", "bumpalo",
"lazy_static",
"log", "log",
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.100",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
[[package]] [[package]]
name = "wasm-bindgen-macro" name = "wasm-bindgen-macro"
version = "0.2.74" version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "088169ca61430fe1e58b8096c24975251700e7b1f6fd91cc9d59b04fb9b18bd4" checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407"
dependencies = [ dependencies = [
"quote", "quote",
"wasm-bindgen-macro-support", "wasm-bindgen-macro-support",
@ -677,28 +635,31 @@ dependencies = [
[[package]] [[package]]
name = "wasm-bindgen-macro-support" name = "wasm-bindgen-macro-support"
version = "0.2.74" version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be2241542ff3d9f241f5e2cb6dd09b37efe786df8851c54957683a49f0987a97" checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
dependencies = [ dependencies = [
"proc-macro2", "proc-macro2",
"quote", "quote",
"syn", "syn 2.0.100",
"wasm-bindgen-backend", "wasm-bindgen-backend",
"wasm-bindgen-shared", "wasm-bindgen-shared",
] ]
[[package]] [[package]]
name = "wasm-bindgen-shared" name = "wasm-bindgen-shared"
version = "0.2.74" version = "0.2.100"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d7cff876b8f18eed75a66cf49b65e7f967cb354a7aa16003fb55dbfd25b44b4f" checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d"
dependencies = [
"unicode-ident",
]
[[package]] [[package]]
name = "web-sys" name = "web-sys"
version = "0.3.51" version = "0.3.77"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e828417b379f3df7111d3a2a9e5753706cae29c41f7c4029ee9fd77f3e09e582" checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2"
dependencies = [ dependencies = [
"js-sys", "js-sys",
"wasm-bindgen", "wasm-bindgen",
@ -722,11 +683,11 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]] [[package]]
name = "winapi-util" name = "winapi-util"
version = "0.1.5" version = "0.1.9"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178" checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
dependencies = [ dependencies = [
"winapi", "windows-sys",
] ]
[[package]] [[package]]
@ -734,3 +695,76 @@ name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0" version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f"
[[package]]
name = "windows-sys"
version = "0.59.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e38bc4d79ed67fd075bcc251a1c39b32a1776bbe92e5bef1f0bf1f8c531853b"
dependencies = [
"windows-targets",
]
[[package]]
name = "windows-targets"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9b724f72796e036ab90c1021d4780d4d3d648aca59e491e6b98e725b84e99973"
dependencies = [
"windows_aarch64_gnullvm",
"windows_aarch64_msvc",
"windows_i686_gnu",
"windows_i686_gnullvm",
"windows_i686_msvc",
"windows_x86_64_gnu",
"windows_x86_64_gnullvm",
"windows_x86_64_msvc",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a4622180e7a0ec044bb555404c800bc9fd9ec262ec147edd5989ccd0c02cd3"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "09ec2a7bb152e2252b53fa7803150007879548bc709c039df7627cabbd05d469"
[[package]]
name = "windows_i686_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8e9b5ad5ab802e97eb8e295ac6720e509ee4c243f69d781394014ebfe8bbfa0b"
[[package]]
name = "windows_i686_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0eee52d38c090b3caa76c563b86c3a4bd71ef1a819287c19d586d7334ae8ed66"
[[package]]
name = "windows_i686_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "240948bc05c5e7c6dabba28bf89d89ffce3e303022809e73deaefe4f6ec56c66"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "147a5c80aabfbf0c7d901cb5895d1de30ef2907eb21fbbab29ca94c5b08b1a78"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24d5b23dc417412679681396f2b49f3de8c1473deb516bd34410872eff51ed0d"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "589f6da84c646204747d1270a2a5661ea66ed1cced2631d546fdfb155959f9ec"

View file

@ -1,18 +1,22 @@
[workspace]
members = [".", "jsonformat-cli"]
[package] [package]
name = "jsonformat" name = "jsonformat"
version = "0.1.0" version = "2.1.0"
authors = ["Nilstrieb <nilstrieb@gmail.com>"] edition = "2024"
edition = "2018" license = "MIT"
description = "Reads raw json from stdin and formats it to stdout" description = "Formats JSON extremely fast"
homepage = "https://github.com/Noratrieb/jsonformat"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html repository = "https://github.com/Noratrieb/jsonformat"
readme = "README.md"
[dependencies] keywords = ["json", "formatting"]
clap = "3.0.0-beta.2" categories = ["parser-implementations"]
include = ["src/lib.rs", "Cargo.toml", "LICENSE", "README.md", "CHANGELOG.md", "benches/bench.rs"]
[dev-dependencies]
criterion = "0.3"
[[bench]] [[bench]]
name = "bench" name = "bench"
harness = false harness = false
[dev-dependencies]
criterion = "0.3.5"

View file

@ -2,40 +2,43 @@
`jsonformat` is an extremely fast JSON formatter. `jsonformat` is an extremely fast JSON formatter.
It formats over 60MB of nested JSON in under 0.4s. It formats over 20MB of nested JSON in 60ms.
## Install ## Library crate
Currently, you have to build and install it yourself.
`cargo build --release`
The executable can then be found in `target/release/jsonformat`
## Usage For the library crate, look at [docs.rs](https://docs.rs/jsonformat)
## Binary Install
You need Rust installed on your system
`cargo install jsonformat-cli`
## Binary Usage
``` ```
jsonformat-cli 0.2.0
Formats JSON extremely fast
USAGE: USAGE:
jsonformat [OPTIONS] [input] jsonformat [OPTIONS] [INPUT]
ARGS: ARGS:
<input> The input file to format <INPUT> The input file
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
OPTIONS: OPTIONS:
-i, --indent <indentation> Set the indentation used (\s for space, \t for tab) -h, --help Print help information
-o, --output <output> The output file for the formatted json -i, --indentation <INDENTATION> The indentation, s will replaced by a space and t by a tab.
ss is the default
-o, --output <OUTPUT> The output file
-V, --version Print version information
``` ```
Reads from stdin if no file is supplied. Reads from stdin if no file is supplied.
Outputs to stdout if no output file is specified. Outputs to stdout if no output file is specified.
On windows, it writes to a file called `<filename>_f.json`, unless the `--stdout` flag is used or a custom output file is provided. This it to enable drag-and-drop in windows explorer.
## Error handling ## Error handling
`jsonformat` does not report malformed json - it can't even fully know whether the json is actually malformed. Malformed json is just formatted kind of incorrectly, with no data lost and no crashes. If you find one, open an issue, `jsonformat` does not report malformed json - it can't even fully know whether the json is actually malformed.
Malformed json is just formatted kind of incorrectly, with no data lost and no crashes. If you find one, open an issue,
## How? ## How?
`jsonformat` does not actually parse the json, it just loops through each character and keeps track of some flags. It then copies these characters to the output buffer, adding and removing whitespace. `jsonformat` does not actually parse the json, it just loops through each character and keeps track of some flags.
It then copies these characters to the output buffer, adding and removing whitespace.
The code is currently a bit chaotic, but it works and is fast, so good enough for now. Maybe it could profit from SIMD in the future, but I have never used it and I don't know whether it would work. Maybe some day...

View file

@ -1,17 +1,42 @@
use criterion::{criterion_group, criterion_main, Criterion}; use std::{fs, path::PathBuf};
use jsonformat::format_json;
use std::{fs, io};
/// You need a json file called massive.json in your project root use criterion::{black_box, criterion_group, criterion_main, Criterion};
fn format_massive_json(file: &str) -> io::Result<String> { use jsonformat::{format, format_reader_writer, Indentation};
Ok(format_json(&file, None))
}
fn criterion_benchmark(c: &mut Criterion) { fn criterion_benchmark(c: &mut Criterion) {
let file = fs::read_to_string("massive.json").expect("massive.json file in project directory"); // using `include_str` makes the benches a lot less reliable for some reason???
let file = PathBuf::from(file!())
.parent()
.unwrap()
.join("large-file.json");
let file = fs::read_to_string(file).unwrap();
c.bench_function("Format massive json", |b| { c.bench_function("Format json default settings", |b| {
b.iter(|| format_massive_json(&file)) b.iter(|| {
let json = format(black_box(&file), Indentation::TwoSpace);
black_box(json);
})
});
c.bench_function("Format json custom indentation", |b| {
b.iter(|| {
let json = format(black_box(&file), Indentation::Custom("123456"));
black_box(json);
})
});
c.bench_function("Format json no utf8 validation", |b| {
b.iter(|| {
let mut writer = Vec::with_capacity(file.len() * 2);
format_reader_writer(
black_box(file.as_bytes()),
&mut writer,
Indentation::TwoSpace,
)
.unwrap();
black_box(writer);
})
}); });
} }

23
jsonformat-cli/Cargo.toml Normal file
View file

@ -0,0 +1,23 @@
[package]
name = "jsonformat-cli"
version = "1.0.0"
edition = "2024"
license = "MIT"
description = "Formats JSON extremely fast"
homepage = "https://github.com/Noratrieb/jsonformat"
repository = "https://github.com/Noratrieb/jsonformat"
readme = "README.md"
keywords = ["json", "formatting", "cli"]
categories = ["command-line-utilities"]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
jsonformat = { path = "..", version = "2.1.0" }
clap = { version = "3.1.12", features = ["derive"] }
anyhow = "1.0.57"
[[bin]]
name = "jsonformat"
path = "src/main.rs"

21
jsonformat-cli/LICENSE Normal file
View file

@ -0,0 +1,21 @@
MIT License
Copyright (c) 2021 nilstrieb
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.

44
jsonformat-cli/README.md Normal file
View file

@ -0,0 +1,44 @@
# Extremely fast JSON formatter
`jsonformat` is an extremely fast JSON formatter.
It formats over 20MB of nested JSON in 60ms.
## Library crate
For the library crate, look at [docs.rs](https://docs.rs/jsonformat)
## Binary Install
You need Rust installed on your system
`cargo install jsonformat-cli`
## Binary Usage
```
jsonformat-cli 0.2.0
Formats JSON extremely fast
USAGE:
jsonformat [OPTIONS] [INPUT]
ARGS:
<INPUT> The input file
OPTIONS:
-h, --help Print help information
-i, --indentation <INDENTATION> The indentation, s will replaced by a space and t by a tab.
ss is the default
-o, --output <OUTPUT> The output file
-V, --version Print version information
```
Reads from stdin if no file is supplied.
Outputs to stdout if no output file is specified.
## Error handling
`jsonformat` does not report malformed json - it can't even fully know whether the json is actually malformed.
Malformed json is just formatted kind of incorrectly, with no data lost and no crashes. If you find one, open an issue,
## How?
`jsonformat` does not actually parse the json, it just loops through each character and keeps track of some flags.
It then copies these characters to the output buffer, adding and removing whitespace.

View file

@ -0,0 +1,81 @@
use std::{
fs::File,
io,
io::{BufReader, BufWriter, Read, Write},
path::PathBuf,
};
use anyhow::Context;
use clap::Parser;
use jsonformat::{format_reader_writer, Indentation};
#[derive(Parser)]
#[clap(author, about, version)]
struct Options {
/// The indentation, s will replaced by a space and t by a tab. ss is the default.
#[clap(short, long)]
indentation: Option<String>,
#[clap(short, long)]
/// The output file
output: Option<PathBuf>,
/// The input file
input: Option<PathBuf>,
}
fn main() -> anyhow::Result<()> {
let options = Options::parse();
// Note: on-stack dynamic dispatch
// ugly af but works
let (mut file, stdin, mut stdin_lock);
let reader: &mut dyn Read = match &options.input {
Some(path) => {
file = File::open(path)
.context(format!("Name: {}", path.display()))
.context("Open input file")?;
&mut file
}
None => {
stdin = io::stdin();
stdin_lock = stdin.lock();
&mut stdin_lock
}
};
let replaced_indent = options.indentation.map(|value| {
value
.to_lowercase()
.chars()
.filter(|c| ['s', 't'].contains(c))
.collect::<String>()
.replace('s', " ")
.replace('t', "\t")
});
let indent = match replaced_indent {
Some(ref str) => Indentation::Custom(str),
None => Indentation::TwoSpace,
};
// Note: on-stack dynamic dispatch
let (mut file, stdout, mut stdout_lock);
let writer: &mut dyn Write = match &options.output {
Some(path) => {
file = File::create(path)
.context(path.display().to_string())
.context("Open output file")?;
&mut file
}
None => {
stdout = io::stdout();
stdout_lock = stdout.lock();
&mut stdout_lock
}
};
let mut reader = BufReader::new(reader);
let mut writer = BufWriter::new(writer);
format_reader_writer(&mut reader, &mut writer, indent).context("failed to read or write")?;
Ok(())
}

View file

@ -1,37 +1,84 @@
//! jsonformat is a library for formatting json.
//!
//! It does not do anything more than that, which makes it so fast.
use std::{
io,
io::{Read, Write},
};
/// Set the indentation used for the formatting.
/// ///
/// Note: It is *not* recommended to set indentation to anything oder than some spaces or some tabs,
/// but nothing is stopping you from doing that.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash)]
pub enum Indentation<'a> {
/// Fast path for two spaces
TwoSpace,
/// Fast path for four spaces
FourSpace,
/// Fast path for tab
Tab,
/// Use a custom indentation String
Custom(&'a str),
}
impl Default for Indentation<'_> {
fn default() -> Self {
Self::TwoSpace
}
}
/// # Formats a json string /// # Formats a json string
/// ///
/// The indentation can be set to any custom value /// The indentation can be set to any value using [`Indentation`]
/// The default value is two spaces /// The default value is two spaces
/// The default indentation is faster than a custom one /// The default indentation is faster than a custom one
/// pub fn format(json: &str, indentation: Indentation) -> String {
pub fn format_json(json: &str, indentation: Option<&str>) -> String { let mut reader = json.as_bytes();
// at least as big as the input to avoid resizing let mut writer = Vec::with_capacity(json.len());
// this might be too big if the input string is formatted in a weird way, but that's not expected
let mut out = String::with_capacity(json.len());
format_reader_writer(&mut reader, &mut writer, indentation).unwrap();
String::from_utf8(writer).unwrap()
}
/// # Formats a json string
///
/// The indentation can be set to any value using [`Indentation`]
/// The default value is two spaces
/// The default indentation is faster than a custom one
pub fn format_reader_writer<R, W>(
reader: R,
mut writer: W,
indentation: Indentation,
) -> io::Result<()>
where
R: Read,
W: Write,
{
let mut escaped = false; let mut escaped = false;
let mut in_string = false; let mut in_string = false;
let mut indent_level = 0usize; let mut indent_level = 0usize;
let mut newline_requested = false; // invalidated if next character is ] or } let mut newline_requested = false; // invalidated if next character is ] or }
for char in json.chars() { for char in reader.bytes() {
let char = char?;
if in_string { if in_string {
let mut escape_here = false; let mut escape_here = false;
match char { match char {
'"' => { b'"' => {
if !escaped { if !escaped {
in_string = false; in_string = false;
} }
} }
'\\' => { b'\\' => {
if !escaped { if !escaped {
escape_here = true; escape_here = true;
} }
} }
_ => {} _ => {}
} }
out.push(char); writer.write_all(&[char])?;
escaped = escape_here; escaped = escape_here;
} else { } else {
let mut auto_push = true; let mut auto_push = true;
@ -39,65 +86,78 @@ pub fn format_json(json: &str, indentation: Option<&str>) -> String {
let old_level = indent_level; let old_level = indent_level;
match char { match char {
'"' => in_string = true, b'"' => in_string = true,
' ' | '\n' | '\t' => continue, b' ' | b'\n' | b'\r' | b'\t' => continue,
'[' => { b'[' => {
indent_level += 1; indent_level += 1;
request_newline = true; request_newline = true;
} }
'{' => { b'{' => {
indent_level += 1; indent_level += 1;
request_newline = true; request_newline = true;
} }
'}' | ']' => { b'}' | b']' => {
indent_level = indent_level.saturating_sub(1); indent_level = indent_level.saturating_sub(1);
if !newline_requested { if !newline_requested {
// see comment below about newline_requested // see comment below about newline_requested
out.push('\n'); writer.write_all(b"\n")?;
indent(&mut out, indent_level, indentation); indent(&mut writer, indent_level, indentation)?;
} }
} }
':' => { b':' => {
auto_push = false; auto_push = false;
out.push(char); writer.write_all(&[char])?;
out.push(' '); writer.write_all(&[b' '])?;
} }
',' => { b',' => {
request_newline = true; request_newline = true;
} }
_ => {} _ => {}
} }
if newline_requested && char != ']' && char != '}' { if newline_requested && char != b']' && char != b'}' {
// newline only happens after { [ and , // newline only happens after { [ and ,
// this means we can safely assume that it being followed up by } or ] // this means we can safely assume that it being followed up by } or ]
// means an empty object/array // means an empty object/array
out.push('\n'); writer.write_all(b"\n")?;
indent(&mut out, old_level, indentation); indent(&mut writer, old_level, indentation)?;
} }
if auto_push { if auto_push {
out.push(char); writer.write_all(&[char])?;
} }
newline_requested = request_newline; newline_requested = request_newline;
} }
} }
out // trailing newline
writer.write_all(b"\n")?;
Ok(())
} }
fn indent(buf: &mut String, level: usize, indent_str: Option<&str>) { fn indent<W>(writer: &mut W, level: usize, indent_str: Indentation) -> io::Result<()>
where
W: Write,
{
for _ in 0..level { for _ in 0..level {
match indent_str { match indent_str {
None => { Indentation::TwoSpace => {
buf.push(' '); writer.write_all(b" ")?;
buf.push(' ');
} }
Some(indent) => { Indentation::FourSpace => {
buf.push_str(indent); writer.write_all(b" ")?;
}
Indentation::Tab => {
writer.write_all(b"\t")?;
}
Indentation::Custom(indent) => {
writer.write_all(indent.as_bytes())?;
} }
} }
} }
Ok(())
} }
#[cfg(test)] #[cfg(test)]
@ -106,28 +166,28 @@ mod test {
#[test] #[test]
fn echoes_primitive() { fn echoes_primitive() {
let json = "1.35"; let json = "1.35\n";
assert_eq!(json, format_json(json, None)); assert_eq!(json, format(json, Indentation::TwoSpace));
} }
#[test] #[test]
fn ignore_whitespace_in_string() { fn ignore_whitespace_in_string() {
let json = "\" hallo \""; let json = "\" hallo \"\n";
assert_eq!(json, format_json(json, None)); assert_eq!(json, format(json, Indentation::TwoSpace));
} }
#[test] #[test]
fn remove_leading_whitespace() { fn remove_leading_whitespace() {
let json = " 0"; let json = " 0";
let expected = "0"; let expected = "0\n";
assert_eq!(expected, format_json(json, None)); assert_eq!(expected, format(json, Indentation::TwoSpace));
} }
#[test] #[test]
fn handle_escaped_strings() { fn handle_escaped_strings() {
let json = " \" hallo \\\" \" "; let json = " \" hallo \\\" \" ";
let expected = "\" hallo \\\" \""; let expected = "\" hallo \\\" \"\n";
assert_eq!(expected, format_json(json, None)); assert_eq!(expected, format(json, Indentation::TwoSpace));
} }
#[test] #[test]
@ -135,8 +195,9 @@ mod test {
let json = "{\"a\":0}"; let json = "{\"a\":0}";
let expected = "{ let expected = "{
\"a\": 0 \"a\": 0
}"; }
assert_eq!(expected, format_json(json, None)); ";
assert_eq!(expected, format(json, Indentation::TwoSpace));
} }
#[test] #[test]
@ -146,8 +207,9 @@ mod test {
1, 1,
2, 2,
null null
]"; ]
assert_eq!(expected, format_json(json, None)); ";
assert_eq!(expected, format(json, Indentation::TwoSpace));
} }
#[test] #[test]
@ -161,9 +223,10 @@ mod test {
{ {
\"a\": null \"a\": null
} }
]"; ]
";
assert_eq!(expected, format_json(json, None)); assert_eq!(expected, format(json, Indentation::TwoSpace));
} }
#[test] #[test]
@ -176,8 +239,26 @@ mod test {
{ {
\"a\": null \"a\": null
} }
]"; ]
";
assert_eq!(expected, format_json(expected, None)); assert_eq!(expected, format(expected, Indentation::TwoSpace));
}
#[test]
fn contains_crlf() {
let json = "[\r\n{\r\n\"a\":0\r\n},\r\n{},\r\n{\r\n\"a\": null\r\n}\r\n]\r\n";
let expected = "[
{
\"a\": 0
},
{},
{
\"a\": null
}
]
";
assert_eq!(expected, format(json, Indentation::TwoSpace));
} }
} }

View file

@ -1,63 +0,0 @@
use clap::clap_app;
use jsonformat::format_json;
use std::fs;
use std::io;
use std::io::Read;
fn main() -> Result<(), io::Error> {
let matches = clap_app!(jsonformat =>
(version: "1.1")
(author: "nilstrieb <nilstrieb@gmail.com>")
(about: "Formats json from stdin or from a file")
(@arg stdout: -s --stdout "Output the result to stdout instead of the default output file. Windows only.")
(@arg indentation: -i --indent +takes_value "Set the indentation used (\\s for space, \\t for tab)")
(@arg output: -o --output +takes_value "The output file for the formatted json")
(@arg input: "The input file to format")
)
.get_matches();
let str = match matches.value_of("input") {
Some(path) => fs::read_to_string(path)?,
None => {
let mut buf = String::new();
let stdin = std::io::stdin();
stdin.lock().read_to_string(&mut buf)?;
buf
}
};
let replaced_indent = matches.value_of("indentation").map(|value| {
value
.to_lowercase()
.chars()
.filter(|c| ['s', 't'].contains(c))
.collect::<String>()
.replace("s", " ")
.replace("t", "\t")
});
let formatted = format_json(&str, replaced_indent.as_deref());
let mut output = matches.value_of("output");
let mut windows_output_default_file: Option<String> = None;
#[cfg(windows)]
if !matches.is_present("stdout") {
if let Some(file) = matches.value_of("input") {
// on windows, set the default output file if no stdout flag is provided
// this makes it work with drag and drop in windows explorer
windows_output_default_file = Some(file.replace(".json", "_f.json"))
}
}
output = windows_output_default_file.as_deref().or(output);
match output {
Some(file) => {
fs::write(file, formatted)?;
}
None => println!("{}", formatted),
}
Ok(())
}