Skip to content

Commit b626695

Browse files
committed
Fix internal JSON stringify not preserving p_full_precision
1 parent 71a9948 commit b626695

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed

core/io/json.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,18 +73,18 @@ void JSON::_stringify(String &r_result, const Variant &p_var, const String &p_in
7373
r_result += itos(p_var);
7474
return;
7575
case Variant::FLOAT: {
76-
double num = p_var;
76+
const double num = p_var;
7777

7878
// Only for exactly 0. If we have approximately 0 let the user decide how much
7979
// precision they want.
80-
if (num == double(0)) {
80+
if (num == double(0.0)) {
8181
r_result += "0.0";
8282
return;
8383
}
8484

85-
double magnitude = std::log10(Math::abs(num));
86-
int total_digits = p_full_precision ? 17 : 14;
87-
int precision = MAX(1, total_digits - (int)Math::floor(magnitude));
85+
const double magnitude = std::log10(Math::abs(num));
86+
const int total_digits = p_full_precision ? 17 : 14;
87+
const int precision = MAX(1, total_digits - (int)Math::floor(magnitude));
8888

8989
r_result += String::num(num, precision);
9090
return;
@@ -120,7 +120,7 @@ void JSON::_stringify(String &r_result, const Variant &p_var, const String &p_in
120120
r_result += end_statement;
121121
}
122122
_add_indent(r_result, p_indent, p_cur_indent + 1);
123-
_stringify(r_result, var, p_indent, p_cur_indent + 1, p_sort_keys, p_markers);
123+
_stringify(r_result, var, p_indent, p_cur_indent + 1, p_sort_keys, p_markers, p_full_precision);
124124
}
125125
r_result += end_statement;
126126
_add_indent(r_result, p_indent, p_cur_indent);
@@ -154,9 +154,9 @@ void JSON::_stringify(String &r_result, const Variant &p_var, const String &p_in
154154
r_result += end_statement;
155155
}
156156
_add_indent(r_result, p_indent, p_cur_indent + 1);
157-
_stringify(r_result, String(key), p_indent, p_cur_indent + 1, p_sort_keys, p_markers);
157+
_stringify(r_result, String(key), p_indent, p_cur_indent + 1, p_sort_keys, p_markers, p_full_precision);
158158
r_result += colon;
159-
_stringify(r_result, d[key], p_indent, p_cur_indent + 1, p_sort_keys, p_markers);
159+
_stringify(r_result, d[key], p_indent, p_cur_indent + 1, p_sort_keys, p_markers, p_full_precision);
160160
}
161161

162162
r_result += end_statement;

core/io/json.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ class JSON : public Resource {
7272
static const char *tk_name[];
7373

7474
static void _add_indent(String &r_result, const String &p_indent, int p_size);
75-
static void _stringify(String &r_result, const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys, HashSet<const void *> &p_markers, bool p_full_precision = false);
75+
static void _stringify(String &r_result, const Variant &p_var, const String &p_indent, int p_cur_indent, bool p_sort_keys, HashSet<const void *> &p_markers, bool p_full_precision);
7676
static Error _get_token(const char32_t *p_str, int &index, int p_len, Token &r_token, int &line, String &r_err_str);
7777
static Error _parse_value(Variant &value, Token &token, const char32_t *p_str, int &index, int p_len, int &line, int p_depth, String &r_err_str);
7878
static Error _parse_array(Array &array, const char32_t *p_str, int &index, int p_len, int &line, int p_depth, String &r_err_str);

tests/core/io/test_json.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ TEST_CASE("[JSON] Stringify arrays") {
7171
indented_array.push_back(nested_array);
7272
CHECK(JSON::stringify(indented_array, "\t") == "[\n\t0,\n\t1,\n\t2,\n\t3,\n\t4,\n\t[\n\t\t0,\n\t\t1,\n\t\t2,\n\t\t3,\n\t\t4\n\t]\n]");
7373

74+
Array full_precision_array;
75+
full_precision_array.push_back(0.123456789012345677);
76+
CHECK(JSON::stringify(full_precision_array, "", true, true) == "[0.123456789012345677]");
77+
7478
ERR_PRINT_OFF
7579
Array self_array;
7680
self_array.push_back(self_array);
@@ -105,6 +109,10 @@ TEST_CASE("[JSON] Stringify dictionaries") {
105109
outer["inner"] = inner;
106110
CHECK(JSON::stringify(outer) == "{\"inner\":{\"key\":\"value\"}}");
107111

112+
Dictionary full_precision_dictionary;
113+
full_precision_dictionary["key"] = 0.123456789012345677;
114+
CHECK(JSON::stringify(full_precision_dictionary, "", true, true) == "{\"key\":0.123456789012345677}");
115+
108116
ERR_PRINT_OFF
109117
Dictionary self_dictionary;
110118
self_dictionary["key"] = self_dictionary;

0 commit comments

Comments
 (0)