Comments (6)
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.
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.
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.
sorry, I should have checked this properly. I think you're right. thanks!
from serde-yaml.
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.
Yeah, good call.
Fixed in serde_yaml 0.9.25.
from serde-yaml.
Related Issues (20)
- #[serde(flatten)] disables treating an integer as a string
- Make Iterator implementation of Deserializer::from_reader lazy
- Fail to deserialize i128 in internally tagged enum HOT 1
- Deserialization using serde_yaml seems to be failing when it involves a nested struct
- [BUG] Tagged null at end of flow sequence gives scanner error
- Inconsistent Quoting Behavior in String Serialization HOT 2
- QUESTION: decoded from blank dictionary field
- Document the YAML specification version and schema
- Enhancement: Allow compact one-line representation of some fields HOT 1
- Yaml with colon at the beginning of a value won't parse
- Support for serialization/deserialization of nested enums HOT 1
- Deserializer only acknowledges first line of YAML HOT 2
- Long Map keys bug HOT 3
- Untagged variant will make tagged ones unparsable HOT 1
- Thoughts about libyaml-safer HOT 3
- how can I merge 2 serde_yaml::Value? HOT 1
- ignore trailing document separators
- Unable to deserialize number as string for HashMap if flattened or inside untagged enum
- Why is it trying to deserialize a map instead of an enum?
- Singleton Map Recursion fails to compile due to recursion
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from serde-yaml.