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

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

Open
filzrev opened this issue Oct 26, 2024 · 0 comments

Comments

@filzrev
Copy link
Contributor

filzrev commented Oct 26, 2024

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

3 participants
@filzrev and others