Compare commits

..

No commits in common. "master" and "v1.1" have entirely different histories.
master ... v1.1

13 changed files with 403 additions and 664 deletions

6
.gitignore vendored
View file

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

View file

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

View file

@ -1,11 +0,0 @@
# 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.

520
Cargo.lock generated
View file

@ -1,22 +1,5 @@
# This file is automatically @generated by Cargo.
# 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]]
name = "atty"
version = "0.2.14"
@ -30,27 +13,42 @@ dependencies = [
[[package]]
name = "autocfg"
version = "1.4.0"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ace50bade8e6234aa140d9a2f552bbee1db4d353f69b8217bc503490fc1a9f26"
checksum = "cdb031dd78e28731d87d56cc8ffef4a8f36ca26c38fe2de700543e627f8a464a"
[[package]]
name = "bitflags"
version = "1.3.2"
version = "1.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a"
checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693"
[[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]]
name = "bumpalo"
version = "3.17.0"
version = "3.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1628fb46dfa0b37568d12e5edd512553eccf6a22a78e8bde00bb4aed84d5bdbf"
checksum = "9c59e7af012c713f529e7a3ee57ce9b31ddd858d4b512923602f74608b009631"
[[package]]
name = "cast"
version = "0.3.0"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
checksum = "4c24dab4283a142afa2fdca129b80ad2c6284e073930f964c3a1293c225ee39a"
dependencies = [
"rustc_version",
]
[[package]]
name = "cfg-if"
@ -60,9 +58,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "2.34.0"
version = "2.33.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a0610544180c38b88101fecf2dd634b174a62eef6946f84dfc6a7127512b381c"
checksum = "37e58ac78573c40708d45522f0d80fa2f01cc4f9b4e2bf749807255454312002"
dependencies = [
"bitflags",
"textwrap 0.11.0",
@ -71,55 +69,48 @@ dependencies = [
[[package]]
name = "clap"
version = "3.2.25"
version = "3.0.0-beta.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ea181bf566f71cb9a5d17a59e1871af638180a18fb0035c92ae62b705207123"
checksum = "4bd1061998a501ee7d4b6d449020df3266ca3124b941ec56cf2005c3779ca142"
dependencies = [
"atty",
"bitflags",
"clap_derive",
"clap_lex",
"indexmap",
"once_cell",
"lazy_static",
"os_str_bytes",
"strsim",
"termcolor",
"textwrap 0.16.2",
"textwrap 0.12.1",
"unicode-width",
"vec_map",
]
[[package]]
name = "clap_derive"
version = "3.2.25"
version = "3.0.0-beta.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae6371b8bdc8b7d3959e9cf7b22d4435ef3e79e138688421ec654acf8c81b008"
checksum = "370f715b81112975b1b69db93e0b56ea4cd4e5002ac43b2da8474106a54096a1"
dependencies = [
"heck",
"proc-macro-error",
"proc-macro2",
"quote",
"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",
"syn",
]
[[package]]
name = "criterion"
version = "0.3.6"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b01d6de93b2b6c65e17c634a26653a29d107b3c98c607c765bf38d041531cd8f"
checksum = "ab327ed7354547cc2ef43cbe20ef68b988e70b4b593cbd66a2a61733123a3d23"
dependencies = [
"atty",
"cast",
"clap 2.34.0",
"clap 2.33.3",
"criterion-plot",
"csv",
"itertools",
"itertools 0.10.1",
"lazy_static",
"num-traits",
"oorandom",
@ -136,45 +127,65 @@ dependencies = [
[[package]]
name = "criterion-plot"
version = "0.4.5"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2673cc8207403546f45f5fd319a974b1e6983ad1a3ee7e6041650013be041876"
checksum = "e022feadec601fba1649cfa83586381a4ad31c6bf3a9ab7d408118b05dd9889d"
dependencies = [
"cast",
"itertools",
"itertools 0.9.0",
]
[[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]]
name = "crossbeam-deque"
version = "0.8.6"
version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9dd111b7b7f7d55b72c0a6ae361660ee5853c9af73f70c3c2ef6858b950e2e51"
checksum = "94af6efb46fef72616855b036a624cf27ba656ffc9be1b9a3c931cfc7749a9a9"
dependencies = [
"cfg-if",
"crossbeam-epoch",
"crossbeam-utils",
]
[[package]]
name = "crossbeam-epoch"
version = "0.9.18"
version = "0.9.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e"
checksum = "4ec02e091aa634e2c3ada4a392989e7c3116673ef0ac5b72232439094d73b7fd"
dependencies = [
"cfg-if",
"crossbeam-utils",
"lazy_static",
"memoffset",
"scopeguard",
]
[[package]]
name = "crossbeam-utils"
version = "0.8.21"
version = "0.8.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d0a5c400df2834b80a4c3327b3aad3a4c4cd4de0629063962b03235697506a28"
checksum = "d82cfc11ce7f2c3faef78d8a684447b40d503d9681acebed6cb728d45940c4db"
dependencies = [
"cfg-if",
"lazy_static",
]
[[package]]
name = "csv"
version = "1.3.1"
version = "1.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acdc4883a9c96732e4733212c01447ebd805833b7275a73ca3ee080fd77afdaf"
checksum = "22813a6dc45b335f9bade10bf7271dc477e81113e89eb251a0bc2a8a81c536e1"
dependencies = [
"bstr",
"csv-core",
"itoa",
"ryu",
@ -183,36 +194,39 @@ dependencies = [
[[package]]
name = "csv-core"
version = "0.1.12"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7d02f3b0da4c6504f86e9cd789d8dbafab48c2321be74e9987593de5a894d93d"
checksum = "2b2466559f260f48ad25fe6317b3c8dac77b5bdb5763ac7d9d6103530663bc90"
dependencies = [
"memchr",
]
[[package]]
name = "either"
version = "1.15.0"
version = "1.6.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "48c757948c5ede0e46177b7add2e67155f70e33c07fea8284df6576da70b3719"
checksum = "e78d4f1cc4ae33bbfc157ed5d5a5ef3bc29227303d595861deb238fcec4e9457"
[[package]]
name = "half"
version = "1.8.3"
version = "1.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1b43ede17f21864e81be2fa654110bf1e793774238d86ef8555c37e6519c0403"
checksum = "62aca2aba2d62b4a7f5b33f3712cb1b0692779a56fb510499d5c0aa594daeaf3"
[[package]]
name = "hashbrown"
version = "0.12.3"
version = "0.11.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a9ee70c43aaf417c914396645a0fa852624801b24ebb7ae78fe8272889ac888"
checksum = "ab5ef0d4909ef3724cc8cce6ccc8572c5c817592e9285f5464f8e86f8bd3726e"
[[package]]
name = "heck"
version = "0.4.1"
version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
checksum = "6d621efb26863f0e9924c6ac577e8275e5e6b77455db64ffa6c65c904e9e132c"
dependencies = [
"unicode-segmentation",
]
[[package]]
name = "hermit-abi"
@ -225,9 +239,9 @@ dependencies = [
[[package]]
name = "indexmap"
version = "1.9.3"
version = "1.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bd070e393353796e801d209ad339e89596eb4c8d430d18ede6a1cced8fafbd99"
checksum = "bc633605454125dec4b66843673f01c7df2b89479b32e0ed634e43a91cff62a5"
dependencies = [
"autocfg",
"hashbrown",
@ -235,101 +249,117 @@ dependencies = [
[[package]]
name = "itertools"
version = "0.10.5"
version = "0.9.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
checksum = "284f18f85651fe11e8a991b2adb42cb078325c996ed026d994719efcfca1d54b"
dependencies = [
"either",
]
[[package]]
name = "itertools"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69ddb889f9d0d08a67338271fa9b62996bc788c7796a5c18cf057420aaed5eaf"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.15"
version = "0.4.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
checksum = "dd25036021b0de88a0aff6b850051563c6516d0bf53f8638938edbb9de732736"
[[package]]
name = "js-sys"
version = "0.3.77"
version = "0.3.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cfaf33c695fc6e08064efbc1f72ec937429614f25eef83af942d0e227c3a28f"
checksum = "83bdfbace3a0e81a4253f73b49e960b053e396a11012cbd49b9b74d6a2b67062"
dependencies = [
"once_cell",
"wasm-bindgen",
]
[[package]]
name = "jsonformat"
version = "2.1.0"
version = "0.1.0"
dependencies = [
"clap 3.0.0-beta.2",
"criterion",
]
[[package]]
name = "jsonformat-cli"
version = "1.0.0"
dependencies = [
"anyhow",
"clap 3.2.25",
"jsonformat",
]
[[package]]
name = "lazy_static"
version = "1.5.0"
version = "1.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
[[package]]
name = "libc"
version = "0.2.171"
version = "0.2.98"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c19937216e9d3aa9956d9bb8dfc0b0c8beb6058fc4f7a4dc4d850edf86a237d6"
checksum = "320cfe77175da3a483efed4bc0adc1968ca050b098ce4f2f1c13a56626128790"
[[package]]
name = "log"
version = "0.4.26"
version = "0.4.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "30bde2b3dc3671ae49d8e2e9f044c7c005836e7a023ee57cffa25ab82764bb9e"
checksum = "51b9bbe6c47d51fc3e1a9b945965946b4c44142ab8792c50835a980d362c2710"
dependencies = [
"cfg-if",
]
[[package]]
name = "memchr"
version = "2.7.4"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3"
checksum = "b16bd47d9e329435e309c58469fe0791c2d0d1ba96ec0954152a5ae2b04387dc"
[[package]]
name = "num-traits"
version = "0.2.19"
name = "memoffset"
version = "0.6.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
checksum = "59accc507f1338036a0477ef61afdae33cde60840f4dfe481319ce3ad116ddf9"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.21.1"
name = "num-traits"
version = "0.2.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d75b0bedcc4fe52caa0e03d9f1151a323e4aa5e2d78ba3580400cd3c9e2bc4bc"
checksum = "9a64b1ec5cda2586e284722486d802acf1f7dbdc623e2bfc57e65ca1cd099290"
dependencies = [
"autocfg",
]
[[package]]
name = "num_cpus"
version = "1.13.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "05499f3756671c15885fee9034446956fff3f243d6077b91e5767df161f766b3"
dependencies = [
"hermit-abi",
"libc",
]
[[package]]
name = "oorandom"
version = "11.1.5"
version = "11.1.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d6790f58c7ff633d8771f42965289203411a5e5c68388703c06e14f24770b41e"
checksum = "0ab1bc2a289d34bd04a330323ac98a1b4bc82c9d9fcb1e66b63caa84da26b575"
[[package]]
name = "os_str_bytes"
version = "6.6.1"
version = "2.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2355d85b9a3786f481747ced0e0ff2ba35213a1f9bd406ed906554d7af805a1"
checksum = "afb2e1c3ee07430c2cf76151675e583e0f19985fa6efae47d6848a3e2c824f85"
[[package]]
name = "plotters"
version = "0.3.7"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5aeb6f403d7a4911efb1e33402027fc44f29b5bf6def3effcc22d7bb75f2b747"
checksum = "32a3fd9ec30b9749ce28cd91f255d569591cdf937fe280c312143e3c4bad6f2a"
dependencies = [
"num-traits",
"plotters-backend",
@ -340,15 +370,15 @@ dependencies = [
[[package]]
name = "plotters-backend"
version = "0.3.7"
version = "0.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "df42e13c12958a16b3f7f4386b9ab1f3e7933914ecea48da7139435263a4172a"
checksum = "d88417318da0eaf0fdcdb51a0ee6c3bed624333bff8f946733049380be67ac1c"
[[package]]
name = "plotters-svg"
version = "0.3.7"
version = "0.3.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51bae2ac328883f7acdfea3d66a7c35751187f870bc81f94563733a154d7a670"
checksum = "521fa9638fa597e1dc53e9412a4f9cefb01187ee1f7413076f9e6749e2885ba9"
dependencies = [
"plotters-backend",
]
@ -362,7 +392,7 @@ dependencies = [
"proc-macro-error-attr",
"proc-macro2",
"quote",
"syn 1.0.109",
"syn",
"version_check",
]
@ -379,82 +409,82 @@ dependencies = [
[[package]]
name = "proc-macro2"
version = "1.0.94"
version = "1.0.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a31971752e70b8b2686d7e46ec17fb38dad4051d94024c88df49b667caea9c84"
checksum = "f0d8caf72986c1a598726adc988bb5984792ef84f5ee5aa50209145ee8077038"
dependencies = [
"unicode-ident",
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.40"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1885c039570dc00dcb4ff087a89e185fd56bae234ddc7f056a945bf36467248d"
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "rayon"
version = "1.10.0"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b418a60154510ca1a002a752ca9714984e21e4241e804d32555251faf8b78ffa"
checksum = "c06aca804d41dbc8ba42dfd964f0d01334eceb64314b9ecf7c5fad5188a06d90"
dependencies = [
"autocfg",
"crossbeam-deque",
"either",
"rayon-core",
]
[[package]]
name = "rayon-core"
version = "1.12.1"
version = "1.9.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1465873a3dfdaa8ae7cb14b4383657caab0b3e8a0aa9ae8e04b044854c8dfce2"
checksum = "d78120e2c850279833f1dd3582f730c4ab53ed95aeaaaa862a2a5c71b1656d8e"
dependencies = [
"crossbeam-channel",
"crossbeam-deque",
"crossbeam-utils",
"lazy_static",
"num_cpus",
]
[[package]]
name = "regex"
version = "1.11.1"
version = "1.5.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b544ef1b4eac5dc2db33ea63606ae9ffcfac26c1416a2806ae0bf5f56b201191"
checksum = "d07a8629359eb56f1e2fb1652bb04212c072a87ba68546a04065d525673ac461"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.9"
version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "809e8dc61f6de73b46c85f4c96486310fe304c434cfa43669d7b40f711150908"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
checksum = "6c230d73fb8d8c1b9c0b3135c5142a8acee3a0558fb8db5cf1cb65f8d7862132"
[[package]]
name = "regex-syntax"
version = "0.8.5"
version = "0.6.25"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b15c43186be67a4fd63bee50d0303afffcef381492ebe2c5d87f324e1b8815c"
checksum = "f497285884f3fcff424ffc933e56d7cbca511def0c9831a7f9b5f6153e3cc89b"
[[package]]
name = "rustversion"
version = "1.0.20"
name = "rustc_version"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "eded382c5f5f786b989652c49544c4877d9f015cc22e145a5ea8ea66c2921cd2"
checksum = "bfa0f585226d2e68097d4f95d113b15b83a82e819ab25717ec0590d9584ef366"
dependencies = [
"semver",
]
[[package]]
name = "ryu"
version = "1.0.20"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "28d3b2b1366ec20994f1fd18c3c594f05c5dd4bc44d8bb0c1c632c8d6829481f"
checksum = "71d301d4193d031abdd79ff7e3dd721168a9572ef3fe51a1517aba235bd8f86e"
[[package]]
name = "same-file"
@ -466,19 +496,28 @@ dependencies = [
]
[[package]]
name = "serde"
version = "1.0.219"
name = "scopeguard"
version = "1.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6"
dependencies = [
"serde_derive",
]
checksum = "d29ab0c6d3fc0ee92fe66e2d99f700eab17a8d57d1c1d3b748380fb20baa78cd"
[[package]]
name = "semver"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f3aac57ee7f3272d8395c6e4f502f434f0e289fcd62876f70daa008c20dcabe"
[[package]]
name = "serde"
version = "1.0.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ec7505abeacaec74ae4778d9d9328fe5a5d04253220a85c4ee022239fc996d03"
[[package]]
name = "serde_cbor"
version = "0.11.2"
version = "0.11.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2bef2ebfde456fb76bbcf9f59315333decc4fda0b2b44b420243c11e0f5ec1f5"
checksum = "1e18acfa2f90e8b735b2836ab8d538de304cbb6729a7360729ea5a895d15a622"
dependencies = [
"half",
"serde",
@ -486,23 +525,22 @@ dependencies = [
[[package]]
name = "serde_derive"
version = "1.0.219"
version = "1.0.126"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00"
checksum = "963a7dbc9895aeac7ac90e74f34a5d5261828f79df35cbed41e10189d3804d43"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.100",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.140"
version = "1.0.64"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20068b6e96dc6c9bd23e01df8827e6c7e1f2fddd43c21810382803c136b99373"
checksum = "799e97dc9fdae36a5c8b8f2cae9ce2ee9fdce2058c57a93e6099d919fd982f79"
dependencies = [
"itoa",
"memchr",
"ryu",
"serde",
]
@ -515,31 +553,20 @@ checksum = "73473c0e59e6d5812c5dfe2a064a6444949f089e20eec9a2e5506596494e4623"
[[package]]
name = "syn"
version = "1.0.109"
version = "1.0.73"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237"
checksum = "f71489ff30030d2ae598524f61326b902466f72a0fb1a8564c001cc63425bcc7"
dependencies = [
"proc-macro2",
"quote",
"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",
"unicode-xid",
]
[[package]]
name = "termcolor"
version = "1.4.1"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755"
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
dependencies = [
"winapi-util",
]
@ -555,9 +582,12 @@ dependencies = [
[[package]]
name = "textwrap"
version = "0.16.2"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c13547615a44dc9c452a8a534638acdf07120d4b6847c8178705da06306a3057"
checksum = "203008d98caf094106cfaba70acfed15e18ed3ddb7d94e49baec153a2b462789"
dependencies = [
"unicode-width",
]
[[package]]
name = "tinytemplate"
@ -570,64 +600,76 @@ dependencies = [
]
[[package]]
name = "unicode-ident"
version = "1.0.18"
name = "unicode-segmentation"
version = "1.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5a5f39404a5da50712a4c1eecf25e90dd62b613502b7e925fd4e4d19b5c96512"
checksum = "8895849a949e7845e06bd6dc1aa51731a103c42707010a5b591c0038fb73385b"
[[package]]
name = "unicode-width"
version = "0.1.14"
version = "0.1.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af"
checksum = "9337591893a19b88d8d87f2cec1e73fad5cdfd10e5a6f349f498ad6ea2ffb1e3"
[[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]]
name = "version_check"
version = "0.9.5"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b928f33d975fc6ad9f86c8f283853ad26bdd5b10b7f1542aa2fa15e2289105a"
checksum = "5fecdca9a5291cc2b8dcf7dc02453fee791a280f3743cb0905f8822ae463b3fe"
[[package]]
name = "walkdir"
version = "2.5.0"
version = "2.3.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b"
checksum = "808cf2735cd4b6866113f648b791c6adc5714537bc222d9347bb203386ffda56"
dependencies = [
"same-file",
"winapi",
"winapi-util",
]
[[package]]
name = "wasm-bindgen"
version = "0.2.100"
version = "0.2.74"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1edc8929d7499fc4e8f0be2262a241556cfc54a0bea223790e71446f2aab1ef5"
checksum = "d54ee1d4ed486f78874278e63e4069fc1ab9f6a18ca492076ffb90c5eb2997fd"
dependencies = [
"cfg-if",
"once_cell",
"rustversion",
"wasm-bindgen-macro",
]
[[package]]
name = "wasm-bindgen-backend"
version = "0.2.100"
version = "0.2.74"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2f0a0651a5c2bc21487bde11ee802ccaf4c51935d0d3d42a6101f98161700bc6"
checksum = "3b33f6a0694ccfea53d94db8b2ed1c3a8a4c86dd936b13b9f0a15ec4a451b900"
dependencies = [
"bumpalo",
"lazy_static",
"log",
"proc-macro2",
"quote",
"syn 2.0.100",
"syn",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-macro"
version = "0.2.100"
version = "0.2.74"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7fe63fc6d09ed3792bd0897b314f53de8e16568c2b3f7982f468c0bf9bd0b407"
checksum = "088169ca61430fe1e58b8096c24975251700e7b1f6fd91cc9d59b04fb9b18bd4"
dependencies = [
"quote",
"wasm-bindgen-macro-support",
@ -635,31 +677,28 @@ dependencies = [
[[package]]
name = "wasm-bindgen-macro-support"
version = "0.2.100"
version = "0.2.74"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ae87ea40c9f689fc23f209965b6fb8a99ad69aeeb0231408be24920604395de"
checksum = "be2241542ff3d9f241f5e2cb6dd09b37efe786df8851c54957683a49f0987a97"
dependencies = [
"proc-macro2",
"quote",
"syn 2.0.100",
"syn",
"wasm-bindgen-backend",
"wasm-bindgen-shared",
]
[[package]]
name = "wasm-bindgen-shared"
version = "0.2.100"
version = "0.2.74"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a05d73b933a847d6cccdda8f838a22ff101ad9bf93e33684f39c1f5f0eece3d"
dependencies = [
"unicode-ident",
]
checksum = "d7cff876b8f18eed75a66cf49b65e7f967cb354a7aa16003fb55dbfd25b44b4f"
[[package]]
name = "web-sys"
version = "0.3.77"
version = "0.3.51"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33b6dd2ef9186f1f2072e409e99cd22a975331a6b3591b12c764e0e55c60d5d2"
checksum = "e828417b379f3df7111d3a2a9e5753706cae29c41f7c4029ee9fd77f3e09e582"
dependencies = [
"js-sys",
"wasm-bindgen",
@ -683,11 +722,11 @@ checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6"
[[package]]
name = "winapi-util"
version = "0.1.9"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf221c93e13a30d793f7645a0e7762c55d169dbb0a49671918a2319d289b10bb"
checksum = "70ec6ce85bb158151cae5e5c87f95a8e97d2c0c4b001223f33a334e3ce5de178"
dependencies = [
"windows-sys",
"winapi",
]
[[package]]
@ -695,76 +734,3 @@ name = "winapi-x86_64-pc-windows-gnu"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
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,22 +1,18 @@
[workspace]
members = [".", "jsonformat-cli"]
[package]
name = "jsonformat"
version = "2.1.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"]
categories = ["parser-implementations"]
include = ["src/lib.rs", "Cargo.toml", "LICENSE", "README.md", "CHANGELOG.md", "benches/bench.rs"]
version = "0.1.0"
authors = ["Nilstrieb <nilstrieb@gmail.com>"]
edition = "2018"
description = "Reads raw json from stdin and formats it to stdout"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
clap = "3.0.0-beta.2"
[dev-dependencies]
criterion = "0.3"
[[bench]]
name = "bench"
harness = false
[dev-dependencies]
criterion = "0.3.5"
harness = false

View file

@ -2,43 +2,40 @@
`jsonformat` is an extremely fast JSON formatter.
It formats over 20MB of nested JSON in 60ms.
It formats over 60MB of nested JSON in under 0.4s.
## Library crate
## Install
Currently, you have to build and install it yourself.
`cargo build --release`
The executable can then be found in `target/release/jsonformat`
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
## Usage
```
jsonformat-cli 0.2.0
Formats JSON extremely fast
USAGE:
jsonformat [OPTIONS] [INPUT]
jsonformat [OPTIONS] [input]
ARGS:
<INPUT> The input file
<input> The input file to format
FLAGS:
-h, --help Prints help information
-V, --version Prints version information
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
-i, --indent <indentation> Set the indentation used (\s for space, \t for tab)
-o, --output <output> The output file for the formatted json
```
Reads from stdin if no file is supplied.
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
`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?
`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,42 +1,17 @@
use std::{fs, path::PathBuf};
use criterion::{criterion_group, criterion_main, Criterion};
use jsonformat::format_json;
use std::{fs, io};
use criterion::{black_box, criterion_group, criterion_main, Criterion};
use jsonformat::{format, format_reader_writer, Indentation};
/// You need a json file called massive.json in your project root
fn format_massive_json(file: &str) -> io::Result<String> {
Ok(format_json(&file, None))
}
fn criterion_benchmark(c: &mut Criterion) {
// 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();
let file = fs::read_to_string("massive.json").expect("massive.json file in project directory");
c.bench_function("Format json default settings", |b| {
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);
})
c.bench_function("Format massive json", |b| {
b.iter(|| format_massive_json(&file))
});
}

View file

@ -1,23 +0,0 @@
[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"

View file

@ -1,21 +0,0 @@
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.

View file

@ -1,44 +0,0 @@
# 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

@ -1,81 +0,0 @@
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,84 +1,37 @@
//! 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
///
/// The indentation can be set to any value using [`Indentation`]
/// The indentation can be set to any custom value
/// The default value is two spaces
/// The default indentation is faster than a custom one
pub fn format(json: &str, indentation: Indentation) -> String {
let mut reader = json.as_bytes();
let mut writer = Vec::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,
{
pub fn format_json(json: &str, indentation: Option<&str>) -> String {
// at least as big as the input to avoid resizing
// 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());
let mut escaped = false;
let mut in_string = false;
let mut indent_level = 0usize;
let mut newline_requested = false; // invalidated if next character is ] or }
for char in reader.bytes() {
let char = char?;
for char in json.chars() {
if in_string {
let mut escape_here = false;
match char {
b'"' => {
'"' => {
if !escaped {
in_string = false;
}
}
b'\\' => {
'\\' => {
if !escaped {
escape_here = true;
}
}
_ => {}
}
writer.write_all(&[char])?;
out.push(char);
escaped = escape_here;
} else {
let mut auto_push = true;
@ -86,78 +39,65 @@ where
let old_level = indent_level;
match char {
b'"' => in_string = true,
b' ' | b'\n' | b'\r' | b'\t' => continue,
b'[' => {
'"' => in_string = true,
' ' | '\n' | '\t' => continue,
'[' => {
indent_level += 1;
request_newline = true;
}
b'{' => {
'{' => {
indent_level += 1;
request_newline = true;
}
b'}' | b']' => {
'}' | ']' => {
indent_level = indent_level.saturating_sub(1);
if !newline_requested {
// see comment below about newline_requested
writer.write_all(b"\n")?;
indent(&mut writer, indent_level, indentation)?;
out.push('\n');
indent(&mut out, indent_level, indentation);
}
}
b':' => {
':' => {
auto_push = false;
writer.write_all(&[char])?;
writer.write_all(&[b' '])?;
out.push(char);
out.push(' ');
}
b',' => {
',' => {
request_newline = true;
}
_ => {}
}
if newline_requested && char != b']' && char != b'}' {
if newline_requested && char != ']' && char != '}' {
// newline only happens after { [ and ,
// this means we can safely assume that it being followed up by } or ]
// means an empty object/array
writer.write_all(b"\n")?;
indent(&mut writer, old_level, indentation)?;
out.push('\n');
indent(&mut out, old_level, indentation);
}
if auto_push {
writer.write_all(&[char])?;
out.push(char);
}
newline_requested = request_newline;
}
}
// trailing newline
writer.write_all(b"\n")?;
Ok(())
out
}
fn indent<W>(writer: &mut W, level: usize, indent_str: Indentation) -> io::Result<()>
where
W: Write,
{
fn indent(buf: &mut String, level: usize, indent_str: Option<&str>) {
for _ in 0..level {
match indent_str {
Indentation::TwoSpace => {
writer.write_all(b" ")?;
None => {
buf.push(' ');
buf.push(' ');
}
Indentation::FourSpace => {
writer.write_all(b" ")?;
}
Indentation::Tab => {
writer.write_all(b"\t")?;
}
Indentation::Custom(indent) => {
writer.write_all(indent.as_bytes())?;
Some(indent) => {
buf.push_str(indent);
}
}
}
Ok(())
}
#[cfg(test)]
@ -166,28 +106,28 @@ mod test {
#[test]
fn echoes_primitive() {
let json = "1.35\n";
assert_eq!(json, format(json, Indentation::TwoSpace));
let json = "1.35";
assert_eq!(json, format_json(json, None));
}
#[test]
fn ignore_whitespace_in_string() {
let json = "\" hallo \"\n";
assert_eq!(json, format(json, Indentation::TwoSpace));
let json = "\" hallo \"";
assert_eq!(json, format_json(json, None));
}
#[test]
fn remove_leading_whitespace() {
let json = " 0";
let expected = "0\n";
assert_eq!(expected, format(json, Indentation::TwoSpace));
let expected = "0";
assert_eq!(expected, format_json(json, None));
}
#[test]
fn handle_escaped_strings() {
let json = " \" hallo \\\" \" ";
let expected = "\" hallo \\\" \"\n";
assert_eq!(expected, format(json, Indentation::TwoSpace));
let expected = "\" hallo \\\" \"";
assert_eq!(expected, format_json(json, None));
}
#[test]
@ -195,9 +135,8 @@ mod test {
let json = "{\"a\":0}";
let expected = "{
\"a\": 0
}
";
assert_eq!(expected, format(json, Indentation::TwoSpace));
}";
assert_eq!(expected, format_json(json, None));
}
#[test]
@ -207,9 +146,8 @@ mod test {
1,
2,
null
]
";
assert_eq!(expected, format(json, Indentation::TwoSpace));
]";
assert_eq!(expected, format_json(json, None));
}
#[test]
@ -223,10 +161,9 @@ mod test {
{
\"a\": null
}
]
";
]";
assert_eq!(expected, format(json, Indentation::TwoSpace));
assert_eq!(expected, format_json(json, None));
}
#[test]
@ -239,26 +176,8 @@ mod test {
{
\"a\": null
}
]
";
]";
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));
assert_eq!(expected, format_json(expected, None));
}
}

63
src/main.rs Normal file
View file

@ -0,0 +1,63 @@
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(())
}