immersive-home/app/lib/utils/variant_serializer.gd

84 lines
2.0 KiB
GDScript3
Raw Normal View History

2023-12-09 19:39:38 +02:00
extends Object
2024-03-17 01:14:31 +02:00
## Convert a dictionary to be able to be serialized to JSON.
2023-12-09 19:39:38 +02:00
static func stringify_value(value):
match typeof(value):
TYPE_DICTIONARY:
var new_dict = {}
for key in value.keys():
new_dict[key] = stringify_value(value[key])
return new_dict
TYPE_ARRAY:
return value.map(func(item):
return stringify_value(item)
)
2024-01-25 17:29:33 +02:00
TYPE_BOOL, TYPE_INT, TYPE_FLOAT, TYPE_STRING, TYPE_NIL:
2023-12-09 19:39:38 +02:00
return value
2024-04-29 18:18:15 +03:00
TYPE_STRING_NAME:
return str(value)
2023-12-09 19:39:38 +02:00
TYPE_VECTOR2:
return {
"x": value.x,
"y": value.y,
"_type": "Vector2"
}
TYPE_VECTOR3:
return {
"x": value.x,
"y": value.y,
"z": value.z,
"_type": "Vector3"
}
TYPE_TRANSFORM3D:
return {
"origin": stringify_value(value.origin),
"basis": stringify_value(value.basis),
"_type": "Transform3D"
}
TYPE_BASIS:
return {
"x": stringify_value(value.x),
"y": stringify_value(value.y),
"z": stringify_value(value.z),
"_type": "Basis"
}
TYPE_QUATERNION:
return {
"x": value.x,
"y": value.y,
"z": value.z,
"w": value.w,
"_type": "Quaternion"
}
_:
assert(false, "Unsupported type: %s" % typeof(value))
2024-03-17 01:14:31 +02:00
## Restore a dictionary from a JSON-serialized dictionary.
2023-12-09 19:39:38 +02:00
static func parse_value(value):
if typeof(value) == TYPE_ARRAY:
return value.map(func(item):
return parse_value(item)
)
elif typeof(value) == TYPE_DICTIONARY:
if value.has("_type"):
match value["_type"]:
"Vector2":
return Vector2(value["x"], value["y"])
"Vector3":
return Vector3(value["x"], value["y"], value["z"])
"Transform3D":
return Transform3D(parse_value(value["basis"]), parse_value(value["origin"]))
"Basis":
return Basis(parse_value(value["x"]), parse_value(value["y"]), parse_value(value["z"]))
"Quaternion":
return Quaternion(value["x"], value["y"], value["z"], value["w"])
_:
assert(false, "Unsupported type: %s" % value["_type"])
else:
var new_dict = {}
for key in value.keys():
new_dict[key] = parse_value(value[key])
return new_dict
else:
return value