You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 GDscriptprint([10,20,30,40].find(30)) # gives 2print(parse_json(to_json([10,20,30,40])).find(30)) # gives -1# In JavascriptJSON.parse(JSON.stringify([10,20,30,40])).indexOf(30) # gives 2# In Pythonjson.loads(json.dumps([10,20,30,40])).index(30) # gives 2
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
Your Godot version:
3.2.2.stable
Issue description:
The documentation for
to_json()
gives the example: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.URL to the documentation page:
https://docs.godotengine.org/en/latest/classes/class_@gdscript.html#class-gdscript-method-to-json
The text was updated successfully, but these errors were encountered: