From b8022294231467f188f8d92e29b5be1c4a5b2737 Mon Sep 17 00:00:00 2001 From: Thaddeus Crews Date: Sat, 11 Oct 2025 14:49:37 -0500 Subject: [PATCH] Core: Support `INF`/`NAN` in JSON from/to native --- core/io/json.cpp | 10 +++++++++- tests/core/io/test_json_native.h | 3 +++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/core/io/json.cpp b/core/io/json.cpp index ffc9efb26e5..34b5696df13 100644 --- a/core/io/json.cpp +++ b/core/io/json.cpp @@ -1056,7 +1056,15 @@ Variant JSON::_to_native(const Variant &p_json, bool p_allow_objects, int p_dept if (s.begins_with("i:")) { return s.substr(2).to_int(); } else if (s.begins_with("f:")) { - return s.substr(2).to_float(); + const String sub = s.substr(2); + if (sub == "inf") { + return Math::INF; + } else if (sub == "-inf") { + return -Math::INF; + } else if (sub == "nan") { + return Math::NaN; + } + return sub.to_float(); } else if (s.begins_with("s:")) { return s.substr(2); } else if (s.begins_with("sn:")) { diff --git a/tests/core/io/test_json_native.h b/tests/core/io/test_json_native.h index f5b6a423f2e..f6e067744a3 100644 --- a/tests/core/io/test_json_native.h +++ b/tests/core/io/test_json_native.h @@ -60,6 +60,9 @@ TEST_CASE("[JSON][Native] Conversion between native and JSON formats") { // Numbers and strings (represented as JSON strings). test(1, R"("i:1")"); test(1.0, R"("f:1.0")"); + test(Math::INF, R"("f:inf")"); + test(-Math::INF, R"("f:-inf")"); + test(Math::NaN, R"("f:nan")"); test(String("abc"), R"("s:abc")"); test(StringName("abc"), R"("sn:abc")"); test(NodePath("abc"), R"("np:abc")");