X Tutup
Skip to content

Use num_scientific (Grisu2) when stringifying JSON with full precision#108836

Merged
Repiteo merged 1 commit intogodotengine:masterfrom
aaronfranke:json-grisu
Sep 30, 2025
Merged

Use num_scientific (Grisu2) when stringifying JSON with full precision#108836
Repiteo merged 1 commit intogodotengine:masterfrom
aaronfranke:json-grisu

Conversation

@aaronfranke
Copy link
Member

@aaronfranke aaronfranke commented Jul 21, 2025

The JSON specification supports scientific notation for numbers. https://www.json.org/json-en.html

This PR changes the behavior of the JSON stringify function so that when the p_full_precision flag is true, it uses num_scientific, and therefore the Grisu2 algorithm introduced in PR #98750.

There has been a bug for awhile where glTF files exported from Godot are reported as invalid by the glTF validator because the accessor min and max values do not match the data in the accessor:

Screenshot 2025-07-21 at 7 04 00 AM

I've known about this bug for years, but it hasn't been a priority. I decided to solve it, and turns out, it's not a bug with the glTF exporter, it's a bug with Godot's JSON stringify function. This PR fixes that function, and also adjusts the glTF code to set p_full_precision to true. With this PR, the exported glTF file passes validation:

Screenshot 2025-07-21 at 8 53 26 AM

Also: In most cases, this approach can be way more efficient. 1.7976931348623157e+308 is much shorter than 179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0

Copy link
Member

@Calinou Calinou left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code looks good to me.

@aaronfranke aaronfranke marked this pull request as ready for review July 26, 2025 01:04
@aaronfranke aaronfranke requested review from a team as code owners July 26, 2025 01:04
@DjSapsan
Copy link

JSON.parse_string also incorrectly parsing huge integers. For example 109775241382906655 becomes 109775241382906660.
Is it possible to fix?

@aaronfranke
Copy link
Member Author

@DjSapsan No, it's not possible, because that is beyond what can be represented by double.

@Repiteo Repiteo merged commit e256771 into godotengine:master Sep 30, 2025
20 checks passed
@Repiteo
Copy link
Contributor

Repiteo commented Sep 30, 2025

Thanks!

@aaronfranke aaronfranke deleted the json-grisu branch September 30, 2025 16:44
@akien-mga akien-mga changed the title Use num_scientific (Grisu2) when stringifying JSON with full precision Use num_scientific (Grisu2) when stringifying JSON with full precision Jan 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

X Tutup