-
-
Notifications
You must be signed in to change notification settings - Fork 462
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
string numbers should be quoted #33
Comments
The YAML specification defines different schemas that specify how scalars should be treated by default. It is funny that you mention this because I just started working on schemas yesterday :) |
Any progress on this? |
I am also curious about progress on this. The problem is that not all strings should be serialized as $builder = [YamlDotNet.Serialization.SerializerBuilder]::new()
$serializer = $builder.Build()
$obj = @{"IAmAString" = "200"; "IAmAnInt" = 200}
$serializer.Serialize($obj) which outputs: IAmAString: 200
IAmAnInt: 200 When you deserialize this, you loose the type. This is why the Python yaml module, adds single quotes around values that might cause ambiguity. I think this could be fixed here: By testing if the value can be cast to any type other than string, and if it can be, just set:
|
So, using something like this: diff --git a/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs b/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs
index dae028e..d78aff5 100644
--- a/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs
+++ b/YamlDotNet/Serialization/EventEmitters/TypeAssigningEventEmitter.cs
@@ -81,6 +81,21 @@ namespace YamlDotNet.Serialization.EventEmitters
break;
case TypeCode.String:
+ decimal dec;
+ int number;
+ float flt;
+ long lng;
+ double dbl;
+ var val = eventInfo.Source.Value.ToString();
+ if (Int32.TryParse(val, out number) ||
+ Int64.TryParse(val, out lng) ||
+ Double.TryParse(val, out dbl) ||
+ Decimal.TryParse(val, out dec) ||
+ float.TryParse(val, out flt))
+ {
+ eventInfo.Style = ScalarStyle.DoubleQuoted;
+ }
+ goto case TypeCode.Char;
case TypeCode.Char:
eventInfo.Tag = "tag:yaml.org,2002:str";
eventInfo.RenderedValue = eventInfo.Source.Value.ToString();
@@ -146,4 +161,4 @@ namespace YamlDotNet.Serialization.EventEmitters
}
}
}
-}
\ No newline at end of file
+}
Results in: $builder = [YamlDotNet.Serialization.SerializerBuilder]::new()
$serializer = $builder.Build()
$obj = @{
"IAmAString" = "200"
"IAmAnInt" = 200
"11" = "KeyAsIntAndShouldBeDisambiguated"
"1sa1" = "JustAStringStartingWithANumber"
}
$serializer.Serialize($obj)
IAmAnInt: 200
IAmAString: "200"
"11": KeyAsIntAndShouldBeDisambiguated
1sa1: JustAStringStartingWithANumber Which uses quotes around values that are at risk to loose their type when serialized and deserialized by other parsers. |
Just checking in on the progress of this. It looks like @gabriel-samfira has fixed this downstream in the following PR: https://github.com/cloudbase/powershell-yaml/pull/41/files#diff-e4c14acd05e286e165f6b75e7a30d165. Is it possible to bring this change (or something similar) upstream in this library? |
@aaubry I noticed you accepted my PR #477, are you up to a version release soon? |
@Roadrunner67 I'll do a release today. Regarding the fix, I don't think it should be included because I don't think that it is correct. The YAML language uses tags to represent types. Whether a scalar is plain or quoted is a presentation detail. The spec states that two scalars are equal if they have the same tag and content, regardless of their style. Nevertheless, if someone wants @gabriel-samfira 's behaviour, it is just a matter of registering a implementation of |
That is actually how we implemented it in the end in @aaubry not sure if I ever mentioned this before, but thanks for all your work! |
Thanks @gabriel-samfira, you're welcome! |
is it normal that it don't work in 8.1.2? for the moment i still use something like that in my *.yaml files: |
I know this is pretty old, but it will be resolved in the next release so I'm closing this. |
Please fix it |
Nevermine i just writed myself a fixer of the output because I know that this is gonna take forever |
It’s actually released. On the serializer builder call with quoting necessary strings. |
It highlights everything except the strings |
This works, quoting string when they are numbers using YamlDotNet.Serialization;
var test = new { Item = "123" };
var serializer = new SerializerBuilder()
.WithQuotingNecessaryStrings()
.Build();
var actual = serializer.Serialize(test);
Console.WriteLine(actual); Results in:
|
yes but i dont want to quote numbers or bools I want to quote strings |
Currently you can use the yamlmember attribute to specify you want double or single quotes. Currently there is no feature for defaulting to using quotes. Wouldn’t be hard to do though. If that is what you are looking for, open a new issue. |
You get the same result if you serialize 5 or "5" or 5.0. As a result, type information is lost:
Since the result can be explicitly deserialized into int, string, or double it's not a serious bug.
I'm not even sure what the yaml spec says on the issue, as I usually just see what the PyYaml implementation does:
Anyway, great work on the library so far, the last release fixed a bug I was having trouble with :)
The text was updated successfully, but these errors were encountered: