Skip to content
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

Better serialization of strings that look like integers (specifically, octals) #934

Closed
fiotti opened this issue Jun 17, 2024 · 2 comments
Closed

Comments

@fiotti
Copy link

fiotti commented Jun 17, 2024

Is your feature request related to a problem? Please describe.
All values are treated as strings, but numeric strings are not escaped, example:

ISerializer serializer = new SerializerBuilder().Build();
serializer.Serialize(new { Test = "1234" }); // output: "Test: 1234\r\n"

I think this does not directly violate YAML specs, but according to YAML specs, plain (unquoted) scalars may be interpreted in special ways. By serializing in this format we lost the information that the value was originally a string, now it looks more like an integer.

This might not be a big problem for most strings, but YAML specs gives an example of an octal number as a sequence of digits with a leading 0, as an example, the YAML value 014 is interpreted as "14 octal", which is 12.

serializer.Serialize(new { Test = "01234" }); // output: "Test: 01234\r\n"

Describe the solution you'd like
A more versatile approach would be to add quotes to strings that look like numbers.

serializer.Serialize(new { Test = "hello" }); // output: "Test: hello\r\n"
serializer.Serialize(new { Test = "01234" }); // output: "Test: '01234'\r\n"

Describe alternatives you've considered
I considered using a custom YamlFormatter, but there is no FormatAsString() method exposed, so this is not a viable solution.

I ended up wrapping in a custom QuotedString class all the strings that look like numbers, then using a custom IYamlTypeConverter to emit a Scalar with ScalarStyle.SingleQuoted.

Additional context
In my specific case I'm passing the output YAML to Helm, which interprets that 01234 value as the integer value 668 instead of as the string '01234', breaking some functionalities.

Thank you!

@EdwardCooke
Copy link
Collaborator

Try using withquotenecessarystrings on your serializerbuilder. That should automatically add quotes around scalars that are numbers only. Along with other special values.

@fiotti
Copy link
Author

fiotti commented Jul 3, 2024

Using WithAttemptingUnquotedStringTypeDeserialization() during deserialization and WithQuoteNecessaryStrings() during serialization does indeed solve the issue.

Thanks! I had completely missed it 😅

@fiotti fiotti closed this as completed Jul 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants