Compare two `Option<&serde_json::Value>` for sort. Nulls / absent keys sort last; numbers compare numerically; everything else falls back to string comparison.
(
a: Option<&serde_json::Value>,
b: Option<&serde_json::Value>,
)
| 137 | /// keys sort last; numbers compare numerically; everything else falls |
| 138 | /// back to string comparison. |
| 139 | fn compare_json_values( |
| 140 | a: Option<&serde_json::Value>, |
| 141 | b: Option<&serde_json::Value>, |
| 142 | ) -> std::cmp::Ordering { |
| 143 | use serde_json::Value as V; |
| 144 | use std::cmp::Ordering; |
| 145 | let a_is_null = matches!(a, None | Some(V::Null)); |
| 146 | let b_is_null = matches!(b, None | Some(V::Null)); |
| 147 | if a_is_null && b_is_null { |
| 148 | return Ordering::Equal; |
| 149 | } |
| 150 | if a_is_null { |
| 151 | return Ordering::Greater; |
| 152 | } |
| 153 | if b_is_null { |
| 154 | return Ordering::Less; |
| 155 | } |
| 156 | match (a.unwrap(), b.unwrap()) { |
| 157 | (V::Number(x), V::Number(y)) => { |
| 158 | let xf = x.as_f64().unwrap_or(0.0); |
| 159 | let yf = y.as_f64().unwrap_or(0.0); |
| 160 | xf.partial_cmp(&yf).unwrap_or(Ordering::Equal) |
| 161 | } |
| 162 | (V::String(x), V::String(y)) => x.cmp(y), |
| 163 | (V::Bool(x), V::Bool(y)) => x.cmp(y), |
| 164 | (x, y) => x.to_string().cmp(&y.to_string()), |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // ── CoreLoop impl ────────────────────────────────────────────────────────── |
| 169 |
no test coverage detected