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
Dictionary.parse_json() turns integers into floats. #9499
Comments
Well, AFAIK there only are floating point numbers in JSON, so it's expected to return floats |
karroffel is correct, JSON only has a "number" type and not specific int or float types. (source) In order to keep Godot's compliance with the JSON standard I suggest you just int() your values, but someone else might have other ideas regarding this issue. |
but it's not like it's a mystery if the number is int or float, you can
make it int if it's "5" and float if it's "5.0", like in a gdscript
dictionary
…On 4 July 2017 at 18:03, triverske ***@***.***> wrote:
karroffel is correct, JSON only has a "number" type and not specific int
or float types. (source) <https://tools.ietf.org/html/rfc7159#section-6>
In order to keep Godot's compliance with the JSON standard I suggest you
just int() your values, but someone else might have other ideas regarding
this issue.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#9499 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGVmPX_m162gCl31gV8rLtLpT08eEwV0ks5sKqiMgaJpZM4ONj7t>
.
|
Making a guess may work in this specific instance, but eventually someone is going to come along wondering why their 5.0 floats are being converted to ints in the dictionaries. In my opinion we should just make a note in the documentation that turning a JSON into a dictionary will set all numerical values as floats and that you should write code accordingly. |
No, the "5.0" will be a float, the "5" will be an int. I'm saying keep the
same behavior we have on the gdscript parser, which we're already using and
it doesn't seem to be a problem..
…On 5 July 2017 at 02:40, triverske ***@***.***> wrote:
Making a guess may work in this specific instance, but eventually someone
is going to come along wondering why their 5.0 floats are being converted
to ints in the dictionaries. In my opinion we should just make a note in
the documentation that turning a JSON into a dictionary will set all
numerical values as floats and that you should write code accordingly.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#9499 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AGVmPRxXQLHB8mJUuUg1JuaR8SNmkVE_ks5sKyHFgaJpZM4ONj7t>
.
|
Thank you all, I didn't know that part of the JSON spec 😳 Could be cool to have this feature by the way ! PS: Do you have any idea where the code of .parse_json() is ? I can't promise to make a pull-request for this, but who knows... |
it's simply safer to assume everything is floats
…On Wed, Jul 5, 2017 at 8:38 AM, ducdetronquito ***@***.***> wrote:
Thanks you all, I didn't know that part of the JSON spec 😳
Could be cool to have this feature by the way !
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
<#9499 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AF-Z27doxeV7711p-vXi3RfmFnn2FYXDks5sK3W6gaJpZM4ONj7t>
.
|
Fine for me. Like @triverske said, would it be interresting to mention this behavior (even if it's the correct one) in the parse_json() documentation ? |
Definitely. We can assume that many Godot users will use the JSON API without necessary being experts on the JSON spec :) |
Hi there ! I made a PR to improve the documentation of Dictionary.parse_json() regarding how numerical values are converted: reviews welcomed ! Have a great day 😄 |
But official JSON schema defines an integer-type. https://json-schema.org/understanding-json-schema/reference/numeric.html#integer with all the rounding issues i really would prefer, that any number without a . is treated as a integer |
JSON-schema != JSON. The JSON spec is really small and does only define a "number" type. JSON-schema is a kind of description language embedded in JSON that you might or might not use. The "What is a schema" page even mentions that there is only a number type in JSON and no distinction between floats and ints. |
Operating system or device - Godot version:
Godot 2.1.2 - Ubuntu 17.10
Issue description:
Hi everyone !
When I turn a JSON file into a GDScript dictionary, every integer values are turned into floats.
Are you able to reproduce it with the following example ?
Steps to reproduce:
Here is an JSON file example:
And here is the way I parse its content into a dictionary:
The text was updated successfully, but these errors were encountered: