From 0e226317c283b20f401328fc049f5edd41fc443d Mon Sep 17 00:00:00 2001 From: squaaawk <81951801+xorshift@users.noreply.github.com> Date: Mon, 17 Jun 2024 19:09:42 -0700 Subject: [PATCH] fix ron & json serialization of large floats --- src/serde_json.rs | 2 +- src/serde_ron.rs | 2 +- tests/ser_de.rs | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/serde_json.rs b/src/serde_json.rs index 47ceef0..4c42ec3 100644 --- a/src/serde_json.rs +++ b/src/serde_json.rs @@ -715,7 +715,7 @@ macro_rules! impl_ser_de_json_float { ( $ ty: ident) => { impl SerJson for $ty { fn ser_json(&self, _d: usize, s: &mut SerJsonState) { - s.out.push_str(&self.to_string()); + s.out.push_str(&format!("{self:?}")); } } diff --git a/src/serde_ron.rs b/src/serde_ron.rs index ce8b954..71077e7 100644 --- a/src/serde_ron.rs +++ b/src/serde_ron.rs @@ -726,7 +726,7 @@ macro_rules! impl_ser_de_ron_float { ( $ ty: ident) => { impl SerRon for $ty { fn ser_ron(&self, _d: usize, s: &mut SerRonState) { - s.out.push_str(&self.to_string()); + s.out.push_str(&format!("{self:?}")); } } diff --git a/tests/ser_de.rs b/tests/ser_de.rs index 0509b0c..48bf3e3 100644 --- a/tests/ser_de.rs +++ b/tests/ser_de.rs @@ -25,6 +25,7 @@ fn ser_de() { e: Option>, f: Option<([u32; 4], String)>, g: (), + h: f64, } let mut map = BTreeMap::new(); @@ -32,12 +33,13 @@ fn ser_de() { let test: Test = Test { a: 1, - b: 2., + b: 2.718281828459045, c: Some("asd".to_string()), d: None, e: Some(map), f: Some(([1, 2, 3, 4], "tuple".to_string())), g: (), + h: 1e30, }; #[cfg(feature = "binary")]