blob: 85f54889f2ba5a1b12c44287c057a03be3e042d7 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# Hurry Curry! - a game about cooking
# Copyright 2025 nokoe
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, version 3 of the License only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
class_name Serialize
extends Object
func serialize() -> String:
return JSON.stringify(to_dict())
func to_dict() -> Dictionary:
var dict: Dictionary = {}
for i in get_property_list():
var type = i["type"]
var v = get(i["name"])
if v == null:
continue
match type:
TYPE_NIL || TYPE_CALLABLE: pass
TYPE_OBJECT:
if v is Serialize:
dict[i["name"]] = v.to_dict()
TYPE_ARRAY:
dict[i["name"]] = v.map(func(e): return e.to_dict() if e is Serialize else e)
_:
dict[i["name"]] = v
return dict
func deserialize(de: String):
return from_dict(JSON.parse_string(de))
func from_dict(dict: Dictionary):
for i in get_property_list():
var type = i["type"]
var name = i["name"]
if !dict.has(name) || dict[name] == null:
continue
match type:
TYPE_NIL || TYPE_CALLABLE: pass
TYPE_OBJECT:
var copy = new()
copy.from_dict(dict[name])
set(name, copy)
TYPE_ARRAY:
set(name, dict[name].map(func(e): if e is Dictionary:
var copy = new()
copy.from_dict(e)
return copy
else: return e
)
)
_:
set(name, dict[name])
|