1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-04 12:00:25 +00:00

Handle NaN and Infinity in JSON stringify function

Co-authored-by: Thaddeus Crews <repiteo@outlook.com>
Co-authored-by: Lukas Tenbrink <lukas.tenbrink@gmail.com>
This commit is contained in:
Aaron Franke
2025-07-21 07:49:18 -07:00
parent 534591047e
commit cc13a376c5
3 changed files with 35 additions and 0 deletions

View File

@@ -76,6 +76,18 @@ void JSON::_stringify(String &r_result, const Variant &p_var, const String &p_in
case Variant::FLOAT: {
const double num = p_var;
// JSON does not support NaN or Infinity, so use extremely large numbers for infinity.
if (!Math::is_finite(num)) {
if (num == Math::INF) {
r_result += "1e99999";
} else if (num == -Math::INF) {
r_result += "-1e99999";
} else {
WARN_PRINT_ONCE("`NaN` (\"Not a Number\") found in argument passed to JSON.stringify(). `NaN` cannot be represented in JSON, so the value has been replaced with `null`. This warning will not be printed for any later NaN occurrences.");
r_result += "null";
}
return;
}
// Only for exactly 0. If we have approximately 0 let the user decide how much
// precision they want.
if (num == double(0.0)) {