sort-keys
Recursively sort the keys of a JSON object — for deterministic, stable output.
A faithful Rust port of the widely-used
sort-keys npm package, operating on
[serde_json::Value].
- Zero dependencies beyond
serde_json deep,ignore_keys, and a custom comparator- Differential-tested against the reference
sort-keysimplementation (60k cases)
Install
[]
= "0.1"
= "1"
Usage
use json;
use ;
assert_eq!;
// Recurse into nested objects and arrays:
assert_eq!;
// Custom comparator (reverse):
assert_eq!;
// Keep some keys unsorted (they come first, in original order):
let _ = sort_keys_with;
Notes
- The default comparator orders keys by UTF-16 code units, matching JavaScript's default
string sort. Use [
sort_keys_by] for any other order. - This crate sorts all keys, including numeric-looking ones, by the comparator. (The
JavaScript engine additionally reorders canonical integer keys first when serializing — a
runtime quirk that does not apply to Rust; enable
serde_json'spreserve_orderfeature to keep this crate's sorted order on output.)
Contributors ✨
This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
License
Licensed under either of MIT or Apache-2.0 at your option.