Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/tests/core/io/test_json.h
20784 views
1
/**************************************************************************/
2
/* test_json.h */
3
/**************************************************************************/
4
/* This file is part of: */
5
/* GODOT ENGINE */
6
/* https://godotengine.org */
7
/**************************************************************************/
8
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10
/* */
11
/* Permission is hereby granted, free of charge, to any person obtaining */
12
/* a copy of this software and associated documentation files (the */
13
/* "Software"), to deal in the Software without restriction, including */
14
/* without limitation the rights to use, copy, modify, merge, publish, */
15
/* distribute, sublicense, and/or sell copies of the Software, and to */
16
/* permit persons to whom the Software is furnished to do so, subject to */
17
/* the following conditions: */
18
/* */
19
/* The above copyright notice and this permission notice shall be */
20
/* included in all copies or substantial portions of the Software. */
21
/* */
22
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29
/**************************************************************************/
30
31
#pragma once
32
33
#include "core/io/json.h"
34
35
#include "thirdparty/doctest/doctest.h"
36
37
namespace TestJSON {
38
39
TEST_CASE("[JSON] Stringify single data types") {
40
CHECK(JSON::stringify(Variant()) == "null");
41
CHECK(JSON::stringify(false) == "false");
42
CHECK(JSON::stringify(true) == "true");
43
CHECK(JSON::stringify(0) == "0");
44
CHECK(JSON::stringify(12345) == "12345");
45
CHECK(JSON::stringify(0.75) == "0.75");
46
CHECK(JSON::stringify("test") == "\"test\"");
47
CHECK(JSON::stringify("\\\b\f\n\r\t\v\"") == "\"\\\\\\b\\f\\n\\r\\t\\v\\\"\"");
48
}
49
50
TEST_CASE("[JSON] Stringify arrays") {
51
CHECK(JSON::stringify(Array()) == "[]");
52
53
Array int_array;
54
for (int i = 0; i < 10; i++) {
55
int_array.push_back(i);
56
}
57
CHECK(JSON::stringify(int_array) == "[0,1,2,3,4,5,6,7,8,9]");
58
59
Array str_array;
60
str_array.push_back("Hello");
61
str_array.push_back("World");
62
str_array.push_back("!");
63
CHECK(JSON::stringify(str_array) == "[\"Hello\",\"World\",\"!\"]");
64
65
Array indented_array;
66
Array nested_array;
67
for (int i = 0; i < 5; i++) {
68
indented_array.push_back(i);
69
nested_array.push_back(i);
70
}
71
indented_array.push_back(nested_array);
72
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]");
73
74
Array full_precision_array;
75
full_precision_array.push_back(0.12345678901234568);
76
CHECK(JSON::stringify(full_precision_array, "", true, true) == "[0.12345678901234568]");
77
78
Array non_finite_array;
79
non_finite_array.push_back(Math::INF);
80
non_finite_array.push_back(-Math::INF);
81
non_finite_array.push_back(Math::NaN);
82
ERR_PRINT_OFF
83
CHECK(JSON::stringify(non_finite_array) == "[1e99999,-1e99999,null]");
84
85
Array non_finite_round_trip = JSON::parse_string(JSON::stringify(non_finite_array));
86
CHECK(non_finite_round_trip[0] == Variant(Math::INF));
87
CHECK(non_finite_round_trip[1] == Variant(-Math::INF));
88
CHECK(non_finite_round_trip[2].get_type() == Variant::NIL);
89
90
Array self_array;
91
self_array.push_back(self_array);
92
CHECK(JSON::stringify(self_array) == "[\"[...]\"]");
93
self_array.clear();
94
95
Array max_recursion_array;
96
for (int i = 0; i < Variant::MAX_RECURSION_DEPTH + 1; i++) {
97
Array next;
98
next.push_back(max_recursion_array);
99
max_recursion_array = next;
100
}
101
CHECK(JSON::stringify(max_recursion_array).contains("[...]"));
102
ERR_PRINT_ON
103
}
104
105
TEST_CASE("[JSON] Stringify dictionaries") {
106
CHECK(JSON::stringify(Dictionary()) == "{}");
107
108
Dictionary single_entry;
109
single_entry["key"] = "value";
110
CHECK(JSON::stringify(single_entry) == "{\"key\":\"value\"}");
111
112
Dictionary indented;
113
indented["key1"] = "value1";
114
indented["key2"] = 2;
115
CHECK(JSON::stringify(indented, "\t") == "{\n\t\"key1\": \"value1\",\n\t\"key2\": 2\n}");
116
117
Dictionary outer;
118
Dictionary inner;
119
inner["key"] = "value";
120
outer["inner"] = inner;
121
CHECK(JSON::stringify(outer) == "{\"inner\":{\"key\":\"value\"}}");
122
123
Dictionary full_precision_dictionary;
124
full_precision_dictionary["key"] = 0.12345678901234568;
125
CHECK(JSON::stringify(full_precision_dictionary, "", true, true) == "{\"key\":0.12345678901234568}");
126
127
Dictionary non_finite_dictionary;
128
non_finite_dictionary["-inf"] = -Math::INF;
129
non_finite_dictionary["inf"] = Math::INF;
130
non_finite_dictionary["nan"] = Math::NaN;
131
ERR_PRINT_OFF
132
CHECK(JSON::stringify(non_finite_dictionary) == "{\"-inf\":-1e99999,\"inf\":1e99999,\"nan\":null}");
133
134
Dictionary non_finite_round_trip = JSON::parse_string(JSON::stringify(non_finite_dictionary));
135
CHECK(non_finite_round_trip["-inf"] == Variant(-Math::INF));
136
CHECK(non_finite_round_trip["inf"] == Variant(Math::INF));
137
CHECK(non_finite_round_trip["nan"].get_type() == Variant::NIL);
138
139
Dictionary self_dictionary;
140
self_dictionary["key"] = self_dictionary;
141
CHECK(JSON::stringify(self_dictionary) == "{\"key\":\"{...}\"}");
142
self_dictionary.clear();
143
144
Dictionary max_recursion_dictionary;
145
for (int i = 0; i < Variant::MAX_RECURSION_DEPTH + 1; i++) {
146
Dictionary next;
147
next["key"] = max_recursion_dictionary;
148
max_recursion_dictionary = next;
149
}
150
CHECK(JSON::stringify(max_recursion_dictionary).contains("{...:...}"));
151
ERR_PRINT_ON
152
}
153
154
// NOTE: The current JSON parser accepts many non-conformant strings such as
155
// single-quoted strings, duplicate commas and trailing commas.
156
// This is intentionally not tested as users shouldn't rely on this behavior.
157
158
TEST_CASE("[JSON] Parsing single data types") {
159
// Parsing a single data type as JSON is valid per the JSON specification.
160
161
JSON json;
162
163
json.parse("null");
164
CHECK_MESSAGE(
165
json.get_error_line() == 0,
166
"Parsing `null` as JSON should parse successfully.");
167
CHECK_MESSAGE(
168
json.get_data() == Variant(),
169
"Parsing a double quoted string as JSON should return the expected value.");
170
171
json.parse("true");
172
CHECK_MESSAGE(
173
json.get_error_line() == 0,
174
"Parsing boolean `true` as JSON should parse successfully.");
175
CHECK_MESSAGE(
176
json.get_data(),
177
"Parsing boolean `true` as JSON should return the expected value.");
178
179
json.parse("false");
180
CHECK_MESSAGE(
181
json.get_error_line() == 0,
182
"Parsing boolean `false` as JSON should parse successfully.");
183
CHECK_MESSAGE(
184
!json.get_data(),
185
"Parsing boolean `false` as JSON should return the expected value.");
186
187
json.parse("123456");
188
CHECK_MESSAGE(
189
json.get_error_line() == 0,
190
"Parsing an integer number as JSON should parse successfully.");
191
CHECK_MESSAGE(
192
(int)(json.get_data()) == 123456,
193
"Parsing an integer number as JSON should return the expected value.");
194
195
json.parse("0.123456");
196
CHECK_MESSAGE(
197
json.get_error_line() == 0,
198
"Parsing a floating-point number as JSON should parse successfully.");
199
CHECK_MESSAGE(
200
double(json.get_data()) == doctest::Approx(0.123456),
201
"Parsing a floating-point number as JSON should return the expected value.");
202
203
json.parse("\"hello\"");
204
CHECK_MESSAGE(
205
json.get_error_line() == 0,
206
"Parsing a double quoted string as JSON should parse successfully.");
207
CHECK_MESSAGE(
208
json.get_data() == "hello",
209
"Parsing a double quoted string as JSON should return the expected value.");
210
}
211
212
TEST_CASE("[JSON] Parsing arrays") {
213
JSON json;
214
215
// JSON parsing fails if it's split over several lines (even if leading indentation is removed).
216
json.parse(R"(["Hello", "world.", "This is",["a","json","array.",[]], "Empty arrays ahoy:", [[["Gotcha!"]]]])");
217
218
const Array array = json.get_data();
219
CHECK_MESSAGE(
220
json.get_error_line() == 0,
221
"Parsing a JSON array should parse successfully.");
222
CHECK_MESSAGE(
223
array[0] == "Hello",
224
"The parsed JSON should contain the expected values.");
225
const Array sub_array = array[3];
226
CHECK_MESSAGE(
227
sub_array.size() == 4,
228
"The parsed JSON should contain the expected values.");
229
CHECK_MESSAGE(
230
sub_array[1] == "json",
231
"The parsed JSON should contain the expected values.");
232
CHECK_MESSAGE(
233
sub_array[3].hash() == Array().hash(),
234
"The parsed JSON should contain the expected values.");
235
const Array deep_array = Array(Array(array[5])[0])[0];
236
CHECK_MESSAGE(
237
deep_array[0] == "Gotcha!",
238
"The parsed JSON should contain the expected values.");
239
}
240
241
TEST_CASE("[JSON] Parsing objects (dictionaries)") {
242
JSON json;
243
244
json.parse(R"({"name": "Godot Engine", "is_free": true, "bugs": null, "apples": {"red": 500, "green": 0, "blue": -20}, "empty_object": {}})");
245
246
const Dictionary dictionary = json.get_data();
247
CHECK_MESSAGE(
248
dictionary["name"] == "Godot Engine",
249
"The parsed JSON should contain the expected values.");
250
CHECK_MESSAGE(
251
dictionary["is_free"],
252
"The parsed JSON should contain the expected values.");
253
CHECK_MESSAGE(
254
dictionary["bugs"] == Variant(),
255
"The parsed JSON should contain the expected values.");
256
CHECK_MESSAGE(
257
(int)Dictionary(dictionary["apples"])["blue"] == -20,
258
"The parsed JSON should contain the expected values.");
259
CHECK_MESSAGE(
260
dictionary["empty_object"].hash() == Dictionary().hash(),
261
"The parsed JSON should contain the expected values.");
262
}
263
264
TEST_CASE("[JSON] Parsing escape sequences") {
265
// Only certain escape sequences are valid according to the JSON specification.
266
// Others must result in a parsing error instead.
267
268
JSON json;
269
270
TypedArray<String> valid_escapes = { "\";\"", "\\;\\", "/;/", "b;\b", "f;\f", "n;\n", "r;\r", "t;\t" };
271
272
SUBCASE("Basic valid escape sequences") {
273
for (int i = 0; i < valid_escapes.size(); i++) {
274
String valid_escape = valid_escapes[i];
275
String valid_escape_string = valid_escape.get_slicec(';', 0);
276
String valid_escape_value = valid_escape.get_slicec(';', 1);
277
278
String json_string = "\"\\";
279
json_string += valid_escape_string;
280
json_string += "\"";
281
json.parse(json_string);
282
283
CHECK_MESSAGE(
284
json.get_error_line() == 0,
285
vformat("Parsing valid escape sequence `%s` as JSON should parse successfully.", valid_escape_string));
286
287
String json_value = json.get_data();
288
CHECK_MESSAGE(
289
json_value == valid_escape_value,
290
vformat("Parsing valid escape sequence `%s` as JSON should return the expected value.", valid_escape_string));
291
}
292
}
293
294
SUBCASE("Valid unicode escape sequences") {
295
String json_string = "\"\\u0020\"";
296
json.parse(json_string);
297
298
CHECK_MESSAGE(
299
json.get_error_line() == 0,
300
vformat("Parsing valid unicode escape sequence with value `0020` as JSON should parse successfully."));
301
302
String json_value = json.get_data();
303
CHECK_MESSAGE(
304
json_value == " ",
305
vformat("Parsing valid unicode escape sequence with value `0020` as JSON should return the expected value."));
306
}
307
308
SUBCASE("Invalid escape sequences") {
309
ERR_PRINT_OFF
310
for (char32_t i = 0; i < 128; i++) {
311
bool skip = false;
312
for (int j = 0; j < valid_escapes.size(); j++) {
313
String valid_escape = valid_escapes[j];
314
String valid_escape_string = valid_escape.get_slicec(';', 0);
315
if (valid_escape_string[0] == i) {
316
skip = true;
317
break;
318
}
319
}
320
321
if (skip) {
322
continue;
323
}
324
325
String json_string = "\"\\";
326
json_string += i;
327
json_string += "\"";
328
Error err = json.parse(json_string);
329
330
// TODO: Line number is currently kept on 0, despite an error occurring. This should be fixed in the JSON parser.
331
// CHECK_MESSAGE(
332
// json.get_error_line() != 0,
333
// vformat("Parsing invalid escape sequence with ASCII value `%d` as JSON should fail to parse.", i));
334
CHECK_MESSAGE(
335
err == ERR_PARSE_ERROR,
336
vformat("Parsing invalid escape sequence with ASCII value `%d` as JSON should fail to parse with ERR_PARSE_ERROR.", i));
337
}
338
ERR_PRINT_ON
339
}
340
}
341
342
TEST_CASE("[JSON] Serialization") {
343
JSON json;
344
345
struct FpTestCase {
346
double number;
347
String json;
348
};
349
350
struct IntTestCase {
351
int64_t number;
352
String json;
353
};
354
355
struct UIntTestCase {
356
uint64_t number;
357
String json;
358
};
359
360
static FpTestCase fp_tests_default_precision[] = {
361
{ 0.0, "0.0" },
362
{ 1000.1234567890123456789, "1000.12345678901" },
363
{ -1000.1234567890123456789, "-1000.12345678901" },
364
{ DBL_MAX, "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0" },
365
{ DBL_MAX - 1, "179769313486231570814527423731704356798070567525844996598917476803157260780028538760589558632766878171540458953514382464234321326889464182768467546703537516986049910576551282076245490090389328944075868508455133942304583236903222948165808559332123348274797826204144723168738177180919299881250404026184124858368.0" },
366
{ std::pow(2, 53), "9007199254740992.0" },
367
{ -std::pow(2, 53), "-9007199254740992.0" },
368
{ 0.00000000000000011, "0.00000000000000011" },
369
{ -0.00000000000000011, "-0.00000000000000011" },
370
{ 1.0 / 3.0, "0.333333333333333" },
371
{ 0.9999999999999999, "1.0" },
372
{ 1.0000000000000001, "1.0" },
373
};
374
375
static FpTestCase fp_tests_full_precision[] = {
376
{ 0.0, "0.0" },
377
{ 1000.1234567890123456789, "1000.1234567890124" },
378
{ -1000.1234567890123456789, "-1000.1234567890124" },
379
{ DBL_MAX, "1.7976931348623157e+308" },
380
{ DBL_MAX - 1, "1.7976931348623157e+308" },
381
{ std::pow(2, 53), "9.007199254740992e+15" },
382
{ -std::pow(2, 53), "-9.007199254740992e+15" },
383
{ 0.00000000000000011, "1.1e-16" },
384
{ -0.00000000000000011, "-1.1e-16" },
385
{ 1.0 / 3.0, "0.3333333333333333" },
386
{ 0.9999999999999999, "0.9999999999999999" },
387
{ 1.0000000000000001, "1.0" },
388
};
389
390
static IntTestCase int_tests[] = {
391
{ 0, "0" },
392
{ INT64_MAX, "9223372036854775807" },
393
{ INT64_MIN, "-9223372036854775808" },
394
};
395
396
SUBCASE("Floating point default precision") {
397
for (FpTestCase &test : fp_tests_default_precision) {
398
String json_value = json.stringify(test.number, "", true, false);
399
400
CHECK_MESSAGE(
401
json_value == test.json,
402
vformat("Serializing `%.20d` to JSON should return the expected value.", test.number));
403
}
404
}
405
406
SUBCASE("Floating point full precision") {
407
for (FpTestCase &test : fp_tests_full_precision) {
408
String json_value = json.stringify(test.number, "", true, true);
409
410
CHECK_MESSAGE(
411
json_value == test.json,
412
vformat("Serializing `%20f` to JSON should return the expected value.", test.number));
413
}
414
}
415
416
SUBCASE("Signed integer") {
417
for (IntTestCase &test : int_tests) {
418
String json_value = json.stringify(test.number, "", true, true);
419
420
CHECK_MESSAGE(
421
json_value == test.json,
422
vformat("Serializing `%d` to JSON should return the expected value.", test.number));
423
}
424
}
425
}
426
} // namespace TestJSON
427
428