Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Commit

Permalink
handling for NO on serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
thedavidmeister committed Nov 28, 2023
1 parent c3fb5bc commit e923a53
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 13 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@ fn main() -> Result<(), serde_yaml::Error> {
map.insert("y".to_string(), 2.0);

// Serialize it to a YAML string.
// y is quoted to avoid ambiguity in parsers that might read it as `true`.
let yaml = serde_yaml::to_string(&map)?;
assert_eq!(yaml, "x: 1.0\ny: 2.0\n");
assert_eq!(yaml, "x: 1.0\n'y': 2.0\n");

// Deserialize it back to a Rust type.
let deserialized_map: BTreeMap<String, f64> = serde_yaml::from_str(&yaml)?;
Expand Down Expand Up @@ -75,7 +76,7 @@ fn main() -> Result<(), serde_yaml::Error> {
let point = Point { x: 1.0, y: 2.0 };

let yaml = serde_yaml::to_string(&point)?;
assert_eq!(yaml, "x: 1.0\ny: 2.0\n");
assert_eq!(yaml, "x: 1.0\n'y': 2.0\n");

let deserialized_point: Point = serde_yaml::from_str(&yaml)?;
assert_eq!(point, deserialized_point);
Expand Down
21 changes: 14 additions & 7 deletions src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1101,13 +1101,20 @@ pub(crate) fn digits_but_not_number(scalar: &str) -> bool {
/// serialize it with quotes to be safe.
/// This avoids the norway problem https://hitchdev.com/strictyaml/why/implicit-typing-removed/
pub(crate) fn ambiguous_string(scalar: &str) -> bool {
parse_bool(scalar).is_some()
|| parse_null(scalar.as_bytes()).is_some()
|| scalar.len() == 0
|| scalar.bytes().nth(0).unwrap().is_ascii_digit()
|| scalar.starts_with('-')
|| scalar.starts_with('.')
|| scalar.starts_with("+")
let lower_scalar = scalar.to_lowercase();
parse_bool(&lower_scalar).is_some()
|| parse_null(&lower_scalar.as_bytes()).is_some()
|| lower_scalar.len() == 0
|| lower_scalar.bytes().nth(0).unwrap().is_ascii_digit()
|| lower_scalar.starts_with('-')
|| lower_scalar.starts_with('.')
|| lower_scalar.starts_with("+")
// Things that we don't parse as bool but could be parsed as bool by
// other YAML parsers.
|| lower_scalar == "y"
|| lower_scalar == "yes"
|| lower_scalar == "n"
|| lower_scalar == "no"
}

pub(crate) fn visit_int<'de, V>(visitor: V, v: &str) -> Result<Result<V::Value>, V>
Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,9 @@
//! map.insert("y".to_string(), 2.0);
//!
//! // Serialize it to a YAML string.
//! // 'y' is quoted to avoid ambiguity in parsers that might read it as `true`.
//! let yaml = serde_yaml::to_string(&map)?;
//! assert_eq!(yaml, "x: 1.0\ny: 2.0\n");
//! assert_eq!(yaml, "x: 1.0\n'y': 2.0\n");
//!
//! // Deserialize it back to a Rust type.
//! let deserialized_map: BTreeMap<String, f64> = serde_yaml::from_str(&yaml)?;
Expand Down Expand Up @@ -55,7 +56,7 @@
//! let point = Point { x: 1.0, y: 2.0 };
//!
//! let yaml = serde_yaml::to_string(&point)?;
//! assert_eq!(yaml, "x: 1.0\ny: 2.0\n");
//! assert_eq!(yaml, "x: 1.0\n'y': 2.0\n");
//!
//! let deserialized_point: Point = serde_yaml::from_str(&yaml)?;
//! assert_eq!(point, deserialized_point);
Expand Down
21 changes: 19 additions & 2 deletions tests/test_serde.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ fn test_map() {
thing.insert("y".to_owned(), 2);
let yaml = indoc! {"
x: 1
y: 2
'y': 2
"};
test_serde(&thing, yaml);
}
Expand Down Expand Up @@ -238,7 +238,7 @@ fn test_basic_struct() {
};
let yaml = indoc! {r#"
x: -4
y: "hi\tquoted"
'y': "hi\tquoted"
z: true
"#};
test_serde(&thing, yaml);
Expand Down Expand Up @@ -335,6 +335,23 @@ fn test_strings_needing_quote() {
string: ''
"};
test_serde(&thing3, yaml3);

let thing4 = Struct2 {
string: " ".to_owned(),
};
let yaml4 = indoc! {"
string: ' '
"};
test_serde(&thing4, yaml4);

// The literal norway problem https://hitchdev.com/strictyaml/why/implicit-typing-removed/
let thing5 = Struct2 {
string: "NO".to_owned(),
};
let yaml5 = indoc! {"
string: 'NO'
"};
test_serde(&thing5, yaml5);
}

#[test]
Expand Down

0 comments on commit e923a53

Please sign in to comment.