Skip to content

Commit

Permalink
Don't fail if an object is keyed with a string and we're expecting a …
Browse files Browse the repository at this point in the history
…number
  • Loading branch information
erickt committed Aug 19, 2014
1 parent e95552c commit 9b23287
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/libserialize/json.rs
Expand Up @@ -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)))
}
Expand Down Expand Up @@ -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)))
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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());
Expand Down

5 comments on commit 9b23287

@bors
Copy link
Contributor

@bors bors commented on 9b23287 Aug 19, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from acrichto
at erickt@9b23287

@bors
Copy link
Contributor

@bors bors commented on 9b23287 Aug 19, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging erickt/rust/json-ints = 9b23287 into auto

@bors
Copy link
Contributor

@bors bors commented on 9b23287 Aug 19, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

erickt/rust/json-ints = 9b23287 merged ok, testing candidate = 0600a3b

@bors
Copy link
Contributor

@bors bors commented on 9b23287 Aug 19, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 0600a3b

Please sign in to comment.