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

Speed up float printing #88

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, f64> = serde_json::from_str(&s).unwrap();
assert_eq!(map, deserialized_map);
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions json/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
1 change: 1 addition & 0 deletions json/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
5 changes: 3 additions & 2 deletions json/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<W, F=CompactFormatter> {
Expand Down Expand Up @@ -588,7 +589,7 @@ fn fmt_f32_or_null<W>(wr: &mut W, value: f32) -> Result<()>
try!(wr.write_all(b"null"))
}
_ => {
try!(write!(wr, "{:?}", value))
try!(dtoa::write(wr, value))
}
}

Expand All @@ -603,7 +604,7 @@ fn fmt_f64_or_null<W>(wr: &mut W, value: f64) -> Result<()>
try!(wr.write_all(b"null"))
}
_ => {
try!(write!(wr, "{:?}", value))
try!(dtoa::write(wr, value))
}
}

Expand Down
12 changes: 4 additions & 8 deletions json_tests/tests/test_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down