-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtoJson.cpp
More file actions
189 lines (164 loc) · 4.96 KB
/
toJson.cpp
File metadata and controls
189 lines (164 loc) · 4.96 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#include "Headers.h"
using namespace rttr;
void toJsonRecursive(const instance& obj, json& writer);
/////////////////////////////////////////////////////////////////////////////////////////
bool WriteVariant(const variant& var, json& writer);
bool AttemptWriteFundementalType(const type& t, const variant& var, json& writer)
{
// Json Number
if (t.is_arithmetic())
{
if (t == type::get<bool>())
writer = var.to_bool();
else if (t == type::get<char>())
writer = var.to_bool();
else if (t == type::get<int>())
writer = var.to_int();
else if (t == type::get<uint64_t>())
writer = var.to_uint64();
else if (t == type::get<float>())
writer = var.to_double();
else if (t == type::get<double>())
writer = var.to_double();
return true;
}
// Enumeration as string
else if (t.is_enumeration())
{
bool ok = false;
// Attempt to serialize as string
auto result = var.to_string(&ok);
if (ok)
{
writer = var.to_string();
}
else
{
// Attempt to serialize as number
auto value = var.to_uint64(&ok);
if (ok)
writer = uint64_t(value);
else
writer = nullptr;
}
return true;
}
// Strings!
else if (t == type::get<std::string>())
{
writer = var.to_string();
return true;
}
// Not a fundamental type we know how to process
return false;
}
/////////////////////////////////////////////////////////////////////////////////////////
static void WriteArray(const variant_sequential_view& view, json& writer)
{
// Init array
writer = json::array();
int i = 0;
for (const auto& item : view)
{
WriteVariant(item, writer[i]);
i++;
}
}
/////////////////////////////////////////////////////////////////////////////////////////
static void WriteAssociativeContainer(const variant_associative_view& view, json& writer)
{
static const string_view key_name("key");
static const string_view value_name("value");
writer = json::array();
int i = 0;
// Dealing with keys = values containers like sets
if (view.is_key_only_type())
{
for (auto& item : view)
{
WriteVariant(item.first, writer[i]);
i++;
}
}
else
{
for (auto& item : view)
{
WriteVariant(item.first, writer[i][key_name.data()]);
WriteVariant(item.second, writer[i][value_name.data()]);
i++;
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////
bool WriteVariant(const variant& var, json& writer)
{
// Deal with wrapped type
variant localVar = var;
type varType = var.get_type();
if(varType.is_wrapper())
{
varType = varType.get_wrapped_type();
localVar = localVar.extract_wrapped_value();
}
if (AttemptWriteFundementalType(varType, localVar, writer))
{
// Successful write!
return true;
}
// If its not a basic, is it a sequential?
else if (var.is_sequential_container())
{
WriteArray(var.create_sequential_view(), writer);
}
else if (var.is_associative_container())
{
WriteAssociativeContainer(var.create_associative_view(), writer);
}
else
{
// Not a fundemental, or a container. Thus a OBJ.
auto child_props = varType.get_properties();
if (!child_props.empty())
{
toJsonRecursive(var, writer);
}
else
{
// Assert
// Some unknown type that is not a fundamental, has no properties, and is not a data structure
// Probably some registration issue
// Or its a pointer! I handled pointers in here my game code
assert("Unknown RTTR serilization edge case that we haven't discovered");
return false;
}
}
return true;
}
void toJsonRecursive(const instance& obj2, json& writer)
{
instance obj = obj2.get_type().get_raw_type().is_wrapper() ? obj2.get_wrapped_instance() : obj2;
auto prop_list = obj.get_derived_type().get_properties();
for (auto prop : prop_list)
{
variant prop_value = prop.get_value(obj);
if (!prop_value)
continue; // cannot serialize, because we cannot retrieve the value
if (prop.get_metadata("NO_SERIALIZE"))
continue;
const auto name = prop.get_name();
if (!WriteVariant(prop_value, writer[name.data()]))
{
std::cerr << "Failed to serialize" << name << std::endl;
}
}
}
// Kickstart function
json ToJson(rttr::instance obj, const char * name) {
json jsonWriter;
// Check instance for errors
if (!obj.is_valid())
return std::string();
toJsonRecursive(obj, jsonWriter[name]);
return jsonWriter;
}