Git Product home page Git Product logo

Comments (6)

Ramilito avatar Ramilito commented on July 17, 2024 2

I'm getting similar behaviour using 0.9.26 but not in 0.8.26 where serializing and then deserializing results in an unquoted string as output.

The input:

          - name: KUBERNETES
            value: "yes"

Output:

          - name: KUBERNETES
            value: yes

from serde-yaml.

deadmilkman avatar deadmilkman commented on July 17, 2024 2

Having a similar issue with a key containing a string with the single character y. Even if a create the mapping value as a string, when outputing the yaml, y is not quoted. This ends up being interpreted as a bool instead of a string value by kubectl.

Reference: https://yaml.org/type/bool.html

Question: Is there a way of forcing values of type string to be always quoted when outputing as a string?

from serde-yaml.

dtolnay avatar dtolnay commented on July 17, 2024 1

This is behaving correctly as far as I can tell. 007 is a !!str in yaml, not a !!int. If a different library you are using is interpreting it as an int, that is a bug in the other library.

Here is the spec section that determines what untagged scalars are int: https://yaml.org/spec/1.2.2/#1022-tag-resolution.

from serde-yaml.

horacimacias avatar horacimacias commented on July 17, 2024

sorry, I should have checked this properly. I think you're right. thanks!

from serde-yaml.

james-jra avatar james-jra commented on July 17, 2024

I'm not sure I agree with this resolution. @dtolnay any chance you can take another look?

From the linked article I see that:

Scalars with the “?” non-specific tag (that is, plain scalars) are matched with an extended list of regular expressions.

One of which is [-+]? [0-9]+, which resolves to tag:yaml.org,2002:int (Base 10).

I think 007 is a plain scalar and matches the above regular expression, so it should be interpreted as an int. Therefore the round-trip of "007" to 007 is not valid, and parsing 007 as an integer is valid.

I've tested this with rust-yaml, yq, and PyYaml, which all agree so far that 007 is an int.

E.g.

use serde_yaml; // 0.9.24
use yaml_rust;  // 0.4.5

fn main() {
    use serde_yaml::Mapping;

    let original_config = r#"---
agent: "007"
"#;

    let parsed_serde_yaml: Mapping = serde_yaml::from_str(original_config).unwrap();

    // serde_yaml knows it's a string
    assert_eq!(parsed_serde_yaml["agent"], serde_yaml::Value::String("007".into()));
    let serialized_serde_yaml = serde_yaml::to_string(&parsed_serde_yaml).unwrap();

    // Serializes it back to 007, no quotes.
    assert_eq!(serialized_serde_yaml, r#"agent: 007
"#);

    // serde_yaml parses it back as a string, so we're self-consistent.
    let parsed_serde_yaml: Mapping = serde_yaml::from_str(&serialized_serde_yaml).unwrap();
    assert_eq!(parsed_serde_yaml["agent"], serde_yaml::Value::String("007".into()));

    // But yaml_rust parses it as an integer
    let parsed_yaml_rust = yaml_rust::YamlLoader::load_from_str(&serialized_serde_yaml).unwrap();
    let doc = &parsed_yaml_rust[0];
    // thread 'main' panicked at 'assertion failed: `(left == right)`
    // left: `Integer(7)`,
    // right: `String("007")`',
    assert_eq!(doc["agent"], yaml_rust::Yaml::String("007".into()));
}

from serde-yaml.

dtolnay avatar dtolnay commented on July 17, 2024

Yeah, good call.

Fixed in serde_yaml 0.9.25.

from serde-yaml.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.