From de251c8198995b8bd8b770bc2bb6c9be5a7e6b53 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 21 Aug 2022 13:37:56 -0700 Subject: [PATCH 1/3] Add test of Display for Number containing float --- tests/debug.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/debug.rs b/tests/debug.rs index f99a0d921..432a1378b 100644 --- a/tests/debug.rs +++ b/tests/debug.rs @@ -27,6 +27,7 @@ fn value_number() { assert_eq!(format!("{:?}", json!(1)), "Number(1)"); assert_eq!(format!("{:?}", json!(-1)), "Number(-1)"); assert_eq!(format!("{:?}", json!(1.0)), "Number(1.0)"); + assert_eq!(Number::from_f64(1.0).unwrap().to_string(), "1"); } #[test] From 8ba854166d12e1813aaf09501aa0dd87f3250aed Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 21 Aug 2022 13:38:31 -0700 Subject: [PATCH 2/3] Preserve '.0' when Displaying Number --- src/number.rs | 12 ++---------- tests/debug.rs | 2 +- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/src/number.rs b/src/number.rs index 4ee052690..9c1acd1a8 100644 --- a/src/number.rs +++ b/src/number.rs @@ -294,7 +294,8 @@ impl Display for Number { match self.n { N::PosInt(u) => Display::fmt(&u, formatter), N::NegInt(i) => Display::fmt(&i, formatter), - N::Float(f) => Display::fmt(&f, formatter), + // Preserve `.0` on integral values, which Display hides + N::Float(f) => Debug::fmt(&f, formatter), } } @@ -305,15 +306,6 @@ impl Display for Number { } impl Debug for Number { - #[cfg(not(feature = "arbitrary_precision"))] - fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { - match self.n { - N::PosInt(_) | N::NegInt(_) => write!(formatter, "Number({})", self), - N::Float(f) => write!(formatter, "Number({:?})", f), - } - } - - #[cfg(feature = "arbitrary_precision")] fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { write!(formatter, "Number({})", self) } diff --git a/tests/debug.rs b/tests/debug.rs index 432a1378b..0535bfdd1 100644 --- a/tests/debug.rs +++ b/tests/debug.rs @@ -27,7 +27,7 @@ fn value_number() { assert_eq!(format!("{:?}", json!(1)), "Number(1)"); assert_eq!(format!("{:?}", json!(-1)), "Number(-1)"); assert_eq!(format!("{:?}", json!(1.0)), "Number(1.0)"); - assert_eq!(Number::from_f64(1.0).unwrap().to_string(), "1"); + assert_eq!(Number::from_f64(1.0).unwrap().to_string(), "1.0"); // not just "1" } #[test] From cb2515ba445f724c2059dc68e57e9795d4866be7 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Sun, 21 Aug 2022 13:54:24 -0700 Subject: [PATCH 3/3] Make Display for Number produce the same representation as serializing --- src/number.rs | 7 +++---- tests/debug.rs | 1 + 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/number.rs b/src/number.rs index 9c1acd1a8..df8819259 100644 --- a/src/number.rs +++ b/src/number.rs @@ -292,10 +292,9 @@ impl Display for Number { #[cfg(not(feature = "arbitrary_precision"))] fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result { match self.n { - N::PosInt(u) => Display::fmt(&u, formatter), - N::NegInt(i) => Display::fmt(&i, formatter), - // Preserve `.0` on integral values, which Display hides - N::Float(f) => Debug::fmt(&f, formatter), + N::PosInt(u) => formatter.write_str(itoa::Buffer::new().format(u)), + N::NegInt(i) => formatter.write_str(itoa::Buffer::new().format(i)), + N::Float(f) => formatter.write_str(ryu::Buffer::new().format_finite(f)), } } diff --git a/tests/debug.rs b/tests/debug.rs index 0535bfdd1..8ddcf5a38 100644 --- a/tests/debug.rs +++ b/tests/debug.rs @@ -28,6 +28,7 @@ fn value_number() { assert_eq!(format!("{:?}", json!(-1)), "Number(-1)"); assert_eq!(format!("{:?}", json!(1.0)), "Number(1.0)"); assert_eq!(Number::from_f64(1.0).unwrap().to_string(), "1.0"); // not just "1" + assert_eq!(Number::from_f64(12e40).unwrap().to_string(), "1.2e41"); } #[test]