about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBarosl Lee <vcs@barosl.com>2014-11-24 03:00:10 +0900
committerBarosl Lee <vcs@barosl.com>2014-12-08 18:02:12 +0900
commitf102123b659911dbee25bbe518d73d66185bb978 (patch)
tree35bfc3b87828ebc9f2f041c3e6a978ec185b1ca0
parentca4f53655e82a0a0e1d613b16108720bb7a50fde (diff)
downloadrust-f102123b659911dbee25bbe518d73d66185bb978.tar.gz
rust-f102123b659911dbee25bbe518d73d66185bb978.zip
libserialize: Do not coerce to integer when decoding a float value
When an integral value is expected by the user but a fractional value is
found, the current implementation uses std::num::cast() to coerce to an
integer type, losing the fractional part. This behavior is not desirable
because the number loses precision without notice.

This commit makes it raise ExpectedError when such a situation arises.

[breaking-change]
-rw-r--r--src/libserialize/json.rs8
1 files changed, 4 insertions, 4 deletions
diff --git a/src/libserialize/json.rs b/src/libserialize/json.rs
index 371034746fc..23e2c4b9830 100644
--- a/src/libserialize/json.rs
+++ b/src/libserialize/json.rs
@@ -1974,10 +1974,7 @@ macro_rules! read_primitive {
                     }
                 }
                 Json::F64(f) => {
-                    match num::cast(f) {
-                        Some(f) => Ok(f),
-                        None => Err(ExpectedError("Number".to_string(), format!("{}", f))),
-                    }
+                    Err(ExpectedError("Integer".to_string(), format!("{}", f)))
                 }
                 Json::String(s) => {
                     // re: #12967.. a type w/ numeric keys (ie HashMap<uint, V> etc)
@@ -2830,6 +2827,9 @@ mod tests {
 
         let v: i64 = super::decode("9223372036854775807").unwrap();
         assert_eq!(v, i64::MAX);
+
+        let res: DecodeResult<i64> = super::decode("765.25252");
+        assert_eq!(res, Err(ExpectedError("Integer".into_string(), "765.25252".into_string())));
     }
 
     #[test]