Path: blob/master/tests/core/io/test_json_native.cpp
45997 views
/**************************************************************************/1/* test_json_native.cpp */2/**************************************************************************/3/* This file is part of: */4/* GODOT ENGINE */5/* https://godotengine.org */6/**************************************************************************/7/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */8/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */9/* */10/* Permission is hereby granted, free of charge, to any person obtaining */11/* a copy of this software and associated documentation files (the */12/* "Software"), to deal in the Software without restriction, including */13/* without limitation the rights to use, copy, modify, merge, publish, */14/* distribute, sublicense, and/or sell copies of the Software, and to */15/* permit persons to whom the Software is furnished to do so, subject to */16/* the following conditions: */17/* */18/* The above copyright notice and this permission notice shall be */19/* included in all copies or substantial portions of the Software. */20/* */21/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */22/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */23/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */24/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */25/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */26/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */27/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */28/**************************************************************************/2930#include "tests/test_macros.h"3132TEST_FORCE_LINK(test_json_native)3334#include "core/io/json.h"35#include "core/variant/typed_array.h"36#include "core/variant/typed_dictionary.h"3738namespace TestJSONNative {3940String encode(const Variant &p_variant, bool p_full_objects = false) {41return JSON::stringify(JSON::from_native(p_variant, p_full_objects), "", false);42}4344Variant decode(const String &p_string, bool p_allow_objects = false) {45return JSON::to_native(JSON::parse_string(p_string), p_allow_objects);46}4748void test(const Variant &p_variant, const String &p_string, bool p_with_objects = false) {49CHECK(encode(p_variant, p_with_objects) == p_string);50CHECK(decode(p_string, p_with_objects).get_construct_string() == p_variant.get_construct_string());51}5253TEST_CASE("[JSON][Native] Conversion between native and JSON formats") {54// `Nil` and `bool` (represented as JSON keyword literals).55test(Variant(), "null");56test(false, "false");57test(true, "true");5859// Numbers and strings (represented as JSON strings).60test(1, R"("i:1")");61test(1.0, R"("f:1.0")");62test(Math::INF, R"("f:inf")");63test(-Math::INF, R"("f:-inf")");64test(Math::NaN, R"("f:nan")");65test(String("abc"), R"("s:abc")");66test(StringName("abc"), R"("sn:abc")");67test(NodePath("abc"), R"("np:abc")");6869// Non-serializable types (always empty after deserialization).70test(RID(), R"({"type":"RID"})");71test(Callable(), R"({"type":"Callable"})");72test(Signal(), R"({"type":"Signal"})");7374// Math types.7576test(Vector2(1, 2), R"({"type":"Vector2","args":[1.0,2.0]})");77test(Vector2i(1, 2), R"({"type":"Vector2i","args":[1,2]})");78test(Rect2(1, 2, 3, 4), R"({"type":"Rect2","args":[1.0,2.0,3.0,4.0]})");79test(Rect2i(1, 2, 3, 4), R"({"type":"Rect2i","args":[1,2,3,4]})");80test(Vector3(1, 2, 3), R"({"type":"Vector3","args":[1.0,2.0,3.0]})");81test(Vector3i(1, 2, 3), R"({"type":"Vector3i","args":[1,2,3]})");82test(Transform2D(1, 2, 3, 4, 5, 6), R"({"type":"Transform2D","args":[1.0,2.0,3.0,4.0,5.0,6.0]})");83test(Vector4(1, 2, 3, 4), R"({"type":"Vector4","args":[1.0,2.0,3.0,4.0]})");84test(Vector4i(1, 2, 3, 4), R"({"type":"Vector4i","args":[1,2,3,4]})");85test(Plane(1, 2, 3, 4), R"({"type":"Plane","args":[1.0,2.0,3.0,4.0]})");86test(Quaternion(1, 2, 3, 4), R"({"type":"Quaternion","args":[1.0,2.0,3.0,4.0]})");87test(AABB(Vector3(1, 2, 3), Vector3(4, 5, 6)), R"({"type":"AABB","args":[1.0,2.0,3.0,4.0,5.0,6.0]})");8889const Basis b(Vector3(1, 2, 3), Vector3(4, 5, 6), Vector3(7, 8, 9));90test(b, R"({"type":"Basis","args":[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0]})");9192const Transform3D tr3d(Vector3(1, 2, 3), Vector3(4, 5, 6), Vector3(7, 8, 9), Vector3(10, 11, 12));93test(tr3d, R"({"type":"Transform3D","args":[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0]})");9495const Projection p(Vector4(1, 2, 3, 4), Vector4(5, 6, 7, 8), Vector4(9, 10, 11, 12), Vector4(13, 14, 15, 16));96test(p, R"({"type":"Projection","args":[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0,13.0,14.0,15.0,16.0]})");9798test(Color(1, 2, 3, 4), R"({"type":"Color","args":[1.0,2.0,3.0,4.0]})");99100// `Object`.101102Ref<Resource> res;103res.instantiate();104105// The properties are stored in an array because the order in which they are assigned may be important during initialization.106const String res_repr = R"({"type":"Resource","props":["resource_local_to_scene",false,"resource_name","s:","script",null]})";107108test(res, res_repr, true);109ERR_PRINT_OFF;110CHECK(encode(res) == "null");111CHECK(decode(res_repr).get_type() == Variant::NIL);112ERR_PRINT_ON;113114// `Dictionary`.115116Dictionary dict;117dict[false] = true;118dict[0] = 1;119dict[0.0] = 1.0;120121// Godot dictionaries preserve insertion order, so an array is used for keys/values.122test(dict, R"({"type":"Dictionary","args":[false,true,"i:0","i:1","f:0.0","f:1.0"]})");123124TypedDictionary<int64_t, int64_t> int_int_dict;125int_int_dict[1] = 2;126int_int_dict[3] = 4;127128test(int_int_dict, R"({"type":"Dictionary","key_type":"int","value_type":"int","args":["i:1","i:2","i:3","i:4"]})");129130TypedDictionary<int64_t, Variant> int_var_dict;131int_var_dict[1] = "2";132int_var_dict[3] = "4";133134test(int_var_dict, R"({"type":"Dictionary","key_type":"int","args":["i:1","s:2","i:3","s:4"]})");135136TypedDictionary<Variant, int64_t> var_int_dict;137var_int_dict["1"] = 2;138var_int_dict["3"] = 4;139140test(var_int_dict, R"({"type":"Dictionary","value_type":"int","args":["s:1","i:2","s:3","i:4"]})");141142Dictionary dict2;143dict2["x"] = res;144145const String dict2_repr = vformat(R"({"type":"Dictionary","args":["s:x",%s]})", res_repr);146147test(dict2, dict2_repr, true);148ERR_PRINT_OFF;149CHECK(encode(dict2) == R"({"type":"Dictionary","args":["s:x",null]})");150CHECK(decode(dict2_repr).get_construct_string() == "{\n\"x\": null\n}");151ERR_PRINT_ON;152153TypedDictionary<String, Resource> res_dict;154res_dict["x"] = res;155156const String res_dict_repr = vformat(R"({"type":"Dictionary","key_type":"String","value_type":"Resource","args":["s:x",%s]})", res_repr);157158test(res_dict, res_dict_repr, true);159ERR_PRINT_OFF;160CHECK(encode(res_dict) == "null");161CHECK(decode(res_dict_repr).get_type() == Variant::NIL);162ERR_PRINT_ON;163164// `Array`.165166Array arr = { true, 1, "abc" };167test(arr, R"([true,"i:1","s:abc"])");168169TypedArray<int64_t> int_arr = { 1, 2, 3 };170test(int_arr, R"({"type":"Array","elem_type":"int","args":["i:1","i:2","i:3"]})");171172Array arr2 = { 1, res, 9 };173const String arr2_repr = vformat(R"(["i:1",%s,"i:9"])", res_repr);174175test(arr2, arr2_repr, true);176ERR_PRINT_OFF;177CHECK(encode(arr2) == R"(["i:1",null,"i:9"])");178CHECK(decode(arr2_repr).get_construct_string() == "[1, null, 9]");179ERR_PRINT_ON;180181TypedArray<Resource> res_arr = { res };182const String res_arr_repr = vformat(R"({"type":"Array","elem_type":"Resource","args":[%s]})", res_repr);183184test(res_arr, res_arr_repr, true);185ERR_PRINT_OFF;186CHECK(encode(res_arr) == "null");187CHECK(decode(res_arr_repr).get_type() == Variant::NIL);188ERR_PRINT_ON;189190// Packed arrays.191192test(PackedByteArray({ 1, 2, 3 }), R"({"type":"PackedByteArray","args":[1,2,3]})");193test(PackedInt32Array({ 1, 2, 3 }), R"({"type":"PackedInt32Array","args":[1,2,3]})");194test(PackedInt64Array({ 1, 2, 3 }), R"({"type":"PackedInt64Array","args":[1,2,3]})");195test(PackedFloat32Array({ 1, 2, 3 }), R"({"type":"PackedFloat32Array","args":[1.0,2.0,3.0]})");196test(PackedFloat64Array({ 1, 2, 3 }), R"({"type":"PackedFloat64Array","args":[1.0,2.0,3.0]})");197test(PackedStringArray({ "a", "b", "c" }), R"({"type":"PackedStringArray","args":["a","b","c"]})");198199const PackedVector2Array pv2arr({ Vector2(1, 2), Vector2(3, 4), Vector2(5, 6) });200test(pv2arr, R"({"type":"PackedVector2Array","args":[1.0,2.0,3.0,4.0,5.0,6.0]})");201202const PackedVector3Array pv3arr({ Vector3(1, 2, 3), Vector3(4, 5, 6), Vector3(7, 8, 9) });203test(pv3arr, R"({"type":"PackedVector3Array","args":[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0]})");204205const PackedColorArray pcolarr({ Color(1, 2, 3, 4), Color(5, 6, 7, 8), Color(9, 10, 11, 12) });206test(pcolarr, R"({"type":"PackedColorArray","args":[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0]})");207208const PackedVector4Array pv4arr({ Vector4(1, 2, 3, 4), Vector4(5, 6, 7, 8), Vector4(9, 10, 11, 12) });209test(pv4arr, R"({"type":"PackedVector4Array","args":[1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0]})");210}211212} // namespace TestJSONNative213214215