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

Quote YAML 1.1 bools during serialization #412

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md
Expand Up @@ -75,7 +75,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 All @@ -100,7 +100,7 @@ fn main() -> Result<(), serde_yaml::Error> {
let yaml = "
- !Newtype 1
- !Tuple [0, 0, 0]
- !Struct {x: 1.0, y: 2.0}
- !Struct {x: 1.0, 'y': 2.0}
";
let values: Vec<Enum> = serde_yaml::from_str(yaml).unwrap();
assert_eq!(values[0], Enum::Newtype(1));
Expand All @@ -115,7 +115,7 @@ fn main() -> Result<(), serde_yaml::Error> {
- 0
- !Struct
x: 1.0
y: 2.0
'y': 2.0
";
let values: Vec<Enum> = serde_yaml::from_str(yaml).unwrap();
assert_eq!(values[0], Enum::Tuple(0, 0, 0));
Expand Down
6 changes: 3 additions & 3 deletions src/lib.rs
Expand Up @@ -25,7 +25,7 @@
//!
//! // Serialize it to a YAML string.
//! 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 +55,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 Expand Up @@ -96,7 +96,7 @@
//! - 0
//! - !Struct
//! x: 1.0
//! y: 2.0
//! 'y': 2.0
//! ";
//! let values: Vec<Enum> = serde_yaml::from_str(yaml).unwrap();
//! assert_eq!(values[0], Enum::Tuple(0, 0, 0));
Expand Down
26 changes: 16 additions & 10 deletions src/ser.rs
Expand Up @@ -352,16 +352,22 @@ where
}
}

let style = if value.contains('\n') {
ScalarStyle::Literal
} else {
let result = crate::de::visit_untagged_scalar(
InferScalarStyle,
value,
None,
libyaml::parser::ScalarStyle::Plain,
);
result.unwrap_or(ScalarStyle::Any)
let style = match value {
// Backwards compatibility with old YAML boolean scalars.
// See https://yaml.org/type/bool.html
"y" | "Y" | "yes" | "Yes" | "YES" | "n" | "N" | "no" | "No" | "NO" | "true"
| "True" | "TRUE" | "false" | "False" | "FALSE" | "on" | "On" | "ON" | "off"
| "Off" | "OFF" => ScalarStyle::SingleQuoted,
_ if value.contains('\n') => ScalarStyle::Literal,
_ => {
let result = crate::de::visit_untagged_scalar(
InferScalarStyle,
value,
None,
libyaml::parser::ScalarStyle::Plain,
);
result.unwrap_or(ScalarStyle::Any)
}
};

self.emit_scalar(Scalar {
Expand Down
6 changes: 3 additions & 3 deletions tests/test_de.rs
Expand Up @@ -209,10 +209,10 @@ fn test_enum_representations() {
- !Tuple
- 0
- 0
- !Struct {x: 0, y: 0}
- !Struct {x: 0, 'y': 0}
- !Struct
x: 0
y: 0
'y': 0
- !String '...'
- !String ...
- !Number 0
Expand Down Expand Up @@ -390,7 +390,7 @@ fn test_bomb() {
v: &v [*u,*u,*u,*u,*u,*u,*u,*u,*u]
w: &w [*v,*v,*v,*v,*v,*v,*v,*v,*v]
x: &x [*w,*w,*w,*w,*w,*w,*w,*w,*w]
y: &y [*x,*x,*x,*x,*x,*x,*x,*x,*x]
'y': &y [*x,*x,*x,*x,*x,*x,*x,*x,*x]
z: &z [*y,*y,*y,*y,*y,*y,*y,*y,*y]
expected: string
"};
Expand Down
64 changes: 62 additions & 2 deletions tests/test_serde.rs
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 @@ -270,6 +270,66 @@ fn test_string_escapes() {
test_serde(&"\u{1f389}".to_owned(), yaml);
}

#[test]
fn test_boolish_serialization() {
// See https://yaml.org/type/bool.html
let thing = vec![
Value::String("y".to_owned()),
Value::String("Y".to_owned()),
Value::String("yes".to_owned()),
Value::String("Yes".to_owned()),
Value::String("YES".to_owned()),
Value::String("n".to_owned()),
Value::String("N".to_owned()),
Value::String("no".to_owned()),
Value::String("No".to_owned()),
Value::String("NO".to_owned()),
Value::String("true".to_owned()),
Value::String("True".to_owned()),
Value::String("TRUE".to_owned()),
Value::String("false".to_owned()),
Value::String("False".to_owned()),
Value::String("FALSE".to_owned()),
Value::String("on".to_owned()),
Value::String("On".to_owned()),
Value::String("ON".to_owned()),
Value::String("off".to_owned()),
Value::String("Off".to_owned()),
Value::String("OFF".to_owned()),
Value::Bool(true),
Value::Bool(false),
];

let yaml = indoc! {"
- 'y'
- 'Y'
- 'yes'
- 'Yes'
- 'YES'
- 'n'
- 'N'
- 'no'
- 'No'
- 'NO'
- 'true'
- 'True'
- 'TRUE'
- 'false'
- 'False'
- 'FALSE'
- 'on'
- 'On'
- 'ON'
- 'off'
- 'Off'
- 'OFF'
- true
- false
"};

test_serde(&thing, yaml);
}

#[test]
fn test_multiline_string() {
#[derive(Serialize, Deserialize, PartialEq, Debug)]
Expand Down
6 changes: 3 additions & 3 deletions tests/test_value.rs
Expand Up @@ -63,16 +63,16 @@ fn test_merge() {
// From https://yaml.org/type/merge.html.
let yaml = indoc! {"
---
- &CENTER { x: 1, y: 2 }
- &LEFT { x: 0, y: 2 }
- &CENTER { x: 1, 'y': 2 }
- &LEFT { x: 0, 'y': 2 }
- &BIG { r: 10 }
- &SMALL { r: 1 }

# All the following maps are equal:

- # Explicit keys
x: 1
y: 2
'y': 2
r: 10
label: center/big

Expand Down