aaubry / YamlDotNet

YamlDotNet is a .NET library for YAML
MIT License
2.58k stars 486 forks source link

Loss of precision when deserializing floating numbers with ‘WithAttemptingUnquotedStringTypeDeserialization` #1001

Open filzrev opened 1 month ago

filzrev commented 1 month ago

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`
}