Skip to main content

Crate sort_keys

Crate sort_keys 

Source
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 value with the given Options and a custom comparator.
sort_keys_with
Sort the keys of value with the given Options, using the default UTF-16 ordering.