diff --git a/src/de.rs b/src/de.rs index e5f21b06..bab38d13 100644 --- a/src/de.rs +++ b/src/de.rs @@ -987,7 +987,10 @@ where return visitor.visit_i64(n); } } - if v.len() > 1 && v.starts_with('0') && v.bytes().all(|b| b.is_ascii_digit()) { + if { + let v = v.trim_start_matches(&['-', '+'][..]); + v.len() > 1 && v.starts_with('0') && v[1..].bytes().all(|b| b.is_ascii_digit()) + } { // After handling the different number encodings above if we are left // with leading zero(s) followed by numeric characters this is in fact a // string according to the YAML 1.2 spec. diff --git a/tests/test_de.rs b/tests/test_de.rs index 9d7b5bc6..67464b17 100644 --- a/tests/test_de.rs +++ b/tests/test_de.rs @@ -339,7 +339,17 @@ fn test_numbers() { let value = serde_yaml::from_str::(yaml).unwrap(); match value { Value::Number(number) => assert_eq!(number.to_string(), expected), - _ => panic!("expected number"), + _ => panic!("expected number. input={:?}, result={:?}", yaml, value), + } + } + + // NOT numbers. + let cases = ["0127", "+0127", "-0127"]; + for yaml in &cases { + let value = serde_yaml::from_str::(yaml).unwrap(); + match value { + Value::String(string) => assert_eq!(string, *yaml), + _ => panic!("expected string. input={:?}, result={:?}", yaml, value), } } }