mycelium_openapi/functions/
sort_json_keys.rs

1use serde_json::{Map, Value};
2use std::collections::BTreeMap;
3
4/// Sort the keys of a JSON object
5///
6/// This is used to make the JSON object deterministic.
7///
8pub fn sort_json_keys(value: Value) -> Value {
9    match value {
10        Value::Object(map) => {
11            let mut sorted_map = BTreeMap::new();
12            for (key, val) in map {
13                sorted_map.insert(key, sort_json_keys(val));
14            }
15            Value::Object(Map::from_iter(sorted_map))
16        }
17        Value::Array(arr) => {
18            Value::Array(arr.into_iter().map(sort_json_keys).collect())
19        }
20        _ => value,
21    }
22}