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

Double serialization in net481 accuracy problems #110276

Closed
gregsdennis opened this issue Nov 29, 2024 · 2 comments
Closed

Double serialization in net481 accuracy problems #110276

gregsdennis opened this issue Nov 29, 2024 · 2 comments
Labels
area-System.Text.Json tracking-external-issue The issue is caused by external problem (e.g. OS) - nothing we can do to fix it directly

Comments

@gregsdennis
Copy link
Contributor

Description

Using System.Text.Json serialization for double values serializes inaccurately in .Net Framework versions.

Note that it works fine in .Net Core+ versions.

Reproduction Steps

[Test]
public void RawValue()
{
    var expected = "4.2";

    var actual = JsonSerializer.Serialize(4.2, _options);

    Assert.That(actual, Is.EqualTo(expected)); // actual: 4.2000000000000002
}

Expected behavior

I expect that it would serialize to 4.2 as that's the original value, and as it works in .Net Core+ versions.

Actual behavior

double serializes to an approximation.

Regression?

Unknown, guessing not.

Known Workarounds

None.

Configuration

Built and run under .Net 9.0.100 on Windows 11 x86
Using System.Text.Json 9.0.0

Other information

No response

@dotnet-policy-service dotnet-policy-service bot added the untriaged New issue has not been triaged by the area owner label Nov 29, 2024
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis
See info in area-owners.md if you want to be subscribed.

@tannergooding
Copy link
Member

The behavior of floating-point parsing and formatting in .NET Framework has essentially always been buggy, but it can't be fixed due to the much higher compatibility bar that exists.

This is covered in more detail as part of https://devblogs.microsoft.com/dotnet/floating-point-parsing-and-formatting-improvements-in-net-core-3-0/, which details the breaking change that was made to fix this behavior in .NET Core 3.0 and later.

@jkotas jkotas added the tracking-external-issue The issue is caused by external problem (e.g. OS) - nothing we can do to fix it directly label Nov 30, 2024
@eiriktsarpalis eiriktsarpalis closed this as not planned Won't fix, can't repro, duplicate, stale Nov 30, 2024
@dotnet-policy-service dotnet-policy-service bot removed the untriaged New issue has not been triaged by the area owner label Nov 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Text.Json tracking-external-issue The issue is caused by external problem (e.g. OS) - nothing we can do to fix it directly
Projects
None yet
Development

No branches or pull requests

4 participants