From 3c5bb6f4a5f9db617919a66b352bdc4d6bb2906b 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 1/2] 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")] From eba9ac75799ffa3e2a44b7fb92f30944deb70473 Mon Sep 17 00:00:00 2001 From: kirk Date: Sat, 22 Jun 2024 17:04:15 +0000 Subject: [PATCH 2/2] add another test for floats --- tests/json.rs | 36 ++++++++++++++++++++++++++++++++++++ tests/ron.rs | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 71 insertions(+) diff --git a/tests/json.rs b/tests/json.rs index 5aa14d1..dc160f8 100644 --- a/tests/json.rs +++ b/tests/json.rs @@ -3,6 +3,7 @@ use nanoserde::{DeJson, SerJson}; use std::{ collections::{BTreeMap, BTreeSet, LinkedList}, + fmt::Debug, sync::atomic::AtomicBool, }; @@ -702,6 +703,41 @@ fn test_various_escapes() { assert_eq!(unescaped, "\n\t\u{20}\x0c\x08\\\"/😋\r"); } +#[test] +fn test_various_floats() { + #[derive(Debug, SerJson, DeJson, PartialEq)] + struct FloatWrapper { + f32: f32, + f64: f64, + } + + impl From<&(f32, f64)> for FloatWrapper { + fn from(value: &(f32, f64)) -> Self { + Self { + f32: value.0, + f64: value.1, + } + } + } + + let cases: &[(f32, f64)] = &[ + (0.0, 0.0), + (f32::MAX, f64::MAX), + (f32::MIN, f64::MIN), + (f32::MIN_POSITIVE, f64::MIN_POSITIVE), + ]; + + for case in cases { + assert_eq!( + FloatWrapper::from(case), + ::deserialize_json(&dbg!( + FloatWrapper::from(case).serialize_json() + )) + .unwrap() + ) + } +} + // there are only 1024*1024 surrogate pairs, so we can do an exhautive test. #[test] #[cfg_attr(miri, ignore)] diff --git a/tests/ron.rs b/tests/ron.rs index df8395e..b87e462 100644 --- a/tests/ron.rs +++ b/tests/ron.rs @@ -450,6 +450,41 @@ fn test_various_escapes() { assert_eq!(unescaped, "\n\t\u{20}\x0c\x08\\\"/😋\r"); } +#[test] +fn test_various_floats() { + #[derive(Debug, SerRon, DeRon, PartialEq)] + struct FloatWrapper { + f32: f32, + f64: f64, + } + + impl From<&(f32, f64)> for FloatWrapper { + fn from(value: &(f32, f64)) -> Self { + Self { + f32: value.0, + f64: value.1, + } + } + } + + let cases: &[(f32, f64)] = &[ + (0.0, 0.0), + (f32::MAX, f64::MAX), + (f32::MIN, f64::MIN), + (f32::MIN_POSITIVE, f64::MIN_POSITIVE), + ]; + + for case in cases { + assert_eq!( + FloatWrapper::from(case), + ::deserialize_ron(&dbg!( + FloatWrapper::from(case).serialize_ron() + )) + .unwrap() + ) + } +} + // there are only 1024*1024 surrogate pairs, so we can do an exhautive test. #[test] #[cfg_attr(miri, ignore)]