-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathData.cpp
More file actions
263 lines (213 loc) · 6.36 KB
/
Data.cpp
File metadata and controls
263 lines (213 loc) · 6.36 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
#include "Data.h"
Data::Data()
{
root_value = json_value_init_object();
root = json_value_get_object(root_value);
}
Data::Data(const char * filename)
{
root_value = json_parse_string(filename);
root = json_value_get_object(root_value);
}
Data::Data(JSON_Object* root) : root(root)
{}
Data::~Data()
{
json_value_free(root_value);
}
bool Data::AppendArray(const char * name)
{
JSON_Value* value = json_value_init_array();
array = json_value_get_array(value);
return json_object_set_value(root, name, value) == JSONSuccess;
}
bool Data::AppendArrayValue(const Data & data)
{
if (!array)
return false;
return json_array_append_value(array, json_value_deep_copy(data.root_value)) == JSONSuccess;
}
bool Data::AppendString(const char * name, const char * string)
{
return json_object_set_string(root, name, string) == JSONSuccess;
}
bool Data::AppendBool(const char * name, bool value)
{
return json_object_set_boolean(root, name, value) == JSONSuccess;
}
bool Data::AppendUInt(const char * name, unsigned int value)
{
return json_object_set_number(root, name, (double)value) == JSONSuccess;
}
bool Data::AppendInt(const char * name, int value)
{
return json_object_set_number(root, name, value) == JSONSuccess;
}
bool Data::AppendMatrix(const char * name, const math::float4x4 & matrix)
{
JSON_Value* value = json_value_init_array();
JSON_Array* array = json_value_get_array(value);
const float* matrix_array = *matrix.v;
for (int i = 0; i < 16; i++)
json_array_append_number(array, matrix_array[i]);
return json_object_set_value(root, name, value) == JSONSuccess;
}
bool Data::AppendFloat(const char * name, float value)
{
return json_object_set_number(root, name, (double)value) == JSONSuccess;
}
bool Data::AppendFloat2(const char * name, const float * value)
{
JSON_Value* j_value = json_value_init_array();
JSON_Array* array = json_value_get_array(j_value);
for (int i = 0; i < 2; i++)
json_array_append_number(array, value[i]);
return json_object_set_value(root, name, j_value) == JSONSuccess;
}
bool Data::AppendFloat3(const char * name,const float * value)
{
JSON_Value* j_value = json_value_init_array();
JSON_Array* array = json_value_get_array(j_value);
for (int i = 0; i < 3; i++)
json_array_append_number(array, value[i]);
return json_object_set_value(root, name, j_value) == JSONSuccess;
}
bool Data::AppendFloat4(const char * name, const float * value)
{
JSON_Value* j_value = json_value_init_array();
JSON_Array* array = json_value_get_array(j_value);
for (int i = 0; i < 4; i++)
json_array_append_number(array, value[i]);
return json_object_set_value(root, name, j_value) == JSONSuccess;
}
bool Data::AppendDouble(const char * name, double value)
{
return json_object_set_number(root, name, value) == JSONSuccess;
}
Data Data::AppendJObject(const char * name)
{
json_object_set_value(root, name, json_value_init_object());
return GetJObject(name);
}
Data Data::GetJObject(const char* name)const
{
return Data(json_object_get_object(root, name));
}
const char * Data::GetString(const char* name) const
{
return json_object_get_string(root, name);
}
int Data::GetInt(const char* name)const
{
return (int) json_object_get_number(root, name);
}
Data Data::GetArray(const char * name, unsigned int index) const
{
JSON_Array* j_array = json_object_get_array(root, name);
if (j_array)
return Data(json_array_get_object(j_array,index));
return Data((JSON_Object*) nullptr);
}
size_t Data::GetArraySize(const char* name) const
{
size_t size = 0;
JSON_Array* j_array = json_object_get_array(root, name);
if (j_array)
size = json_array_get_count(j_array);
return size;
}
bool Data::GetBool(const char * name) const
{
int ret = json_object_get_boolean(root, name);
return (ret > 0) ? true : false;
}
unsigned int Data::GetUInt(const char * name) const
{
return (unsigned int)json_object_get_number(root, name);
}
float4x4 Data::GetMatrix(const char * name) const
{
JSON_Array* j_array = json_object_get_array(root, name);
if (j_array)
{
float4x4 ret((float)json_array_get_number(j_array, 0),
(float)json_array_get_number(j_array, 1),
(float)json_array_get_number(j_array, 2),
(float)json_array_get_number(j_array, 3),
(float)json_array_get_number(j_array, 4),
(float)json_array_get_number(j_array, 5),
(float)json_array_get_number(j_array, 6),
(float)json_array_get_number(j_array, 7),
(float)json_array_get_number(j_array, 8),
(float)json_array_get_number(j_array, 9),
(float)json_array_get_number(j_array, 10),
(float)json_array_get_number(j_array, 11),
(float)json_array_get_number(j_array, 12),
(float)json_array_get_number(j_array, 13),
(float)json_array_get_number(j_array, 14),
(float)json_array_get_number(j_array, 15));
return ret;
}
return float4x4::identity;
}
float Data::GetFloat(const char * name) const
{
return (float)json_object_get_number(root, name);
}
float2 Data::GetFloat2(const char * name) const
{
JSON_Array* j_array = json_object_get_array(root, name);
if (j_array)
{
float2 ret((float)json_array_get_number(j_array, 0),
(float)json_array_get_number(j_array, 1));
return ret;
}
return float2::zero;
}
float3 Data::GetFloat3(const char * name) const
{
JSON_Array* j_array = json_object_get_array(root, name);
if (j_array)
{
float3 ret((float)json_array_get_number(j_array, 0),
(float)json_array_get_number(j_array, 1),
(float)json_array_get_number(j_array, 2));
return ret;
}
return float3::zero;
}
float4 Data::GetFloat4(const char * name) const
{
JSON_Array* j_array = json_object_get_array(root, name);
if (j_array)
{
float4 ret((float)json_array_get_number(j_array, 0),
(float)json_array_get_number(j_array, 1),
(float)json_array_get_number(j_array, 2),
(float)json_array_get_number(j_array, 3));
return ret;
}
return float4::zero;
}
double Data::GetDouble(const char * name) const
{
return json_object_get_number(root, name);
}
void Data::LoadArray(const char* name)
{
JSON_Array* j_array = json_object_get_array(root, name);
if (j_array)
array = j_array;
}
size_t Data::Serialize(char ** buffer)
{
size_t size = json_serialization_size_pretty(root_value); //TODO: Change for normal serialization. Pretty is enabled now for testing.
*buffer = new char[size];
json_serialize_to_buffer_pretty(root_value, *buffer, size);
return size;
}
bool Data::IsNull() const
{
return (root_value == nullptr && root == nullptr && array == nullptr) ? true : false;
}