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

double quotes lost when deserializing and serializing strings containing only numbers on serde_yaml 0.9 #347

Closed
@horacimacias

Description

@horacimacias

I'm reading some yaml, editing and then saving it again after doing to_string.
For some reason I'm not understanding, some String values which only contain numbers end up not having the double quotes after to_string.
I'm feeding this yaml to something that expects Strings, not numbers, and I'm expecting serde_yaml to respect whichever type the value was.
Here's a minimal reproduceable test, which passes if I use serde_yaml 0.8.26 but does not pass if I use 0.9:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2021&gist=6de99a49da249819a76e5fcd25342ec3

extern crate serde;
extern crate serde_yaml;


#[test]
fn can_serialize_with_quotes() {
    use serde_yaml::Mapping;
    let original_config = r#"---
configuration:
  agent: "007"
"#;
    let config: Mapping = serde_yaml::from_str(original_config).expect("should parse yaml");
    let config = serde_yaml::to_string(&config).expect("should serialize");
    assert_eq!(config, original_config);
}

basically this input:

configuration:
  agent: "007"

ends up as:

configuration:
  agent: 007

(there is also another difference between 0.8 and 0.9 regarding having or not having ---\n at the beginning of the String, but this is not relevant as far as I can tell; the issue is that a String value ends up being converted to a Number value).

Is there any way to avoid this String->Number conversion or some other way to make this test pass?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions