-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathjson.cpp
More file actions
668 lines (638 loc) · 22.5 KB
/
Copy pathjson.cpp
File metadata and controls
668 lines (638 loc) · 22.5 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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
#include "json.hpp"
namespace UrhoExtras
{
bool writeStringWithoutEnding(Urho3D::String const& str, Urho3D::Serializer& dest)
{
return dest.Write(str.CString(), str.Length());
}
Urho3D::String escapeString(Urho3D::String const& str)
{
// TODO: Escape unicode characters!
Urho3D::String result = str;
result.Replace("\\", "\\\\");
result.Replace("\n", "\\n");
result.Replace("\"", "\\\"");
result.Replace("\'", "\\\'");
return result;
}
bool saveJson(Urho3D::JSONValue const& json, Urho3D::Serializer& dest)
{
if (json.IsBool()) {
if (json.GetBool()) {
if (!writeStringWithoutEnding("true", dest)) return false;
} else {
if (!writeStringWithoutEnding("false", dest)) return false;
}
} else if (json.IsNull()) {
if (!writeStringWithoutEnding("null", dest)) return false;
} else if (json.IsNumber()) {
if (json.GetNumberType() == Urho3D::JSONNT_NAN) {
if (!writeStringWithoutEnding("null", dest)) return false;
} else if (json.GetNumberType() == Urho3D::JSONNT_INT) {
if (!writeStringWithoutEnding(Urho3D::String(json.GetInt()), dest)) return false;
} else if (json.GetNumberType() == Urho3D::JSONNT_UINT) {
if (!writeStringWithoutEnding(Urho3D::String(json.GetUInt()), dest)) return false;
} else if (json.GetNumberType() == Urho3D::JSONNT_FLOAT_DOUBLE) {
if (!writeStringWithoutEnding(Urho3D::String(json.GetDouble()), dest)) return false;
}
} else if (json.IsString()) {
if (!dest.WriteByte('\"')) return false;
if (!writeStringWithoutEnding(escapeString(json.GetString()), dest)) return false;
if (!dest.WriteByte('\"')) return false;
} else if (json.IsArray()) {
if (!dest.WriteByte('[')) return false;
Urho3D::JSONArray const& arr = json.GetArray();
for (unsigned i = 0; i < arr.Size(); ++ i) {
if (!saveJson(arr[i], dest)) return false;
if (i < arr.Size() - 1) {
if (!dest.WriteByte(',')) return false;
}
}
if (!dest.WriteByte(']')) return false;
} else if (json.IsObject()) {
if (!dest.WriteByte('{')) return false;
for (Urho3D::JSONObject::ConstIterator it = json.GetObject().Begin(); it != json.GetObject().End(); ++ it) {
Urho3D::String const& key = it->first_;
Urho3D::JSONValue const& value = it->second_;
if (!dest.WriteByte('\"')) return false;
if (!writeStringWithoutEnding(escapeString(key), dest)) return false;
if (!writeStringWithoutEnding("\":", dest)) return false;
if (!saveJson(value, dest)) return false;
Urho3D::JSONObject::ConstIterator it_next = it;
++ it_next;
if (it_next != json.GetObject().End()) {
if (!dest.WriteByte(',')) return false;
}
}
if (!dest.WriteByte('}')) return false;
} else {
return false;
}
return true;
}
int jsonToInt(
Urho3D::JSONValue const& json,
Urho3D::String const& error_prefix,
int min_limit,
int max_limit
)
{
if (!json.IsNumber()) {
throw JsonValidatorError(error_prefix + "Must be an integer!");
}
if (json.GetNumberType() == Urho3D::JSONNT_NAN || json.GetNumberType() == Urho3D::JSONNT_FLOAT_DOUBLE) {
throw JsonValidatorError(error_prefix + "Must be an integer!");
}
int result = json.GetInt();
if (result < min_limit) {
throw JsonValidatorError(error_prefix + "Must be " + Urho3D::String(min_limit) + " or bigger!");
}
if (result > max_limit) {
throw JsonValidatorError(error_prefix + "Must be " + Urho3D::String(max_limit) + " or smaller!");
}
return result;
}
float jsonToFloat(
Urho3D::JSONValue const& json,
Urho3D::String const& error_prefix,
float min_limit,
float max_limit
)
{
if (!json.IsNumber()) {
throw JsonValidatorError(error_prefix + "Must be a float!");
}
if (json.GetNumberType() == Urho3D::JSONNT_NAN) {
throw JsonValidatorError(error_prefix + "Must be a float!");
}
float result = json.GetFloat();
if (result < min_limit) {
throw JsonValidatorError(error_prefix + "Must be " + Urho3D::String(min_limit) + " or bigger!");
}
if (result > max_limit) {
throw JsonValidatorError(error_prefix + "Must be " + Urho3D::String(max_limit) + " or smaller!");
}
return result;
}
Urho3D::JSONValue getJsonObject(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix
)
{
if (!json.IsObject()) {
throw JsonValidatorError(error_prefix + "Unable to get a member from non-object!");
}
if (!json.Contains(key)) {
throw JsonValidatorError(error_prefix + "Not found!");
}
if (!json.Get(key).IsObject()) {
throw JsonValidatorError(error_prefix + "Not an object!");
}
return json.Get(key);
}
Urho3D::JSONValue getJsonObjectIfExists(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix,
Urho3D::JSONValue const& default_value
)
{
if (!json.IsObject()) {
throw JsonValidatorError(error_prefix + "Unable to get a member from non-object!");
}
if (!json.Contains(key)) {
return default_value;
}
return getJsonObject(json, key, error_prefix);
}
Urho3D::String getJsonString(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix,
Urho3D::Vector<Urho3D::String> const& allowed_values
)
{
if (!json.IsObject()) {
throw JsonValidatorError(error_prefix + "Unable to get a member from non-object!");
}
if (!json.Contains(key)) {
throw JsonValidatorError(error_prefix + "Not found!");
}
if (!json.Get(key).IsString()) {
throw JsonValidatorError(error_prefix + "Must be a string!");
}
Urho3D::String result = json.Get(key).GetString();
if (!allowed_values.Empty() && !allowed_values.Contains(result)) {
Urho3D::String error = error_prefix + "Must be a one of the following values: ";
bool first = true;
for (auto value : allowed_values) {
if (first) {
first = false;
} else {
error += ", ";
}
error += "\"" + value + "\"";
}
throw JsonValidatorError(error);
}
return result;
}
Urho3D::String getJsonStringIfExists(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix,
Urho3D::String const& default_value,
Urho3D::Vector<Urho3D::String> const& allowed_values
)
{
if (!json.IsObject()) {
throw JsonValidatorError(error_prefix + "Unable to get a member from non-object!");
}
if (!json.Contains(key)) {
return default_value;
}
return getJsonString(json, key, error_prefix, allowed_values);
}
bool getJsonBoolean(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix
)
{
if (!json.IsObject()) {
throw JsonValidatorError(error_prefix + "Unable to get a member from non-object!");
}
if (!json.Contains(key)) {
throw JsonValidatorError(error_prefix + "Not found!");
}
if (!json.Get(key).IsBool()) {
throw JsonValidatorError(error_prefix + "Must be true or false!");
}
return json.Get(key).GetBool();
}
bool getJsonBooleanIfExists(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix,
bool default_value
)
{
if (!json.IsObject()) {
throw JsonValidatorError(error_prefix + "Unable to get a member from non-object!");
}
if (!json.Contains(key)) {
return default_value;
}
return getJsonBoolean(json, key, error_prefix);
}
int getJsonInt(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix,
int min_limit,
int max_limit
)
{
if (!json.IsObject()) {
throw JsonValidatorError(error_prefix + "Unable to get a member from non-object!");
}
if (!json.Contains(key)) {
throw JsonValidatorError(error_prefix + "Not found!");
}
return jsonToInt(json.Get(key), error_prefix, min_limit, max_limit);
}
int getJsonIntIfExists(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix,
int default_value,
int min_limit,
int max_limit
)
{
if (!json.IsObject()) {
throw JsonValidatorError(error_prefix + "Unable to get a member from non-object!");
}
if (!json.Contains(key)) {
return default_value;
}
return jsonToInt(json.Get(key), error_prefix, min_limit, max_limit);
}
float getJsonFloat(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix,
float min_limit,
float max_limit
)
{
if (!json.IsObject()) {
throw JsonValidatorError(error_prefix + "Unable to get a member from non-object!");
}
if (!json.Contains(key)) {
throw JsonValidatorError(error_prefix + "Not found!");
}
return jsonToFloat(json.Get(key), error_prefix, min_limit, max_limit);
}
float getJsonFloatIfExists(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix,
float default_value,
float min_limit,
float max_limit
)
{
if (!json.IsObject()) {
throw JsonValidatorError(error_prefix + "Unable to get a member from non-object!");
}
if (!json.Contains(key)) {
return default_value;
}
return jsonToFloat(json.Get(key), error_prefix, min_limit, max_limit);
}
Urho3D::JSONArray getJsonArray(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix,
unsigned min_size_limit,
unsigned max_size_limit
)
{
if (!json.IsObject()) {
throw JsonValidatorError(error_prefix + "Unable to get a member from non-object!");
}
if (!json.Contains(key)) {
throw JsonValidatorError(error_prefix + "Not found!");
}
if (!json.Get(key).IsArray()) {
throw JsonValidatorError(error_prefix + "Must be an array!");
}
Urho3D::JSONArray result = json.Get(key).GetArray();
if (min_size_limit == max_size_limit && result.Size() != min_size_limit) {
throw JsonValidatorError(error_prefix + "Must have exactly " + Urho3D::String(min_size_limit) + " items!");
}
if (result.Size() < min_size_limit) {
throw JsonValidatorError(error_prefix + "Must have at least " + Urho3D::String(min_size_limit) + " items!");
}
if (result.Size() > max_size_limit) {
throw JsonValidatorError(error_prefix + "Must have a maximum of " + Urho3D::String(min_size_limit) + " items!");
}
return result;
}
Urho3D::JSONArray getJsonArrayIfExists(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix,
unsigned min_size_limit,
unsigned max_size_limit
)
{
if (!json.IsObject()) {
throw JsonValidatorError(error_prefix + "Unable to get a member from non-object!");
}
if (!json.Contains(key)) {
return Urho3D::JSONArray();
}
return getJsonArray(json, key, error_prefix, min_size_limit, max_size_limit);
}
Urho3D::Vector2 getJsonVector2(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix,
Urho3D::Vector2 const& min_limit,
Urho3D::Vector2 const& max_limit
)
{
if (!json.IsObject()) {
throw JsonValidatorError(error_prefix + "Unable to get a member from non-object!");
}
if (!json.Contains(key)) {
throw JsonValidatorError(error_prefix + "Not found!");
}
if (!json.Get(key).IsArray()) {
throw JsonValidatorError(error_prefix + "Must be an array!");
}
Urho3D::JSONArray array = json.Get(key).GetArray();
if (array.Size() != 2) {
throw JsonValidatorError(error_prefix + "Must have exactly two numbers!");
}
if (!array[0].IsNumber() || !array[1].IsNumber()) {
throw JsonValidatorError(error_prefix + "Must have exactly two numbers!");
}
Urho3D::Vector2 result = Urho3D::Vector2(array[0].GetFloat(), array[1].GetFloat());
if (result.x_ < min_limit.x_) {
throw JsonValidatorError(error_prefix + "Must have an X component " + Urho3D::String(min_limit.x_) + " or bigger!");
}
if (result.x_ > max_limit.x_) {
throw JsonValidatorError(error_prefix + "Must have an X component " + Urho3D::String(max_limit.x_) + " or smaller!");
}
if (result.y_ < min_limit.y_) {
throw JsonValidatorError(error_prefix + "Must have an Y component " + Urho3D::String(min_limit.y_) + " or bigger!");
}
if (result.y_ > max_limit.y_) {
throw JsonValidatorError(error_prefix + "Must have an Y component " + Urho3D::String(max_limit.y_) + " or smaller!");
}
return result;
}
Urho3D::Vector3 getJsonVector3(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix,
Urho3D::Vector3 const& min_limit,
Urho3D::Vector3 const& max_limit
)
{
if (!json.IsObject()) {
throw JsonValidatorError(error_prefix + "Unable to get a member from non-object!");
}
if (!json.Contains(key)) {
throw JsonValidatorError(error_prefix + "Not found!");
}
if (!json.Get(key).IsArray()) {
throw JsonValidatorError(error_prefix + "Must be an array!");
}
Urho3D::JSONArray array = json.Get(key).GetArray();
if (array.Size() != 3) {
throw JsonValidatorError(error_prefix + "Must have exactly three numbers!");
}
if (!array[0].IsNumber() || !array[1].IsNumber() || !array[2].IsNumber()) {
throw JsonValidatorError(error_prefix + "Must have exactly three numbers!");
}
Urho3D::Vector3 result = Urho3D::Vector3(array[0].GetFloat(), array[1].GetFloat(), array[2].GetFloat());
if (result.x_ < min_limit.x_) {
throw JsonValidatorError(error_prefix + "Must have an X component " + Urho3D::String(min_limit.x_) + " or bigger!");
}
if (result.x_ > max_limit.x_) {
throw JsonValidatorError(error_prefix + "Must have an X component " + Urho3D::String(max_limit.x_) + " or smaller!");
}
if (result.y_ < min_limit.y_) {
throw JsonValidatorError(error_prefix + "Must have an Y component " + Urho3D::String(min_limit.y_) + " or bigger!");
}
if (result.y_ > max_limit.y_) {
throw JsonValidatorError(error_prefix + "Must have an Y component " + Urho3D::String(max_limit.y_) + " or smaller!");
}
if (result.z_ < min_limit.z_) {
throw JsonValidatorError(error_prefix + "Must have an Z component " + Urho3D::String(min_limit.z_) + " or bigger!");
}
if (result.z_ > max_limit.z_) {
throw JsonValidatorError(error_prefix + "Must have an Z component " + Urho3D::String(max_limit.z_) + " or smaller!");
}
return result;
}
Urho3D::Vector4 getJsonVector4(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix,
Urho3D::Vector4 const& min_limit,
Urho3D::Vector4 const& max_limit
)
{
if (!json.IsObject()) {
throw JsonValidatorError(error_prefix + "Unable to get a member from non-object!");
}
if (!json.Contains(key)) {
throw JsonValidatorError(error_prefix + "Not found!");
}
if (!json.Get(key).IsArray()) {
throw JsonValidatorError(error_prefix + "Must be an array!");
}
Urho3D::JSONArray array = json.Get(key).GetArray();
if (array.Size() != 4) {
throw JsonValidatorError(error_prefix + "Must have exactly four numbers!");
}
if (!array[0].IsNumber() || !array[1].IsNumber() || !array[2].IsNumber() || !array[3].IsNumber()) {
throw JsonValidatorError(error_prefix + "Must have exactly three numbers!");
}
Urho3D::Vector4 result = Urho3D::Vector4(array[0].GetFloat(), array[1].GetFloat(), array[2].GetFloat(), array[3].GetFloat());
if (result.x_ < min_limit.x_) {
throw JsonValidatorError(error_prefix + "Must have an X component " + Urho3D::String(min_limit.x_) + " or bigger!");
}
if (result.x_ > max_limit.x_) {
throw JsonValidatorError(error_prefix + "Must have an X component " + Urho3D::String(max_limit.x_) + " or smaller!");
}
if (result.y_ < min_limit.y_) {
throw JsonValidatorError(error_prefix + "Must have an Y component " + Urho3D::String(min_limit.y_) + " or bigger!");
}
if (result.y_ > max_limit.y_) {
throw JsonValidatorError(error_prefix + "Must have an Y component " + Urho3D::String(max_limit.y_) + " or smaller!");
}
if (result.z_ < min_limit.z_) {
throw JsonValidatorError(error_prefix + "Must have an Z component " + Urho3D::String(min_limit.z_) + " or bigger!");
}
if (result.z_ > max_limit.z_) {
throw JsonValidatorError(error_prefix + "Must have an Z component " + Urho3D::String(max_limit.z_) + " or smaller!");
}
if (result.w_ < min_limit.w_) {
throw JsonValidatorError(error_prefix + "Must have an W component " + Urho3D::String(min_limit.w_) + " or bigger!");
}
if (result.w_ > max_limit.w_) {
throw JsonValidatorError(error_prefix + "Must have an W component " + Urho3D::String(max_limit.w_) + " or smaller!");
}
return result;
}
Urho3D::Color getJsonColor(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix
)
{
if (!json.IsObject()) {
throw JsonValidatorError(error_prefix + "Unable to get a member from non-object!");
}
if (!json.Contains(key)) {
throw JsonValidatorError(error_prefix + "Not found!");
}
if (json.Get(key).IsArray()) {
Urho3D::JSONArray array = json.Get(key).GetArray();
if (array.Size() < 3 || array.Size() > 4) {
throw JsonValidatorError(error_prefix + "Must have three or four numbers between one and zero!");
}
if (!array[0].IsNumber() || !array[1].IsNumber() || !array[2].IsNumber()) {
throw JsonValidatorError(error_prefix + "Must have three or four numbers between one and zero!");
}
float r = array[0].GetFloat();
float g = array[1].GetFloat();
float b = array[2].GetFloat();
float a = array.Size() == 4 ? array[3].GetFloat() : 1.0f;
if (r < 0 || r > 1 || g < 0 || g > 1 || b < 0 || b > 1 || a < 0 || a > 1) {
throw JsonValidatorError(error_prefix + "Must have three or four numbers between one and zero!");
}
return Urho3D::Color(r, g, b, a);
}
if (json.Get(key).IsString()) {
Urho3D::String color_str = json.Get(key).GetString();
if (color_str.Length() != 7 && color_str.Length() != 9) {
throw JsonValidatorError(error_prefix + "Must be #rrggbb or #rrggbbaa!");
}
if (color_str[0] != '#') {
throw JsonValidatorError(error_prefix + "Must be #rrggbb or #rrggbbaa!");
}
Urho3D::String hex_chars = "0123456789abcdefABCDEF";
for (unsigned i = 1; i < color_str.Length(); ++ i) {
if (hex_chars.Find(color_str[i]) == Urho3D::String::NPOS) {
throw JsonValidatorError(error_prefix + "Must be #rrggbb or #rrggbbaa!");
}
}
Urho3D::Color color;
for (unsigned i = 1; i < color_str.Length(); i += 2) {
Urho3D::String hex = color_str.Substring(i, 2);
float value = ::strtol(hex.CString(), nullptr, 16) / 255.0f;
if (i == 1) {
color.r_ = value;
} else if (i == 3) {
color.g_ = value;
} else if (i == 5) {
color.b_ = value;
} else {
color.a_ = value;
}
}
return color;
}
throw JsonValidatorError(error_prefix + "Must be an array or string!");
}
Urho3D::Color getJsonColorIfExists(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix,
Urho3D::Color const& default_value
)
{
if (!json.IsObject()) {
throw JsonValidatorError(error_prefix + "Unable to get a member from non-object!");
}
if (!json.Contains(key)) {
return default_value;
}
return getJsonColor(json, key, error_prefix);
}
Urho3D::PODVector<int> getJsonIntArray(
Urho3D::JSONValue const& json,
Urho3D::String const& error_prefix,
unsigned min_size_limit,
unsigned max_size_limit
)
{
if (!json.IsArray()) {
throw JsonValidatorError(error_prefix + "Must be an array!");
}
Urho3D::JSONArray arr = json.GetArray();
if (arr.Size() < min_size_limit) {
throw JsonValidatorError(error_prefix + "Must have at least " + Urho3D::String(min_size_limit) + " items!");
}
if (arr.Size() > max_size_limit) {
throw JsonValidatorError(error_prefix + "Must have a maximum of " + Urho3D::String(min_size_limit) + " items!");
}
Urho3D::PODVector<int> result;
for (auto item : arr) {
result.Push(jsonToInt(item));
}
return result;
}
Urho3D::PODVector<int> getJsonIntArray(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix,
unsigned min_size_limit,
unsigned max_size_limit
)
{
Urho3D::JSONArray arr = getJsonArray(json, key, error_prefix, min_size_limit, max_size_limit);
Urho3D::PODVector<int> result;
for (auto item : arr) {
result.Push(jsonToInt(item));
}
return result;
}
Urho3D::PODVector<int> getJsonIntArrayIfExists(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix,
unsigned min_size_limit,
unsigned max_size_limit,
Urho3D::PODVector<int> const& default_value
)
{
if (!json.IsObject()) {
throw JsonValidatorError(error_prefix + "Unable to get a member from non-object!");
}
if (!json.Contains(key)) {
return default_value;
}
return getJsonIntArray(json, key, error_prefix, min_size_limit, max_size_limit);
}
Urho3D::PODVector<float> getJsonFloatArray(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix,
unsigned min_size_limit,
unsigned max_size_limit
)
{
Urho3D::JSONArray arr = getJsonArray(json, key, error_prefix, min_size_limit, max_size_limit);
Urho3D::PODVector<float> result;
for (auto item : arr) {
result.Push(jsonToFloat(item));
}
return result;
}
Urho3D::PODVector<float> getJsonFloatArrayIfExists(
Urho3D::JSONValue const& json,
Urho3D::String const& key,
Urho3D::String const& error_prefix,
unsigned min_size_limit,
unsigned max_size_limit,
Urho3D::PODVector<float> const& default_value
)
{
if (!json.IsObject()) {
throw JsonValidatorError(error_prefix + "Unable to get a member from non-object!");
}
if (!json.Contains(key)) {
return default_value;
}
return getJsonFloatArray(json, key, error_prefix, min_size_limit, max_size_limit);
}
}