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

to_json has a misleading example that does not warn you that parse_json will convert your ints to floats #3848

Closed
goatchurchprime opened this issue Jul 30, 2020 · 1 comment · Fixed by godotengine/godot#40903
Labels

Comments

@goatchurchprime
Copy link

Your Godot version:
3.2.2.stable

Issue description:
The documentation for to_json() gives the example:

a = { "a": 1, "b": 2 }
b = to_json(a)
print(b) # {"a":1, "b":2}

The documentation of this function is a good place to put a warning in bold that you're not going to get the same structure back when to apply parse_json() to this example, because all the integers will return as floats.

Unfortunately, by the way that GDscript works, when you print b it looks exactly the same (without trailing".0"s), so you'll never know it's gone wrong, and pretty soon there are invisible floating point values appearing deep in your data structures where you did not expect to be.

I've been badly burnt by this one. Python (on which GDscript has been inspired) does not do this. This conversion happens in Javascript, but that language is so flimsy with its type handling you expect problems everywhere.

I know this is intentional behavior (see issue 9499), but the inconsistency of not caring about the type distinction between int and float in some places and not others can raise a issues.
For example Array.find() function -- which respects types rather than values -- makes it a problem.

# In GDscript
print([10,20,30,40].find(30))   # gives 2
print(parse_json(to_json([10,20,30,40])).find(30))  # gives -1

# In Javascript
JSON.parse(JSON.stringify([10,20,30,40])).indexOf(30)  # gives 2

# In Python
json.loads(json.dumps([10,20,30,40])).index(30)   # gives 2

URL to the documentation page:
https://docs.godotengine.org/en/latest/classes/class_@gdscript.html#class-gdscript-method-to-json

@Calinou
Copy link
Member

Calinou commented Jul 31, 2020

Unfortunately, by the way that GDscript works, when you print b it looks exactly the same (without trailing".0"s), so you'll never know it's gone wrong, and pretty soon there are invisible floating point values appearing deep in your data structures where you did not expect to be.

This can be solved in 4.0 by changing the string conversions and print handlers for floats. (It might break backwards compatibility, so we can't do it in 3.2.x.)

Calinou added a commit to Calinou/godot that referenced this issue Jul 31, 2020
akien-mga pushed a commit to godotengine/godot that referenced this issue Jul 31, 2020
MarcusElg pushed a commit to MarcusElg/godot that referenced this issue Oct 19, 2020
huhund pushed a commit to huhund/godot that referenced this issue Nov 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants