| 1 | use num_cmp::NumCmp; |
| 2 | use serde_json::{Map, Value}; |
| 3 | |
| 4 | macro_rules! num_cmp { |
| 5 | ($left:expr, $right:expr) => { |
| 6 | if let Some(b) = $right.as_u64() { |
| 7 | NumCmp::num_eq($left, b) |
| 8 | } else if let Some(b) = $right.as_i64() { |
| 9 | NumCmp::num_eq($left, b) |
| 10 | } else { |
| 11 | #[cfg(feature = "arbitrary-precision")] |
| 12 | { |
| 13 | use crate::ext::numeric::bignum; |
| 14 | use fraction::BigFraction; |
| 15 | |
| 16 | let left_frac = BigFraction::from($left); |
| 17 | |
| 18 | // Check BigInt/BigFraction BEFORE f64 to avoid precision loss |
| 19 | if let Some(right_bigint) = bignum::try_parse_bigint($right) { |
| 20 | let right_frac = BigFraction::from(right_bigint); |
| 21 | left_frac == right_frac |
| 22 | } else if let Some(right_frac) = bignum::try_parse_bigfraction($right) { |
| 23 | left_frac == right_frac |
| 24 | } else if let Some(b) = $right.as_f64() { |
| 25 | // Fallback to f64 for scientific notation or other cases |
| 26 | left_frac == BigFraction::from(b) |
| 27 | } else { |
| 28 | // Can't parse right - not equal |
| 29 | false |
| 30 | } |
| 31 | } |
| 32 | #[cfg(not(feature = "arbitrary-precision"))] |
| 33 | { |
| 34 | if let Some(b) = $right.as_f64() { |
| 35 | NumCmp::num_eq($left, b) |
| 36 | } else { |
| 37 | unreachable!("Numbers always fit in u64/i64/f64 without arbitrary-precision") |
| 38 | } |
| 39 | } |
| 40 | } |
| 41 | }; |
| 42 | } |
| 43 | |
| 44 | /// Compare two JSON numbers for equality with arbitrary precision support |
| 45 | #[inline] |
| 46 | pub(crate) fn equal_numbers(left: &serde_json::Number, right: &serde_json::Number) -> bool { |
| 47 | #[cfg(feature = "arbitrary-precision")] |
| 48 | { |
| 49 | use crate::ext::numeric::bignum; |
| 50 | use fraction::BigFraction; |
| 51 | |
| 52 | // Check BigInt/BigFraction first to avoid precision loss from f64 conversion |
| 53 | if let Some(left_bigint) = bignum::try_parse_bigint(left) { |
| 54 | if let Some(right_bigint) = bignum::try_parse_bigint(right) { |
| 55 | left_bigint == right_bigint |
| 56 | } else if let Some(b) = right.as_u64() { |
| 57 | left_bigint == num_bigint::BigInt::from(b) |
| 58 | } else if let Some(b) = right.as_i64() { |
| 59 | left_bigint == num_bigint::BigInt::from(b) |
| 60 | } else if let Some(right_frac) = bignum::try_parse_bigfraction(right) { |
| 61 | BigFraction::from(left_bigint) == right_frac |
| 62 | } else if let Some(b) = right.as_f64() { |
| 63 | BigFraction::from(left_bigint) == BigFraction::from(b) |
| 64 | } else { |
| 65 | unreachable!("Right is not parseable as any numeric type - should not happen for valid JSON numbers") |
| 66 | } |
| 67 | } else if let Some(left_frac) = bignum::try_parse_bigfraction(left) { |
| 68 | if let Some(right_frac) = bignum::try_parse_bigfraction(right) { |
| 69 | left_frac == right_frac |
| 70 | } else if let Some(right_bigint) = bignum::try_parse_bigint(right) { |
| 71 | left_frac == BigFraction::from(right_bigint) |
| 72 | } else if let Some(b) = right.as_u64() { |
| 73 | left_frac == BigFraction::from(b) |
| 74 | } else if let Some(b) = right.as_i64() { |
| 75 | left_frac == BigFraction::from(b) |
| 76 | } else if let Some(b) = right.as_f64() { |
| 77 | left_frac == BigFraction::from(b) |
| 78 | } else { |
| 79 | unreachable!("Right is not parseable as any numeric type - should not happen for valid JSON numbers") |
| 80 | } |
| 81 | } else if let Some(a) = left.as_u64() { |
| 82 | num_cmp!(a, right) |
| 83 | } else if let Some(a) = left.as_i64() { |
| 84 | num_cmp!(a, right) |
| 85 | } else if let Some(a) = left.as_f64() { |
| 86 | num_cmp!(a, right) |
| 87 | } else { |
| 88 | // Left is a number in scientific notation that doesn't fit in f64 |
| 89 | // (e.g., 1e309, 1e400). With arbitrary-precision, these are stored as |
| 90 | // strings but can't be converted to any numeric type we support. |
| 91 | // Return false as we can't reliably compare them. |
| 92 | false |
| 93 | } |
| 94 | } |
| 95 | #[cfg(not(feature = "arbitrary-precision"))] |
| 96 | { |
| 97 | if let Some(a) = left.as_u64() { |
| 98 | num_cmp!(a, right) |
| 99 | } else if let Some(a) = left.as_i64() { |
| 100 | num_cmp!(a, right) |
| 101 | } else if let Some(a) = left.as_f64() { |
| 102 | num_cmp!(a, right) |
| 103 | } else { |
| 104 | unreachable!("Numbers always fit in u64/i64/f64 without arbitrary-precision") |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | /// Tests for two JSON values to be equal using the JSON Schema semantic. |
| 110 | #[must_use] |
| 111 | #[allow(clippy::missing_panics_doc)] |
| 112 | pub fn equal(left: &Value, right: &Value) -> bool { |
| 113 | match (left, right) { |
| 114 | (Value::String(left), Value::String(right)) => left == right, |
| 115 | (Value::Bool(left), Value::Bool(right)) => left == right, |
| 116 | (Value::Null, Value::Null) => true, |
| 117 | (Value::Number(left), Value::Number(right)) => equal_numbers(left, right), |
| 118 | (Value::Array(left), Value::Array(right)) => equal_arrays(left, right), |
| 119 | (Value::Object(left), Value::Object(right)) => equal_objects(left, right), |
| 120 | (_, _) => false, |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | #[inline] |
| 125 | pub(crate) fn equal_arrays(left: &[Value], right: &[Value]) -> bool { |
| 126 | left.len() == right.len() && { |
| 127 | let mut idx = 0_usize; |
| 128 | while idx < left.len() { |
| 129 | if !equal(&left[idx], &right[idx]) { |
| 130 | return false; |
| 131 | } |
| 132 | idx += 1; |
| 133 | } |
| 134 | true |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | #[inline] |
| 139 | pub(crate) fn equal_objects(left: &Map<String, Value>, right: &Map<String, Value>) -> bool { |
| 140 | left.len() == right.len() |
| 141 | && left |
| 142 | .iter() |
| 143 | .zip(right) |
| 144 | .all(|((ka, va), (kb, vb))| ka == kb && equal(va, vb)) |
| 145 | } |
| 146 | |
| 147 | #[cfg(test)] |
| 148 | mod tests { |
| 149 | use super::equal; |
| 150 | use serde_json::{json, Value}; |
| 151 | use test_case::test_case; |
| 152 | |
| 153 | #[test_case(&json!(1), &json!(1.0))] |
| 154 | #[test_case(&json!([2]), &json!([2.0]))] |
| 155 | #[test_case(&json!([-3]), &json!([-3.0]))] |
| 156 | #[test_case(&json!({"a": 1}), &json!({"a": 1.0}))] |
| 157 | fn are_equal(left: &Value, right: &Value) { |
| 158 | assert!(equal(left, right)); |
| 159 | } |
| 160 | |
| 161 | #[test_case(&json!(1), &json!(2.0))] |
| 162 | #[test_case(&json!([]), &json!(["foo"]))] |
| 163 | #[test_case(&json!([-3]), &json!([-4.0]))] |
| 164 | #[test_case(&json!({"a": 1}), &json!({"a": 1.0, "b": 2}))] |
| 165 | fn are_not_equal(left: &Value, right: &Value) { |
| 166 | assert!(!equal(left, right)); |
| 167 | } |
| 168 | |
| 169 | #[cfg(feature = "arbitrary-precision")] |
| 170 | mod arbitrary_precision { |
| 171 | use super::equal; |
| 172 | use serde_json::Value; |
| 173 | use test_case::test_case; |
| 174 | |
| 175 | fn parse_json(s: &str) -> Value { |
| 176 | serde_json::from_str(s).unwrap() |
| 177 | } |
| 178 | #[test_case("0.1", "0.1", true; "exact decimal match")] |
| 179 | #[test_case("0.1", "0.10", true; "decimal with trailing zero")] |
| 180 | #[test_case("0.1", "0.100000", true; "decimal with many trailing zeros")] |
| 181 | #[test_case("0.1", "0.2", false; "different decimals")] |
| 182 | #[test_case("0.3", "0.30", true; "another trailing zero case")] |
| 183 | #[test_case("1.0", "1", true; "decimal vs integer")] |
| 184 | #[test_case("1.00", "1.0", true; "decimals with different trailing zeros")] |
| 185 | #[test_case( |
| 186 | "99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999.5", |
| 187 | "99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999.5", |
| 188 | true; |
| 189 | "huge decimal self equality" |
| 190 | )] |
| 191 | #[test_case( |
| 192 | "99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999.5", |
| 193 | "99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999.6", |
| 194 | false; |
| 195 | "huge decimals different" |
| 196 | )] |
| 197 | #[test_case( |
| 198 | "99999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999.5", |
| 199 | "100", |
| 200 | false; |
| 201 | "huge decimal vs small integer" |
| 202 | )] |
| 203 | #[test_case("18446744073709551616", "18446744073709551616", true; "large integer self equality")] |
| 204 | #[test_case("18446744073709551616", "18446744073709551617", false; "large integers different")] |
| 205 | #[test_case("99999999999999999999999999999999999999", "99999999999999999999999999999999999999", true; "very large integer equality")] |
| 206 | #[test_case("99999999999999999999999999999999999999", "100", false; "very large vs small integer")] |
| 207 | #[test_case("100", "100.0", true; "small integer vs decimal")] |
| 208 | #[test_case("0.1", "1", false; "small decimal vs integer")] |
| 209 | #[test_case("1.0", "1", true; "decimal one vs integer one")] |
| 210 | #[test_case("18446744073709551616", "100.5", false; "large int vs decimal")] |
| 211 | #[test_case("18446744073709551616.0", "18446744073709551616", true; "large int as decimal vs large int")] |
| 212 | #[test_case("-0.1", "-0.1", true; "negative decimal equality")] |
| 213 | #[test_case("-0.1", "-0.10", true; "negative decimal trailing zero")] |
| 214 | #[test_case("-18446744073709551616", "-18446744073709551616", true; "negative large int")] |
| 215 | #[test_case("-18446744073709551616", "18446744073709551616", false; "negative vs positive large")] |
| 216 | #[test_case("-100.5", "-100.5", true; "negative decimal match")] |
| 217 | #[test_case("-100.5", "100.5", false; "negative vs positive decimal")] |
| 218 | #[test_case("0", "0.0", true; "zero integer vs decimal")] |
| 219 | #[test_case("0.0", "0.00", true; "zero decimals with different precision")] |
| 220 | #[test_case("-0.0", "0.0", true; "negative zero vs positive zero")] |
| 221 | #[test_case("1e10", "10000000000", true; "scientific notation vs integer")] |
| 222 | #[test_case("1e19", "10000000000000000000", true; "scientific integer beyond i64")] |
| 223 | #[test_case("1e19", "10000000000000000001", false; "scientific integer mismatch")] |
| 224 | #[test_case("1.5e2", "150", true; "decimal scientific vs integer")] |
| 225 | #[test_case("1.5e2", "150.0", true; "decimal scientific vs decimal")] |
| 226 | #[test_case(r"[0.1, 0.2, 0.3]", r"[0.1, 0.2, 0.3]", true; "array exact match")] |
| 227 | #[test_case(r"[0.1, 0.2]", r"[0.10, 0.20]", true; "array with trailing zeros")] |
| 228 | #[test_case(r"[18446744073709551616]", r"[18446744073709551616]", true; "array with large integer")] |
| 229 | #[test_case(r"[0.1, 0.2]", r"[0.1, 0.3]", false; "array different values")] |
| 230 | #[test_case(r#"{"value": 0.1}"#, r#"{"value": 0.1}"#, true; "object exact match")] |
| 231 | #[test_case(r#"{"value": 0.1}"#, r#"{"value": 0.10}"#, true; "object with trailing zero")] |
| 232 | #[test_case(r#"{"id": 18446744073709551616}"#, r#"{"id": 18446744073709551616}"#, true; "object with large integer")] |
| 233 | #[test_case(r#"{"value": 0.1}"#, r#"{"value": 0.2}"#, false; "object different values")] |
| 234 | #[test_case("18446744073709551616", "-1", false; "large positive bigint vs negative i64")] |
| 235 | #[test_case("18446744073709551616", "-100", false; "large positive bigint vs negative i64 small")] |
| 236 | #[test_case("-18446744073709551616", "-1", false; "large negative bigint vs small negative i64")] |
| 237 | #[test_case("18446744073709551616", "1e10", false; "large bigint vs scientific notation f64")] |
| 238 | #[test_case("10000000000", "1e10", true; "bigint vs scientific notation equal")] |
| 239 | #[test_case("-18446744073709551616", "-1.5e3", false; "negative bigint vs scientific notation")] |
| 240 | #[test_case("0.5", "5e-1", true; "bigfraction vs scientific notation equal")] |
| 241 | #[test_case("0.3", "3e-1", true; "bigfraction vs scientific equal exact")] |
| 242 | #[test_case("123.456", "1.23456e2", true; "bigfraction vs scientific notation")] |
| 243 | #[test_case("0.1", "1e-2", false; "bigfraction vs scientific not equal")] |
| 244 | #[test_case("1e309", "1e309", true; "huge scientific notation now handled")] |
| 245 | #[test_case("1e400", "1e400", true; "extreme scientific notation now handled")] |
| 246 | #[test_case("1e-400", "1e-400", true; "extreme small scientific notation self equality")] |
| 247 | #[test_case("1e309", "1", false; "huge scientific notation vs integer")] |
| 248 | fn arbitrary_precision_equality(left_str: &str, right_str: &str, should_equal: bool) { |
| 249 | let left = parse_json(left_str); |
| 250 | let right = parse_json(right_str); |
| 251 | assert_eq!(equal(&left, &right), should_equal); |
| 252 | } |
| 253 | } |
| 254 | } |