Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions src/trim/canonical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<f64>() {
Expand Down Expand Up @@ -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
Expand Down
Loading