From 21a9fd4c8b9fcb5cea5ee052bae756c288ae8cb3 Mon Sep 17 00:00:00 2001 From: FireCatMagic Date: Wed, 16 Oct 2024 21:53:21 -0400 Subject: [PATCH] Ensure JSON::stringify outputs valid JSON when using inf and nan --- core/io/json.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/core/io/json.cpp b/core/io/json.cpp index 664ff7857b6..9c122bcefbd 100644 --- a/core/io/json.cpp +++ b/core/io/json.cpp @@ -31,6 +31,7 @@ #include "json.h" #include "core/config/engine.h" +#include "core/math/math_funcs.h" #include "core/string/print_string.h" const char *JSON::tk_name[TK_MAX] = { @@ -70,6 +71,11 @@ String JSON::_stringify(const Variant &p_var, const String &p_indent, int p_cur_ return itos(p_var); case Variant::FLOAT: { double num = p_var; + + if (!Math::is_finite(num)) { + return "\"" + String::num(num) + "\""; + } + if (p_full_precision) { // Store unreliable digits (17) instead of just reliable // digits (14) so that the value can be decoded exactly.