1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
use crate::utils::{DedupeHashValue, HashValue};
use serde_json::Value::{self, *};

#[cfg(test)]
mod tests;
mod utils;

/// Remove `Null` value fields from serde_json::Value
/// ## Example
/// ```rust
/// use serde_json::{Value, from_str};
/// use serde_json_utils::skip_null;
///
/// let mut x: Value = from_str(r###"{"key1": null, "key2": "foo"}"###).unwrap();
/// let x_result: Value = from_str(r###"{"key2": "foo"}"###).unwrap();
///
/// skip_null(&mut x);
/// assert_eq!(x, x_result);
/// ```
pub fn skip_null(val: &mut Value) {
    remove_nulls(val, false);
}

/// Remove `Null` value fields & `empty` value fields from serde_json::Value
/// ## Example
/// ```rust
/// use serde_json::{Value, from_str};
/// use serde_json_utils::skip_null_and_empty;
///
/// let mut x: Value = from_str(r###"{"key1": null, "key2": "foo", "key3": [], "key4": {}}"###).unwrap();
/// let x_result: Value = from_str(r###"{"key2": "foo"}"###).unwrap();
///
/// skip_null_and_empty(&mut x);
/// assert_eq!(x, x_result);
/// ```
pub fn skip_null_and_empty(val: &mut Value) {
    remove_nulls(val, true);
}

/// `Dedup` array of json's from serde_json::Value
/// ## Example
/// ```rust
/// use serde_json::{Value, from_str};
/// use serde_json_utils::dedup;
///
/// let mut x: Value = from_str(r###"[{"key1": "foo", "key2": "bar", "key3": [1, 1, 2]}, {"key1": "foo", "key2": "bar", "key3": [1, 1, 2]}]"###).unwrap();
/// let x_result: Value = from_str(r###"[{"key1": "foo", "key2": "bar", "key3": [1, 2]}]"###).unwrap();
///
/// dedup(&mut x);
/// assert_eq!(x, x_result);
/// ```
pub fn dedup(val: &mut Value) {
    match val {
        Null => {}
        Bool(_) => {}
        Number(_) => {}
        String(_) => {}
        Array(a) => {
            let mut aa = a.clone();
            for v in &mut aa {
                dedup(v);
            }
            let mut set = std::collections::HashSet::new();
            let mut candidates = vec![];
            for v in &aa {
                if !set.contains(&DedupeHashValue(v)) {
                    set.insert(DedupeHashValue(v));
                    candidates.push(v.clone());
                }
            }
            a.clear();
            a.extend(candidates);
        }
        Object(o) => {
            for (_, v) in o.iter_mut() {
                dedup(v);
            }
        }
    }
}

pub fn merge_similar(p: &Value, v: &Value) -> Value {
    match (p, v) {
        (Object(a), Object(b)) => {
            if HashValue(p) != HashValue(v) {
                return Array(vec![p.clone(), v.clone()]);
            }
            let mut res = serde_json::Map::new();
            for (k, v) in a {
                let bv = b.get(k).unwrap();
                if let (Array(_arr1), Array(_arr2)) = (v, bv) {
                    if v.eq(bv) {
                        res.insert(k.clone(), v.clone());
                    } else {
                        res.insert(k.clone(), Array(vec![v.clone(), bv.clone()]));
                    }
                } else if let (Array(arr1), _) = (v, bv) {
                    let mut aaa = arr1.clone();
                    if !aaa.contains(bv) {
                        aaa.push(bv.clone());
                    }
                    res.insert(k.clone(), Array(aaa));
                } else if v.eq(bv) {
                        res.insert(k.clone(), v.clone());
                } else {
                    res.insert(k.clone(), Array(vec![v.clone(), bv.clone()]));
                }
            }
            Object(res)
        }
        _ => Array(vec![p.clone(), v.clone()]),
    }
}

/// Remove `Null` value fields & `empty` value fields from serde_json::Value
fn remove_nulls(val: &mut Value, with_empties: bool) -> bool {
    match val {
        Null => {
            return true;
        }
        Bool(_) => {}
        Number(_) => {}
        String(_) => {}
        Array(arr) => {
            if with_empties && arr.is_empty() {
                return true;
            }
            let mut candidates = vec![];
            for v in &mut arr.clone() {
                if !remove_nulls(v, with_empties) {
                    candidates.push(v.clone());
                }
            }
            arr.clear();
            arr.extend(candidates);
        }
        Object(obj) => {
            if with_empties && obj.is_empty() {
                return true;
            }
            let mut candidates = vec![];
            for (k, v) in obj.iter_mut() {
                if remove_nulls(v, with_empties) {
                    candidates.push(k.clone());
                }
            }
            for c in candidates {
                obj.remove(&c);
            }
        }
    }
    false
}