-
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
Incorrect Serialization of long (Int64) Values in ASP.NET Core Web API #55571
Comments
I think this is essentially a duplicate of #16117 (comment). Try setting |
Yes this can be a workaround: JsonSerializerOptions options = new()
{
NumberHandling = JsonNumberHandling.Strict,
WriteIndented = true
};
return Ok(JsonSerializer.Serialize(value, options)); But exactly when will the problem with Ok(object) be solved? |
AFAIK services.AddControllers()
.AddJsonOptions(options =>
{
options.NumberHandling = JsonNumberHandling.WriteAsString;
}); Changing the defaults for everyone would likely be a breaking change. |
But I don't want to use longValue as a string. So I tried to set it up as you said but it didn't work:
|
If you don't want to use a string for a The value would just wrap around and become negative (or throw an |
I think it uses the maximum value of the long and not more than it, but when this value is passed through the OK() method |
Have you tried it yourself? It will be same result with ulong |
I haven't, no, I'm just giving you suggestions. |
Since this: [System.Text.Json] Serializer loses precision on ulong #52393 issue has been resolved, this issue has also been resolved |
The problem is still present
result on browser = -5228845161660334000 |
The imprecision is in the browser, not in ASP.NET Core. In JavaScript, all numbers are double-precision floating point. Ask your favorite REPL to evaluate |
Is there an existing issue for this?
Describe the bug
When returning a Value object containing a long property from ASP.NET Core Web API controllers using the OkObjectResult, the serialization of big long values like Int64.MaxValue is incorrect. This leads to loss of precision in the returned JSON response.
Expected Behavior
The JSON response should accurately represent the long value assigned to the LongValue property of the Value object.
Steps To Reproduce
long
property, such asValue
in the provided code snippet.OkObjectResult
).long
valueInt64.MaxValue
to thelong
property of the model.Exceptions (if any)
The JSON response shows a loss of precision in the long value, with the last digits being incorrect, especially when the long value exceeds Int64.MaxValue.
.NET Version
8.0.204
Anything else?
Example code:
The text was updated successfully, but these errors were encountered: