Skip to content

Commit 59be9e9

Browse files
committed
---
yaml --- r: 128490 b: refs/heads/auto c: 9b23287 h: refs/heads/master v: v3
1 parent 24f816a commit 59be9e9

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
@@ -13,7 +13,7 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1313
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1414
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1515
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
16-
refs/heads/auto: e95552c5e624ac1ad0c04db505e34122c9d261ae
16+
refs/heads/auto: 9b2328797472b710276ef9b627f9d18bd06fbe5a
1717
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1818
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1919
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/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)