Expand description
§sort-keys — recursively sort the keys of a JSON object
Produce a copy of a serde_json::Value with object keys sorted — useful for
deterministic, stable output. A faithful Rust port of the widely-used
sort-keys npm package.
use serde_json::json;
use sort_keys::{sort_keys, sort_keys_with, Options};
assert_eq!(sort_keys(&json!({ "c": 0, "a": 0, "b": 0 })), json!({ "a": 0, "b": 0, "c": 0 }));
// Recurse into nested objects and arrays with `deep`:
assert_eq!(
sort_keys_with(&json!({ "b": { "d": 0, "c": 0 }, "a": 0 }), &Options::new().deep(true)),
json!({ "a": 0, "b": { "c": 0, "d": 0 } })
);By default keys are compared by UTF-16 code units (matching JavaScript’s default sort).
Use sort_keys_by for a custom comparator.
Structs§
- Options
- Options controlling
sort_keys_with/sort_keys_by.
Functions§
- compare_
utf16 - Compare two strings by their UTF-16 code units (JavaScript’s default string ordering).
- sort_
keys - Sort the keys of
value(shallow) using the default UTF-16 ordering. - sort_
keys_ by - Sort the keys of
valuewith the givenOptionsand a custom comparator. - sort_
keys_ with - Sort the keys of
valuewith the givenOptions, using the default UTF-16 ordering.