-
-
Notifications
You must be signed in to change notification settings - Fork 21.3k
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
JSON integers are parsed as float #61464
Comments
As per its specification, JSON does not have an integer or float type. It only has a number type, so all JSON numbers are parsed as floats for compatibility reasons. |
If it hasn't been noted down already, it should probably be specified in the documentation, even if it... typically doesn't matter much? If assigned to integers, floats are casted automatically. |
To be honest I was not aware of this since all langages I worked with preserve type for numbers. Since it's by design in JSON spec I think we can close this issue. @Mickeon for the context I'm writing and API description where a type is expected (I use something inspired by JSON Schema) but I'll make a "number" type which match both. |
This is already documented here: Line 67 in 8a21f72
Do you know of other places where this information could be added? |
Not really, JSON.print() makes note of it, as well as JSONParseResult and parse_json(). Really seems like that's it? |
Some languages like Python will make the number a |
Just ran into this one today when parsing glTF node indices and finding out they were all floats. For example, currently if I attempt to call It is common practice in many JSON libraries to decode values as int64 if they do not contain a decimal place, and provided that the value fits in an int64. I understand the default behavior can't be changed for compat breakage, but it might be nice to consider adding an option the JSON class to permit decoding numbers as int64. Should this be an implementation proposal? |
Yes, if you think there is a use case for such a flag, you need to make a proposal. And this issue should be closed by now, as there is nothing left to discuss: JSON doesn't support integers.
PS. In this case the default should not be changed because it would produce unpredictable results and violate the specification. Consider an array of floating point values where some of them are perfectly valid integers. You don't expect to get a mixed array out of this as a user. So the default behavior should be the least unpredictable one. |
I just encountered this issue because I was reading data from JSON file and tried to use it to get values out of a Dictionary.
|
No that I know of, but as already established this is not a bug or even lacking details, nothing is broken here, this is as expected, and based on how JSON works JSON is intended as a format to communicate with different software, so it needs to be standardized, and we need to follow that standard as well as we can in Godot, if you need to store data for Godot only use some other format like Please open a proposal if you are interested in this, otherwise please don't revive this thread as there's nothing further to do here 🙂 |
Hi. I recently encountered this issue. I have 0 as a value in my json file. then I put this value inside an array and I tried to check if 0 was in it. It wasn't. It took me quite some time to find out that the parsing casted it as a float, so 0 wasn't in it, 0.0 was. I think adding a warning or a note in the parse_string function could be useful so that this kind of issues doesn't lead people to this closed issue :) . |
It's documented, and there wouldn't be possible to add a warning for this, it couldn't tell that you didn't really want to do that check, and it can't know what data is there without adding some specific code just for that, as it just knows it's some data The parsing doesn't cast it to a float, it was a float, JSON only stores floats |
I see, however I think it could be helpful to mention this. We already mention some specs that can be misleading, for instance csv escape of the doublequotes. "Double quotes within a text value can be escaped by doubling their occurrence." https://docs.godotengine.org/en/stable/classes/class_fileaccess.html#class-fileaccess-method-get-csv-line |
Personally I think Godot should improve its handling of JSON numbers which are integer. Many other languages parse JSON numbers without decimal points as int64, but this needs to be discussed as a proposal, not a bug issue. Please see here for the proposal: As for the equality of 0 and 0.0 within an array, I do think that is something that GDScript could improve, but it would be very difficult to do this without compatibility break, so again this is something that can improve in terms of documentation or needs a proposal |
If all non-typed array methods accounted for this they would be slowed down by a pretty large margin. The simpler workaround involves making use of static typing whenever applicable. |
the problem is arrays and dictionaries read from JSON won't use static typing, so there is no way to trivially cast a loaded JSON Dictionary into a particular schema Case in point, if I want to quickly scan the "children" array in a glTF node for a particular node index (integer), that will fail due to this issue because children are parsed as floats But I think the best mitigation is to implement the above proposal 8830 to allow parsing integers from JSON documents as int64. It will solve most of the common cases of type mismatch. |
Godot version
v4.0.alpha8.official [cc3ed63]
System information
Windows 10 64bit - Vulkan API 1.2.141 - GeForce GTX 1660 SUPER
Issue description
int
are converted tofloat
withJson.parse()
method.Godot 3.4 and 4.0 affected.
Steps to reproduce
See code in "Minimal reproduction project"
Minimal reproduction project
Based on documentation example (https://docs.godotengine.org/en/latest/classes/class_json.html):
Output:
Godot 3.4 poc:
The text was updated successfully, but these errors were encountered: