-
Notifications
You must be signed in to change notification settings - Fork 10.3k
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
[System.Text.Json] Serializer loses precision on ulong #52393
Comments
Tagging subscribers to this area: @dotnet/area-system-text-json, @gregsdennis Issue DetailsDescriptionSerializing an ulong to JSON within asp.net core loses precision. Stumbled over this while upgrading an older project from traditional ASPnet to .net core 8. Reproduction StepsUsing .net 8 and System.Text.Json 8.0.0.
Exposing data via minimal API
The result in the browser is the following:
Expected behavior
Actual behavior
Regression?No response Known WorkaroundsFor the moment I have written a custom JsonConverter which calls ToString for the ulong myself. Configuration.net 8, running x64 (emulated on win dev kit arm64) on Windows 11. System.Text.Json 8.0.0 Other informationNo response
|
Javascript number is defined as double. See dotnet/runtime#60780 (comment) and related discussions. |
@huoyaoyuan JavaScript numbers, sure, but this is JSON, in which numbers are arbitrary-precision. I would expect the full value (in the big-int case as well). |
EDIT: As it turned out (see below), the issue is not so much with ASP.NET but rather with imprecise json number presentation in the MS Edge browser. Uff... It appears to be ASP.NET Core's doing, employing a custom/specific STJ configuration serialization. STJ itself has no problem serializing ulong's correctly (https://dotnetfiddle.net/l248U1). I therefore would not see it as a problem of STJ, but an issue related to ASP.NET Core's serialization configuration. |
It seems to be aspnetcore specific, I'm not sure why though. Transferring to the relevant repo for triage. |
Thanks for reporting this issue, @queequac. I appreciate your patience as I work through the backlog of bug reports here. I, unfortunately, wasn't able to repro this locally with the following sample using either both RDG (dynamic code-gen) or RDF (static code-gen). var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
ulong number = 2121241830635120343;
app.MapGet("/primitive", () => number);
app.MapGet("/complex", () => new Test { Key = number });
app.Run();
class Test
{
public ulong Key { get; set; }
} You mentioned that you discovered this problem while migrating a previous codebase. Is the repro missing a detail that only shows up in the code you are migrating? |
I wonder if this is the same issue as #55571? |
Hi @captainsafia , thanks for reaching out. Actually I can easily reproduce the issue with an empty net8.0 web api project from scratch:
Same with your sample above, it does not return 2121241830635120343 but 2121241830635120400 for /primitive |
@martincostello Oh wow, thanks for the hint. |
Not sure where you're suggesting I raise an issue, or for what. |
You mentioned above
In this case handling json-documents in edge's rendering should maybe also not lose precision. Anyway, personally no hard feelings about this.🙃 |
Okay, but JSON and JS number handling are inherently defined differently. While JSON numbers are theoretically unlimited in scale and precision, JS does define those limitations, so if you're interpreting JSON through JS, it's totally understandable that you'll encounter those limits. There is no issue that I can file in any repo that's going to help this. My point in that comment was that if this were an STJ problem, then it should serialize the The behavior of Asp.Net could do what it feels is right given its domain is web services. |
You have completely misunderstood me. :) Anyway, nothing else to discuss here. |
Description
Serializing an ulong to JSON within asp.net core loses precision. Stumbled over this while upgrading an older project from traditional ASPnet to .net core 8.
Reproduction Steps
Using .net 8 and System.Text.Json 8.0.0.
Exposing data via minimal API
The result in the browser is the following:
Expected behavior
Actual behavior
Regression?
No response
Known Workarounds
For the moment I have written a custom JsonConverter which calls ToString for the ulong myself.
Configuration
.net 8, running x64 (emulated on win dev kit arm64) on Windows 11.
System.Text.Json 8.0.0
Other information
No response
The text was updated successfully, but these errors were encountered: