about summary refs log tree commit diff
diff options
context:
space:
mode:
-rw-r--r--src/libserialize/json.rs26
1 files changed, 24 insertions, 2 deletions
diff --git a/src/libserialize/json.rs b/src/libserialize/json.rs
index 3a5e3cfe240..cef50a1308f 100644
--- a/src/libserialize/json.rs
+++ b/src/libserialize/json.rs
@@ -1951,7 +1951,10 @@ macro_rules! read_primitive {
                 String(s) => {
                     // re: #12967.. a type w/ numeric keys (ie HashMap<uint, V> etc)
                     // is going to have a string here, as per JSON spec.
-                    Ok(std::from_str::from_str(s.as_slice()).unwrap())
+                    match std::from_str::from_str(s.as_slice()) {
+                        Some(f) => Ok(f),
+                        None => Err(ExpectedError("Number".to_string(), s)),
+                    }
                 },
                 value => Err(ExpectedError("Number".to_string(), format!("{}", value)))
             }
@@ -1987,7 +1990,10 @@ impl ::Decoder<DecoderError> for Decoder {
             String(s) => {
                 // re: #12967.. a type w/ numeric keys (ie HashMap<uint, V> etc)
                 // is going to have a string here, as per JSON spec.
-                Ok(std::from_str::from_str(s.as_slice()).unwrap())
+                match std::from_str::from_str(s.as_slice()) {
+                    Some(f) => Ok(f),
+                    None => Err(ExpectedError("Number".to_string(), s)),
+                }
             },
             Null => Ok(f64::NAN),
             value => Err(ExpectedError("Number".to_string(), format!("{}", value)))
@@ -3169,6 +3175,7 @@ mod tests {
             _ => {} // it parsed and we are good to go
         }
     }
+
     #[test]
     fn test_prettyencode_hashmap_with_numeric_key() {
         use std::str::from_utf8;
@@ -3189,6 +3196,7 @@ mod tests {
             _ => {} // it parsed and we are good to go
         }
     }
+
     #[test]
     fn test_hashmap_with_numeric_key_can_handle_double_quote_delimited_key() {
         use std::collections::HashMap;
@@ -3202,6 +3210,20 @@ mod tests {
         let _hm: HashMap<uint, bool> = Decodable::decode(&mut decoder).unwrap();
     }
 
+    #[test]
+    fn test_hashmap_with_numeric_key_will_error_with_string_keys() {
+        use std::collections::HashMap;
+        use Decodable;
+        let json_str = "{\"a\":true}";
+        let json_obj = match from_str(json_str) {
+            Err(_) => fail!("Unable to parse json_str: {}", json_str),
+            Ok(o) => o
+        };
+        let mut decoder = Decoder::new(json_obj);
+        let result: Result<HashMap<uint, bool>, DecoderError> = Decodable::decode(&mut decoder);
+        assert_eq!(result, Err(ExpectedError("Number".to_string(), "a".to_string())));
+    }
+
     fn assert_stream_equal(src: &str,
                            expected: Vec<(JsonEvent, Vec<StackElement>)>) {
         let mut parser = Parser::new(src.chars());