YamlDotNet icon indicating copy to clipboard operation
YamlDotNet copied to clipboard

Serialization of numeric valid strings

Open Narazaka opened this issue 8 years ago • 4 comments
trafficstars

Is there a way to serialize a string that is valid as a number, like "001122" and "1_2_3", as a string?

I tryed as below

var builder = new SerializerBuilder();
builder.EmitDefaults();
var serializer = builder.Build();
var dic = new Dictionary<string, object> {
  {"key", "001122"},
  {"key2", "1_2_3"},
};
Console.WriteLine(serializer.Serialize(dic));

and output was

key: 001122
key2: 1_2_3

then other yaml parser recognized as below (because of YAML specification)

js-yaml <<EOM
key: 001122
key2: 1_2_3
EOM
{
  "key": 594,
  "key2": 123
}

How can I serialize them as below?

key: "001122"
key2: "1_2_3"

I read YamlDotNet/Core/Emitter.cs but I have no good idea. (no source value type check?)

Narazaka avatar Jun 22 '17 08:06 Narazaka

I don't think that it is currently possible to change this behavior. But I agree that it should be fixed. I'll assign myself to it and see what can be done.

aaubry avatar Sep 28 '17 21:09 aaubry

Are there any updates to this?

rohankhandelwal avatar Jun 27 '18 19:06 rohankhandelwal

I created my own implementation of a ChainedEventEmitter based on feedback from StackOverflow to handle strings that parse to int's and force quotes around them. To bad this issue has been open for so long.

Psilax avatar Apr 01 '21 04:04 Psilax

3.5 years later and still no fix? :(

joepb avatar Apr 20 '21 08:04 joepb

This issue was resolved in the 12.x releases.

EdwardCooke avatar Jan 15 '23 03:01 EdwardCooke