1
use serde_json::Value;
2

            
3
/// Compare two JSON values.
4
/// If the values are String, Number, or Bool, they are compared directly.
5
/// If the values are arrays, they are compared element-wise.
6
/// Otherwise, they are compared as strings.
7
65244
fn json_value_cmp(a: &Value, b: &Value) -> std::cmp::Ordering {
8
65244
    match (a, b) {
9
        (Value::Null, Value::Null) => std::cmp::Ordering::Equal,
10
        (Value::Bool(a), Value::Bool(b)) => a.cmp(b),
11
        (Value::String(a), Value::String(b)) => a.cmp(b),
12
        (Value::Number(a), Value::Number(b)) => {
13
            let af = a.as_f64().unwrap_or_default();
14
            let bf = b.as_f64().unwrap_or_default();
15
            af.total_cmp(&bf)
16
        }
17
1032
        (Value::Array(a), Value::Array(b)) => {
18
1032
            for (a, b) in a.iter().zip(b.iter()) {
19
1032
                let cmp = json_value_cmp(a, b);
20
1032
                if cmp != std::cmp::Ordering::Equal {
21
1032
                    return cmp;
22
                }
23
            }
24
            std::cmp::Ordering::Equal
25
        }
26
64212
        _ => a.to_string().cmp(&b.to_string()),
27
    }
28
65244
}
29

            
30
/// Sort the "variables" field by name.
31
/// We have to do this separately because that field is not a JSON object, instead it's an array of tuples.
32
468
pub fn sort_json_variables(value: &Value) -> Value {
33
468
    match value {
34
468
        Value::Array(vars) => {
35
468
            let mut vars_sorted = vars.clone();
36
468
            vars_sorted.sort_by(json_value_cmp);
37
468
            Value::Array(vars_sorted)
38
        }
39
        _ => value.clone(),
40
    }
41
468
}
42

            
43
/// Recursively sorts the keys of all JSON objects within the provided JSON value.
44
///
45
/// serde_json will output JSON objects in an arbitrary key order.
46
/// this is normally fine, except in our use case we wouldn't want to update the expected output again and again.
47
/// so a consistent (sorted) ordering of the keys is desirable.
48
121974
pub fn sort_json_object(value: &Value, sort_arrays: bool) -> Value {
49
121974
    match value {
50
30582
        Value::Object(obj) => {
51
30582
            let mut ordered: Vec<(String, Value)> = obj
52
30582
                .iter()
53
94278
                .map(|(k, v)| {
54
94278
                    if k == "variables" {
55
468
                        (k.clone(), sort_json_variables(v))
56
                    } else {
57
93810
                        (k.clone(), sort_json_object(v, sort_arrays))
58
                    }
59
94278
                })
60
30582
                .collect();
61
30582

            
62
63696
            ordered.sort_by(|a, b| a.0.cmp(&b.0));
63
30582
            Value::Object(ordered.into_iter().collect())
64
        }
65
5844
        Value::Array(arr) => {
66
5844
            let mut arr: Vec<Value> = arr
67
5844
                .iter()
68
26946
                .map(|val| sort_json_object(val, sort_arrays))
69
5844
                .collect();
70
5844

            
71
5844
            if sort_arrays {
72
468
                arr.sort_by(json_value_cmp);
73
5376
            }
74

            
75
5844
            Value::Array(arr)
76
        }
77
85548
        _ => value.clone(),
78
    }
79
121974
}