chyh1990 / yaml-rust

A pure rust YAML implementation.
Apache License 2.0
609 stars 140 forks source link

If you load a quoted string that looks like a number, then save it, it is no longer a string #133

Closed bjorn-ove closed 5 years ago

bjorn-ove commented 5 years ago

There is no way as far as I can tell to store number like strings. When you parse the yaml file everything works as expected, but when you serialize it again it turns into a number.

I did look at the response in #56, but don't think that applies here. My use-case is serde-yaml, but I don't think it is possible to solve it there before it is solved here.

let doc = YamlLoader::load_from_str("\"0x123\"").unwrap().pop().unwrap();
assert_eq!(doc, Yaml::String("0x123".to_string()));

let mut out_str = String::new();
YamlEmitter::new(&mut out_str).dump(&doc).unwrap();
let doc2 = YamlLoader::load_from_str(&out_str).unwrap().pop().unwrap();
assert_eq!(doc, doc2); // This fails because the type has changed to a number now
hoodie commented 5 years ago

(looks like I never hit the Comment button here) This looks like the emitter doesn't properly quote strings that start with 0x. I'll see if that can't be fixed.

hoodie commented 5 years ago

@dtolnay @chyh1990 this is an issue that is already fixed, please take a look