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

.net 9 text.json rounds long numbers #110927

Open
MannusEtten opened this issue Dec 24, 2024 · 3 comments
Open

.net 9 text.json rounds long numbers #110927

MannusEtten opened this issue Dec 24, 2024 · 3 comments
Labels
area-System.Text.Json needs-author-action An issue or pull request that requires more info or actions from the author. untriaged New issue has not been triaged by the area owner

Comments

@MannusEtten
Copy link

Description

i do read the number 326051917762398058 into a long property part of an object
when i do Results.Ok(object) the long property is correctly transformed to a json number but rounded, in the json the number changes to 326051917762398100

Reproduction Steps

create a custom object
add long property to it
create a web api get request and return the custom object and for the property the value 326051917762398058

Expected behavior

the json should contain the number 326051917762398058 and not 326051917762398100

Actual behavior

gives back the wrong number

Regression?

I don't know

Known Workarounds

No response

Configuration

No response

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 Dec 24, 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.

@elgonzo
Copy link

elgonzo commented Dec 24, 2024

Since you mentioned you are working with web requests, i assume you inspect the produced/transmitted json in a web browser.

If that's the case, quite likely the json data is correct and intact and the rounding you observed is by the web browser rounding the value when presenting it or processing it in JavaScript. Different web browsers handle this differently, some might have a special viewing mode for "raw" json that doesn't alter the json text while viewing it. (The reason for web-browser-based rounding is most probably the json viewer in the browser to be JS-based and/or the numeric json values are parsed as double precision floating point values in JS.)

You can easily confirm this by yourself. Create a very simple json text file in a text editor with this long number 326051917762398058, and then view this json text file in the web browser you are using. If it doesn't show the accurate value you typed into the json text file, you got the evidence that the web browser json viewer is doing the rounding...


This is for example how Firefox does things when inspecting this simple example json in it:

{
	"value": 326051917762398058
}

When viewing it in json mode, note that the presented value is rounded, not correctly representing the actual json data:

Image

However, when viewing it in raw mode, note how the json is accurately displayed:

Image


Side note: If you intend to process json with Int64 / UInt64 values in Javascript, you will need to customize the client-side json parsing so that the respective numeric json values are using the BigInt type (or whatever other JS type that preserves Int64/UInt64 values accurately). Some web app frameworks might perhaps already handle this, but by default Javascripts json parsing assumes double-precision floating point for all numeric json values, which will result in (unwanted) rounding of very large absolute values.

@eiriktsarpalis
Copy link
Member

@MannusEtten does @elgonzo's response answer your question?

@eiriktsarpalis eiriktsarpalis added the needs-author-action An issue or pull request that requires more info or actions from the author. label Dec 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-System.Text.Json needs-author-action An issue or pull request that requires more info or actions from the author. untriaged New issue has not been triaged by the area owner
Projects
None yet
Development

No branches or pull requests

3 participants