From 507d3d69ff53bfb69e62b4e9ee57946060cfd454 Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 27 Jun 2016 02:59:51 -0700 Subject: [PATCH 1/2] Speed up float printing --- json/Cargo.toml | 1 + json/src/lib.rs | 1 + json/src/ser.rs | 5 +++-- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/json/Cargo.toml b/json/Cargo.toml index d12359c29..496e8cd6e 100644 --- a/json/Cargo.toml +++ b/json/Cargo.toml @@ -19,3 +19,4 @@ num-traits = "~0.1.32" clippy = { version = "^0.*", optional = true } linked-hash-map = { version = "0.0.11", optional = true } itoa = "0.1" +dtoa = "0.1" diff --git a/json/src/lib.rs b/json/src/lib.rs index a497b71f2..b760b0099 100644 --- a/json/src/lib.rs +++ b/json/src/lib.rs @@ -121,6 +121,7 @@ extern crate num_traits; extern crate core; extern crate serde; extern crate itoa; +extern crate dtoa; #[cfg(feature = "preserve_order")] extern crate linked_hash_map; diff --git a/json/src/ser.rs b/json/src/ser.rs index 6dae27d5c..a5885af20 100644 --- a/json/src/ser.rs +++ b/json/src/ser.rs @@ -9,6 +9,7 @@ use serde::ser; use super::error::{Error, ErrorCode, Result}; use itoa; +use dtoa; /// A structure for serializing Rust values into JSON. pub struct Serializer { @@ -588,7 +589,7 @@ fn fmt_f32_or_null(wr: &mut W, value: f32) -> Result<()> try!(wr.write_all(b"null")) } _ => { - try!(write!(wr, "{:?}", value)) + try!(dtoa::write(wr, value)) } } @@ -603,7 +604,7 @@ fn fmt_f64_or_null(wr: &mut W, value: f64) -> Result<()> try!(wr.write_all(b"null")) } _ => { - try!(write!(wr, "{:?}", value)) + try!(dtoa::write(wr, value)) } } From 898b2a56b2a39e26bfa2329999006690fcec09ef Mon Sep 17 00:00:00 2001 From: David Tolnay Date: Mon, 27 Jun 2016 03:04:38 -0700 Subject: [PATCH 2/2] Fix f64 tests --- README.md | 4 ++-- json_tests/tests/test_json.rs | 12 ++++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index a75675182..9d46abdef 100644 --- a/README.md +++ b/README.md @@ -41,7 +41,7 @@ fn main() { map.insert("y".to_string(), 2.0); let s = serde_json::to_string(&map).unwrap(); - assert_eq!(s, "{\"x\":1,\"y\":2}"); + assert_eq!(s, "{\"x\":1.0,\"y\":2.0}"); let deserialized_map: Map = serde_json::from_str(&s).unwrap(); assert_eq!(map, deserialized_map); @@ -78,7 +78,7 @@ fn main() { let point = Point { x: 1.0, y: 2.0 }; let s = serde_json::to_string(&point).unwrap(); - assert_eq!(s, "{\"x\":1,\"y\":2}"); + assert_eq!(s, "{\"x\":1.0,\"y\":2.0}"); let deserialized_point: Point = serde_json::from_str(&s).unwrap(); assert_eq!(point, deserialized_point); diff --git a/json_tests/tests/test_json.rs b/json_tests/tests/test_json.rs index f67a3eef2..33679fc1f 100644 --- a/json_tests/tests/test_json.rs +++ b/json_tests/tests/test_json.rs @@ -117,18 +117,14 @@ fn test_write_i64() { #[test] fn test_write_f64() { - let min_string = format!("{:?}", f64::MIN); - let max_string = format!("{:?}", f64::MAX); - let epsilon_string = format!("{:?}", f64::EPSILON); - let tests = &[ - (3.0, "3"), + (3.0, "3.0"), (3.1, "3.1"), (-1.5, "-1.5"), (0.5, "0.5"), - (f64::MIN, &min_string), - (f64::MAX, &max_string), - (f64::EPSILON, &epsilon_string), + (f64::MIN, "-1.7976931348623157e308"), + (f64::MAX, "1.7976931348623157e308"), + (f64::EPSILON, "2.220446049250313e-16"), ]; test_encode_ok(tests); test_pretty_encode_ok(tests);