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 cannot parse int if it looks like a float 123.0
#40596
Comments
Per the spec JSON does not distinguish between floats and integers, there are only numbers. So looks like this is a spec violation. |
@poizan42 The JSON spec does not say anything about how to map JSON values to the .Net type system. |
@svick nope, but 123.0 and 123 are the same number. Since it's the same value in JSON, whatever .NET does to map them it must give the same result for both. There simply isn't any provision that allows for a parser to handle them differently. If you want to map a json number to a .net Int32 then that mapping must act on the json number and not on its textual representation. |
This is only true for a subset of numbers, not all numbers. For example, If we go strictly by the "de facto" JSON implementation, which is Javascript; there are not integrals only |
Additionally worth noting, the official JSON spec: http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf, details:
It explicitly does not specify how the values should be interpreted and instead just gives a sequence of digits that is up to the individual language to interpret and expose as necessary. |
No, they are both the same double value 1*2^53? But at least RFC8259 specifically allows for limited precision:
ECMA-404 seems to allow for more freedom in how implementations interpret numbers than RFC8259, or at least more clearly states it, still I'm not seeing it justified anywhere that the mere existence of a decimal point can be used to decide which internal representation to use. Interpretations of specifications aside, if the goal is to maximize interoperability then it might be better if internal representation was decided based on which datatype is requested or which one best holds the value rather than on the presence of a decimal point. |
.... okay, then |
Throws an exception indicating that the number is out of range. |
I think it's the opposite: I'm not seeing it mandated anywhere that the existence of a decimal point can't be used to decide which representation to use. Also, that interpretation wouldn't work well for |
I'd be inclined to close this as by-design behavior. Per @svick's original comment this is an issue of mapping JSON to .NET types rather than a spec conformance concern. Recommendation is to either update the model to something that permits floating point representations or register a custom converter for |
The following throws an exception:
Is there a way to prevent the exception since
123.0
is still an integer mathematically?If there is not a solution already, maybe this should be a case in
JsonNumberHandling
#30255I am using netcoreapp3.1
For additional context, I'm calling an API documented to return an
int32
field, however the JSON response has2808.0
. It's always an integer but just contains a dot zero. Is the API violating their documentation? I don't know I just want to parse the JSON. So looks like I'll be using a customJsonConverter<int>
.RiotGames/developer-relations#345
MingweiSamuel/Camille#39
The text was updated successfully, but these errors were encountered: