Skip to content

Commit 8810580

Browse files
committed
Merge pull request #111522 from Repiteo/core/to-from-native
Core: Support `INF`/`NAN` in JSON from/to native
2 parents a0cde1e + b802229 commit 8810580

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

core/io/json.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1056,7 +1056,15 @@ Variant JSON::_to_native(const Variant &p_json, bool p_allow_objects, int p_dept
10561056
if (s.begins_with("i:")) {
10571057
return s.substr(2).to_int();
10581058
} else if (s.begins_with("f:")) {
1059-
return s.substr(2).to_float();
1059+
const String sub = s.substr(2);
1060+
if (sub == "inf") {
1061+
return Math::INF;
1062+
} else if (sub == "-inf") {
1063+
return -Math::INF;
1064+
} else if (sub == "nan") {
1065+
return Math::NaN;
1066+
}
1067+
return sub.to_float();
10601068
} else if (s.begins_with("s:")) {
10611069
return s.substr(2);
10621070
} else if (s.begins_with("sn:")) {

tests/core/io/test_json_native.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ TEST_CASE("[JSON][Native] Conversion between native and JSON formats") {
6060
// Numbers and strings (represented as JSON strings).
6161
test(1, R"("i:1")");
6262
test(1.0, R"("f:1.0")");
63+
test(Math::INF, R"("f:inf")");
64+
test(-Math::INF, R"("f:-inf")");
65+
test(Math::NaN, R"("f:nan")");
6366
test(String("abc"), R"("s:abc")");
6467
test(StringName("abc"), R"("sn:abc")");
6568
test(NodePath("abc"), R"("np:abc")");

0 commit comments

Comments
 (0)