Skip to content
This repository has been archived by the owner on Sep 24, 2022. It is now read-only.

Commit

Permalink
Fix serialization of -0.0. (#426)
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss authored Apr 26, 2021
1 parent b141700 commit 1928707
Showing 1 changed file with 14 additions and 10 deletions.
24 changes: 14 additions & 10 deletions src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,17 +751,21 @@ impl<'a> Serializer<'a> {
macro_rules! serialize_float {
($this:expr, $v:expr) => {{
$this.emit_key(ArrayState::Started)?;
if ($v.is_nan() || $v == 0.0) && $v.is_sign_negative() {
write!($this.dst, "-").map_err(ser::Error::custom)?;
}
if $v.is_nan() {
write!($this.dst, "nan").map_err(ser::Error::custom)?;
} else {
write!($this.dst, "{}", $v).map_err(ser::Error::custom)?;
}
if $v % 1.0 == 0.0 {
write!($this.dst, ".0").map_err(ser::Error::custom)?;
match ($v.is_sign_negative(), $v.is_nan(), $v == 0.0) {
(true, true, _) => write!($this.dst, "-nan"),
(false, true, _) => write!($this.dst, "nan"),
(true, false, true) => write!($this.dst, "-0.0"),
(false, false, true) => write!($this.dst, "0.0"),
(_, false, false) => write!($this.dst, "{}", $v).and_then(|_| {
if $v % 1.0 == 0.0 {
write!($this.dst, ".0")
} else {
Ok(())
}
}),
}
.map_err(ser::Error::custom)?;

if let State::Table { .. } = $this.state {
$this.dst.push_str("\n");
}
Expand Down

0 comments on commit 1928707

Please sign in to comment.