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

Dictionary.parse_json() turns integers into floats. #9499

Closed
ducdetronquito opened this issue Jul 4, 2017 · 12 comments
Closed

Dictionary.parse_json() turns integers into floats. #9499

ducdetronquito opened this issue Jul 4, 2017 · 12 comments

Comments

@ducdetronquito
Copy link
Contributor

ducdetronquito commented Jul 4, 2017

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:

{
    "height": 4,
    "width": 4,
}

And here is the way I parse its content into a dictionary:

    var file = File.new()
    file.open("my_file.json", file.READ)
    
    var tmp_text = file.get_as_text()
    file.close()

    var tmp_dict = {}
    tmp_dict.parse_json(tmp_text)

    var height = tmp_dict['height']
    var width = tmp_dict['width']

    print(typeof(height))
    # 3
    print(typeof(width))
    # 3
@ducdetronquito ducdetronquito changed the title Dictionary.parse_json() turns integer into float. Dictionary.parse_json() turns integers into floats. Jul 4, 2017
@karroffel
Copy link
Contributor

Well, AFAIK there only are floating point numbers in JSON, so it's expected to return floats

@triverske
Copy link

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.

@punto-
Copy link
Contributor

punto- commented Jul 4, 2017 via email

@triverske
Copy link

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.

@punto-
Copy link
Contributor

punto- commented Jul 5, 2017 via email

@ducdetronquito
Copy link
Contributor Author

ducdetronquito commented Jul 5, 2017

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...

@reduz
Copy link
Member

reduz commented Jul 5, 2017 via email

@ducdetronquito
Copy link
Contributor Author

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 ?

@akien-mga
Copy link
Member

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 :)

@ducdetronquito
Copy link
Contributor Author

ducdetronquito commented Jul 6, 2017

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 😄

@guelinator
Copy link

But official JSON schema defines an integer-type.
So i really do disagree with everybody who claims that json only defines a "number".

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

@karroffel
Copy link
Contributor

karroffel commented Jun 26, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants