Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix ron & json serialization of large floats #106

Merged
merged 2 commits into from
Jun 22, 2024
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
2 changes: 1 addition & 1 deletion src/serde_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}"));
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/serde_ron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:?}"));
}
}

Expand Down
36 changes: 36 additions & 0 deletions tests/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use std::{
collections::{BTreeMap, BTreeSet, LinkedList},
fmt::Debug,
sync::atomic::AtomicBool,
};

Expand Down Expand Up @@ -69,7 +70,7 @@
fn de_illegal_inline_comment() {
#[derive(DeJson)]
pub struct Test {
pub a: f32,

Check warning on line 73 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, x86_64-unknown-linux-gnu)

field `a` is never read

Check warning on line 73 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, wasm32-unknown-unknown)

field `a` is never read

Check warning on line 73 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Build (macos-latest, x86_64-apple-darwin)

field `a` is never read

Check warning on line 73 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, x86_64-unknown-linux-gnu)

field `a` is never read

Check warning on line 73 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Test Individual Features NoStd (json)

field `a` is never read

Check warning on line 73 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Test Individual Features (json)

field `a` is never read

Check warning on line 73 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Test No Std (ubuntu-latest, x86_64-unknown-linux-gnu)

field `a` is never read

Check warning on line 73 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Test No Std (ubuntu-latest, x86_64-unknown-linux-gnu)

field `a` is never read

Check warning on line 73 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, x86_64-pc-windows-gnu)

field `a` is never read

Check warning on line 73 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, x86_64-pc-windows-msvc)

field `a` is never read
}

let jsons = vec![
Expand All @@ -92,7 +93,7 @@
fn de_illegal_multiline_comment() {
#[derive(DeJson)]
pub struct Test {
pub a: f32,

Check warning on line 96 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, x86_64-unknown-linux-gnu)

field `a` is never read

Check warning on line 96 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, wasm32-unknown-unknown)

field `a` is never read

Check warning on line 96 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Build (macos-latest, x86_64-apple-darwin)

field `a` is never read

Check warning on line 96 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, x86_64-unknown-linux-gnu)

field `a` is never read

Check warning on line 96 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest, x86_64-unknown-linux-gnu)

field `a` is never read

Check warning on line 96 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Test Individual Features NoStd (json)

field `a` is never read

Check warning on line 96 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Test Individual Features (json)

field `a` is never read

Check warning on line 96 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Test No Std (ubuntu-latest, x86_64-unknown-linux-gnu)

field `a` is never read

Check warning on line 96 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Test No Std (ubuntu-latest, x86_64-unknown-linux-gnu)

field `a` is never read

Check warning on line 96 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Build (ubuntu-latest, x86_64-pc-windows-gnu)

field `a` is never read

Check warning on line 96 in tests/json.rs

View workflow job for this annotation

GitHub Actions / Build (windows-latest, x86_64-pc-windows-msvc)

field `a` is never read
}

let jsons = vec![
Expand Down Expand Up @@ -702,6 +703,41 @@
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),
<FloatWrapper as DeJson>::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)]
Expand Down
35 changes: 35 additions & 0 deletions tests/ron.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
<FloatWrapper as DeRon>::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)]
Expand Down
4 changes: 3 additions & 1 deletion tests/ser_de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,21 @@ fn ser_de() {
e: Option<BTreeMap<String, String>>,
f: Option<([u32; 4], String)>,
g: (),
h: f64,
}

let mut map = BTreeMap::new();
map.insert("a".to_string(), "b".to_string());

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")]
Expand Down
Loading