Skip to content

Commit e2ec70b

Browse files
committed
---
yaml --- r: 129390 b: refs/heads/snap-stage3 c: 9b23287 h: refs/heads/master v: v3
1 parent c5e4f2b commit e2ec70b

File tree

2 files changed

+25
-3
lines changed

2 files changed

+25
-3
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 566b470e138e929e8a93d613372db1ba177c494f
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: e95552c5e624ac1ad0c04db505e34122c9d261ae
4+
refs/heads/snap-stage3: 9b2328797472b710276ef9b627f9d18bd06fbe5a
55
refs/heads/try: 80b45ddbd351f0a4a939c3a3c4e20b4defec4b35
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/libserialize/json.rs

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1951,7 +1951,10 @@ macro_rules! read_primitive {
19511951
String(s) => {
19521952
// re: #12967.. a type w/ numeric keys (ie HashMap<uint, V> etc)
19531953
// is going to have a string here, as per JSON spec.
1954-
Ok(std::from_str::from_str(s.as_slice()).unwrap())
1954+
match std::from_str::from_str(s.as_slice()) {
1955+
Some(f) => Ok(f),
1956+
None => Err(ExpectedError("Number".to_string(), s)),
1957+
}
19551958
},
19561959
value => Err(ExpectedError("Number".to_string(), format!("{}", value)))
19571960
}
@@ -1987,7 +1990,10 @@ impl ::Decoder<DecoderError> for Decoder {
19871990
String(s) => {
19881991
// re: #12967.. a type w/ numeric keys (ie HashMap<uint, V> etc)
19891992
// is going to have a string here, as per JSON spec.
1990-
Ok(std::from_str::from_str(s.as_slice()).unwrap())
1993+
match std::from_str::from_str(s.as_slice()) {
1994+
Some(f) => Ok(f),
1995+
None => Err(ExpectedError("Number".to_string(), s)),
1996+
}
19911997
},
19921998
Null => Ok(f64::NAN),
19931999
value => Err(ExpectedError("Number".to_string(), format!("{}", value)))
@@ -3169,6 +3175,7 @@ mod tests {
31693175
_ => {} // it parsed and we are good to go
31703176
}
31713177
}
3178+
31723179
#[test]
31733180
fn test_prettyencode_hashmap_with_numeric_key() {
31743181
use std::str::from_utf8;
@@ -3189,6 +3196,7 @@ mod tests {
31893196
_ => {} // it parsed and we are good to go
31903197
}
31913198
}
3199+
31923200
#[test]
31933201
fn test_hashmap_with_numeric_key_can_handle_double_quote_delimited_key() {
31943202
use std::collections::HashMap;
@@ -3202,6 +3210,20 @@ mod tests {
32023210
let _hm: HashMap<uint, bool> = Decodable::decode(&mut decoder).unwrap();
32033211
}
32043212

3213+
#[test]
3214+
fn test_hashmap_with_numeric_key_will_error_with_string_keys() {
3215+
use std::collections::HashMap;
3216+
use Decodable;
3217+
let json_str = "{\"a\":true}";
3218+
let json_obj = match from_str(json_str) {
3219+
Err(_) => fail!("Unable to parse json_str: {}", json_str),
3220+
Ok(o) => o
3221+
};
3222+
let mut decoder = Decoder::new(json_obj);
3223+
let result: Result<HashMap<uint, bool>, DecoderError> = Decodable::decode(&mut decoder);
3224+
assert_eq!(result, Err(ExpectedError("Number".to_string(), "a".to_string())));
3225+
}
3226+
32053227
fn assert_stream_equal(src: &str,
32063228
expected: Vec<(JsonEvent, Vec<StackElement>)>) {
32073229
let mut parser = Parser::new(src.chars());

0 commit comments

Comments
 (0)