diff --git a/Cargo.lock b/Cargo.lock index 099d180..62492ac 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -271,9 +271,9 @@ dependencies = [ [[package]] name = "serde_json" -version = "1.0.151" +version = "1.0.154" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c841b55ecdae098c80dcae9cf767f6f8a0c2cdb3416bbef72181df4d0fe73f14" +checksum = "e7e9cc8b1b85264074fbcc02a88680c4096b1e47df8f739dceb03bf482f04bd6" dependencies = [ "itoa", "memchr", diff --git a/src/trim/canonical.rs b/src/trim/canonical.rs index 63a9460..342c506 100644 --- a/src/trim/canonical.rs +++ b/src/trim/canonical.rs @@ -28,6 +28,13 @@ pub fn normalise_numbers(value: &Value) -> Value { let literal = number.as_str(); let is_float = literal.contains('.') || literal.contains('e') || literal.contains('E'); if !is_float { + // serde_json 1.0.152 keeps `-0` as written under + // arbitrary_precision, where 1.0.151 turned it into `0`. + // Python reads `-0` as the int 0 and writes `0`, so match it. + // Measured 2026-10-20: 87 `-0` literals in a 2.0.77 dump. + if literal == "-0" { + return Value::Number(0.into()); + } return value.clone(); } match literal.parse::() { @@ -87,6 +94,14 @@ mod tests { assert_eq!(text, "[\n 0.3945,\n 0.49610000000000004\n]\n"); } + #[test] + fn negative_zero_integer_prints_as_python_does() { + // Python reads `-0` as the int 0. `-0.0` stays a float and keeps its sign. + let value: Value = serde_json::from_str("[-0, -0.0, 0]").unwrap(); + let text = to_canonical_json(&normalise_numbers(&value)); + assert_eq!(text, "[\n 0,\n -0.0,\n 0\n]\n"); + } + #[test] fn an_integer_stays_an_integer() { // JSON does not distinguish them but both Python and this tool do, and