YamlDotNet icon indicating copy to clipboard operation
YamlDotNet copied to clipboard

Loss of precision when deserializing floating numbers with ‘WithAttemptingUnquotedStringTypeDeserialization`

Open filzrev opened this issue 1 year ago • 0 comments

Describe the bug float.Parse method accept number string with higher precision than can be represented with the float type.

So currently when deserialize YAML with WithAttemptingUnquotedStringTypeDeserialization
floating number is always parsed as float type

https://github.com/aaubry/YamlDotNet/blob/master/YamlDotNet/Serialization/NodeDeserializers/ScalarNodeDeserializer.cs#L390-L391

To Reproduce

[Fact]
public void DeserializationTest()
{
    var serializer = new SerializerBuilder().Build();
    var deserializer = new DeserializerBuilder().WithAttemptingUnquotedStringTypeDeserialization().Build();

    // Arrange
    double value = 1.2345678901234567d;
    var yaml = $"Value: {value:G17}"; // Value: 1.2345678901234567

    // Act
    var dict = deserializer.Deserialize<IDictionary<string, object>>(yaml);
    var yamlRoundTrip = serializer.Serialize(dict);

    // Assert
    var result = dict["Value"];
    result.Should().Be(value);   // NG: deserialized as `1.2345679`
}

filzrev avatar Oct 26 '24 02:10 filzrev