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
669
670
671
672
673
|
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/core/utils/Document.h>
#include <iterator>
#include <algorithm>
#include <aws/core/utils/memory/stl/AWSStringStream.h>
#include <aws/core/utils/StringUtils.h>
#include <aws/core/utils/json/JsonSerializer.h>
using namespace Aws::Utils;
Document::Document() : m_wasParseSuccessful(true)
{
m_json = nullptr;
}
Document::Document(cJSON* value) :
m_json(cJSON_AS4CPP_Duplicate(value, true /* recurse */)),
m_wasParseSuccessful(true)
{
}
Document::Document(const Aws::String& value) : m_wasParseSuccessful(true)
{
const char* return_parse_end;
m_json = cJSON_AS4CPP_ParseWithOpts(value.c_str(), &return_parse_end, 1/*require_null_terminated*/);
if (!m_json || cJSON_AS4CPP_IsInvalid(m_json))
{
m_wasParseSuccessful = false;
m_errorMessage = "Failed to parse JSON at: ";
m_errorMessage += return_parse_end;
}
}
Document::Document(Aws::IStream& istream) : m_wasParseSuccessful(true)
{
Aws::StringStream memoryStream;
std::copy(std::istreambuf_iterator<char>(istream), std::istreambuf_iterator<char>(), std::ostreambuf_iterator<char>(memoryStream));
const char* return_parse_end;
const auto input = memoryStream.str();
m_json = cJSON_AS4CPP_ParseWithOpts(input.c_str(), &return_parse_end, 1/*require_null_terminated*/);
if (!m_json || cJSON_AS4CPP_IsInvalid(m_json))
{
m_wasParseSuccessful = false;
m_errorMessage = "Failed to parse JSON. Invalid input at: ";
m_errorMessage += return_parse_end;
}
}
Document::Document(const Document& value) :
m_json(cJSON_AS4CPP_Duplicate(value.m_json, true/*recurse*/)),
m_wasParseSuccessful(value.m_wasParseSuccessful),
m_errorMessage(value.m_errorMessage)
{
}
Document::Document(Document&& value) :
m_json(value.m_json),
m_wasParseSuccessful(value.m_wasParseSuccessful),
m_errorMessage(std::move(value.m_errorMessage))
{
value.m_json = nullptr;
}
Document::Document(const Json::JsonView& view) :
m_json(cJSON_AS4CPP_Duplicate(view.m_value, true/*recurse*/)),
m_wasParseSuccessful(true),
m_errorMessage({})
{
}
void Document::Destroy()
{
cJSON_AS4CPP_Delete(m_json);
}
Document::~Document()
{
Destroy();
}
Document& Document::operator=(const Document& other)
{
if (this == &other)
{
return *this;
}
Destroy();
m_json = cJSON_AS4CPP_Duplicate(other.m_json, true /*recurse*/);
m_wasParseSuccessful = other.m_wasParseSuccessful;
m_errorMessage = other.m_errorMessage;
return *this;
}
Document& Document::operator=(Document&& other)
{
if (this == &other)
{
return *this;
}
using std::swap;
swap(m_json, other.m_json);
swap(m_errorMessage, other.m_errorMessage);
m_wasParseSuccessful = other.m_wasParseSuccessful;
return *this;
}
Document& Document::operator=(const Json::JsonView& other)
{
Destroy();
m_json = cJSON_AS4CPP_Duplicate(other.m_value, true /*recurse*/);
m_wasParseSuccessful = true;
m_errorMessage = {};
return *this;
}
bool Document::operator==(const Document& other) const
{
return cJSON_AS4CPP_Compare(m_json, other.m_json, true /*case-sensitive*/) != 0;
}
bool Document::operator!=(const Document& other) const
{
return !(*this == other);
}
static void AddOrReplace(cJSON* root, const char* key, cJSON* value)
{
const auto existing = cJSON_AS4CPP_GetObjectItemCaseSensitive(root, key);
if (existing)
{
cJSON_AS4CPP_ReplaceItemInObjectCaseSensitive(root, key, value);
}
else
{
cJSON_AS4CPP_AddItemToObject(root, key, value);
}
}
Document& Document::WithString(const char* key, const Aws::String& value)
{
if (!m_json)
{
m_json = cJSON_AS4CPP_CreateObject();
}
const auto val = cJSON_AS4CPP_CreateString(value.c_str());
AddOrReplace(m_json, key, val);
return *this;
}
Document& Document::WithString(const Aws::String& key, const Aws::String& value)
{
return WithString(key.c_str(), value);
}
Document& Document::AsString(const Aws::String& value)
{
Destroy();
m_json = cJSON_AS4CPP_CreateString(value.c_str());
return *this;
}
Document& Document::WithBool(const char* key, bool value)
{
if (!m_json)
{
m_json = cJSON_AS4CPP_CreateObject();
}
const auto val = cJSON_AS4CPP_CreateBool(value);
AddOrReplace(m_json, key, val);
return *this;
}
Document& Document::WithBool(const Aws::String& key, bool value)
{
return WithBool(key.c_str(), value);
}
Document& Document::AsBool(bool value)
{
Destroy();
m_json = cJSON_AS4CPP_CreateBool(value);
return *this;
}
Document& Document::WithInteger(const char* key, int value)
{
return WithDouble(key, static_cast<double>(value));
}
Document& Document::WithInteger(const Aws::String& key, int value)
{
return WithDouble(key.c_str(), static_cast<double>(value));
}
Document& Document::AsInteger(int value)
{
Destroy();
m_json = cJSON_AS4CPP_CreateNumber(static_cast<double>(value));
return *this;
}
Document& Document::WithInt64(const char* key, long long value)
{
if (!m_json)
{
m_json = cJSON_AS4CPP_CreateObject();
}
const auto val = cJSON_AS4CPP_CreateInt64(value);
AddOrReplace(m_json, key, val);
return *this;
}
Document& Document::WithInt64(const Aws::String& key, long long value)
{
return WithInt64(key.c_str(), value);
}
Document& Document::AsInt64(long long value)
{
Destroy();
m_json = cJSON_AS4CPP_CreateInt64(value);
return *this;
}
Document& Document::WithDouble(const char* key, double value)
{
if (!m_json)
{
m_json = cJSON_AS4CPP_CreateObject();
}
const auto val = cJSON_AS4CPP_CreateNumber(value);
AddOrReplace(m_json, key, val);
return *this;
}
Document& Document::WithDouble(const Aws::String& key, double value)
{
return WithDouble(key.c_str(), value);
}
Document& Document::AsDouble(double value)
{
Destroy();
m_json = cJSON_AS4CPP_CreateNumber(value);
return *this;
}
Document& Document::WithArray(const char* key, const Array<Aws::String>& array)
{
if (!m_json)
{
m_json = cJSON_AS4CPP_CreateObject();
}
auto arrayValue = cJSON_AS4CPP_CreateArray();
for (unsigned i = 0; i < array.GetLength(); ++i)
{
cJSON_AS4CPP_AddItemToArray(arrayValue, cJSON_AS4CPP_CreateString(array[i].c_str()));
}
AddOrReplace(m_json, key, arrayValue);
return *this;
}
Document& Document::WithArray(const Aws::String& key, const Array<Aws::String>& array)
{
return WithArray(key.c_str(), array);
}
Document& Document::WithArray(const Aws::String& key, const Array<Document>& array)
{
if (!m_json)
{
m_json = cJSON_AS4CPP_CreateObject();
}
auto arrayValue = cJSON_AS4CPP_CreateArray();
for (unsigned i = 0; i < array.GetLength(); ++i)
{
cJSON_AS4CPP_AddItemToArray(arrayValue, cJSON_AS4CPP_Duplicate(array[i].m_json, true /*recurse*/));
}
AddOrReplace(m_json, key.c_str(), arrayValue);
return *this;
}
Document& Document::WithArray(const Aws::String& key, Array<Document>&& array)
{
if (!m_json)
{
m_json = cJSON_AS4CPP_CreateObject();
}
auto arrayValue = cJSON_AS4CPP_CreateArray();
for (unsigned i = 0; i < array.GetLength(); ++i)
{
cJSON_AS4CPP_AddItemToArray(arrayValue, array[i].m_json);
array[i].m_json = nullptr;
}
AddOrReplace(m_json, key.c_str(), arrayValue);
return *this;
}
Document& Document::AsArray(const Array<Document>& array)
{
auto arrayValue = cJSON_AS4CPP_CreateArray();
for (unsigned i = 0; i < array.GetLength(); ++i)
{
cJSON_AS4CPP_AddItemToArray(arrayValue, cJSON_AS4CPP_Duplicate(array[i].m_json, true /*recurse*/));
}
Destroy();
m_json = arrayValue;
return *this;
}
Document& Document::AsArray(Array<Document>&& array)
{
auto arrayValue = cJSON_AS4CPP_CreateArray();
for (unsigned i = 0; i < array.GetLength(); ++i)
{
cJSON_AS4CPP_AddItemToArray(arrayValue, array[i].m_json);
array[i].m_json = nullptr;
}
Destroy();
m_json = arrayValue;
return *this;
}
Document& Document::WithObject(const char* key, const Document& value)
{
if (!m_json)
{
m_json = cJSON_AS4CPP_CreateObject();
}
const auto copy = value.m_json == nullptr ? cJSON_AS4CPP_CreateObject() : cJSON_AS4CPP_Duplicate(value.m_json, true /*recurse*/);
AddOrReplace(m_json, key, copy);
return *this;
}
Document& Document::WithObject(const Aws::String& key, const Document& value)
{
return WithObject(key.c_str(), value);
}
Document& Document::WithObject(const char* key, Document&& value)
{
if (!m_json)
{
m_json = cJSON_AS4CPP_CreateObject();
}
AddOrReplace(m_json, key, value.m_json == nullptr ? cJSON_AS4CPP_CreateObject() : value.m_json);
value.m_json = nullptr;
return *this;
}
Document& Document::WithObject(const Aws::String& key, Document&& value)
{
return WithObject(key.c_str(), std::move(value));
}
Document& Document::AsObject(const Document& value)
{
*this = value;
return *this;
}
Document& Document::AsObject(Document && value)
{
*this = std::move(value);
return *this;
}
DocumentView Document::View() const
{
return *this;
}
DocumentView::DocumentView() : m_json(nullptr)
{
}
DocumentView::DocumentView(const Document& value) : m_json(value.m_json)
{
}
DocumentView::DocumentView(cJSON* v) : m_json(v)
{
}
DocumentView& DocumentView::operator=(const Document& value)
{
m_json = value.m_json;
return *this;
}
DocumentView& DocumentView::operator=(cJSON* value)
{
m_json = value;
return *this;
}
Aws::String DocumentView::GetString(const Aws::String& key) const
{
assert(m_json);
auto item = cJSON_AS4CPP_GetObjectItemCaseSensitive(m_json, key.c_str());
auto str = cJSON_AS4CPP_GetStringValue(item);
return str ? str : "";
}
Aws::String DocumentView::AsString() const
{
const char* str = cJSON_AS4CPP_GetStringValue(m_json);
if (str == nullptr)
{
return {};
}
return str;
}
bool DocumentView::IsString() const
{
return cJSON_AS4CPP_IsString(m_json) != 0;
}
bool DocumentView::GetBool(const Aws::String& key) const
{
assert(m_json);
auto item = cJSON_AS4CPP_GetObjectItemCaseSensitive(m_json, key.c_str());
assert(item);
return item->valueint != 0;
}
bool DocumentView::AsBool() const
{
assert(cJSON_AS4CPP_IsBool(m_json));
return cJSON_AS4CPP_IsTrue(m_json) != 0;
}
bool DocumentView::IsBool() const
{
return cJSON_AS4CPP_IsBool(m_json) != 0;
}
int DocumentView::GetInteger(const Aws::String& key) const
{
assert(m_json);
auto item = cJSON_AS4CPP_GetObjectItemCaseSensitive(m_json, key.c_str());
assert(item);
return item->valueint;
}
int DocumentView::AsInteger() const
{
assert(cJSON_AS4CPP_IsNumber(m_json)); // can be double or value larger than int_max, but at least not UB
return m_json->valueint;
}
bool DocumentView::IsIntegerType() const
{
if (!cJSON_AS4CPP_IsNumber(m_json))
{
return false;
}
if (m_json->valuestring)
{
Aws::String valueString = m_json->valuestring;
return std::all_of(valueString.begin(), valueString.end(), [](unsigned char c){ return ::isdigit(c) || c == '+' || c == '-'; });
}
return m_json->valuedouble == static_cast<long long>(m_json->valuedouble);
}
int64_t DocumentView::GetInt64(const Aws::String& key) const
{
assert(m_json);
auto item = cJSON_AS4CPP_GetObjectItemCaseSensitive(m_json, key.c_str());
assert(item);
if (item->valuestring)
{
return Aws::Utils::StringUtils::ConvertToInt64(item->valuestring);
}
else
{
return static_cast<int64_t>(item->valuedouble);
}
}
int64_t DocumentView::AsInt64() const
{
assert(cJSON_AS4CPP_IsNumber(m_json));
if (m_json->valuestring)
{
return Aws::Utils::StringUtils::ConvertToInt64(m_json->valuestring);
}
else
{
return static_cast<int64_t>(m_json->valuedouble);
}
}
double DocumentView::GetDouble(const Aws::String& key) const
{
assert(m_json);
auto item = cJSON_AS4CPP_GetObjectItemCaseSensitive(m_json, key.c_str());
assert(item);
return item->valuedouble;
}
double DocumentView::AsDouble() const
{
assert(cJSON_AS4CPP_IsNumber(m_json));
return m_json->valuedouble;
}
bool DocumentView::IsFloatingPointType() const
{
if (!cJSON_AS4CPP_IsNumber(m_json))
{
return false;
}
if (m_json->valuestring)
{
Aws::String valueString = m_json->valuestring;
return std::any_of(valueString.begin(), valueString.end(), [](unsigned char c){ return !::isdigit(c) && c != '+' && c != '-'; });
}
return m_json->valuedouble != static_cast<long long>(m_json->valuedouble);
}
Array<DocumentView> DocumentView::GetArray(const Aws::String& key) const
{
assert(m_json);
auto array = cJSON_AS4CPP_GetObjectItemCaseSensitive(m_json, key.c_str());
assert(cJSON_AS4CPP_IsArray(array));
Array<DocumentView> returnArray(cJSON_AS4CPP_GetArraySize(array));
auto element = array->child;
for (unsigned i = 0; element && i < returnArray.GetLength(); ++i, element = element->next)
{
returnArray[i] = element;
}
return returnArray;
}
Array<DocumentView> DocumentView::AsArray() const
{
assert(cJSON_AS4CPP_IsArray(m_json));
Array<DocumentView> returnArray(cJSON_AS4CPP_GetArraySize(m_json));
auto element = m_json->child;
for (unsigned i = 0; element && i < returnArray.GetLength(); ++i, element = element->next)
{
returnArray[i] = element;
}
return returnArray;
}
bool DocumentView::IsListType() const
{
return cJSON_AS4CPP_IsArray(m_json) != 0;
}
DocumentView DocumentView::GetObject(const Aws::String& key) const
{
assert(m_json);
auto item = cJSON_AS4CPP_GetObjectItemCaseSensitive(m_json, key.c_str());
return item;
}
DocumentView DocumentView::AsObject() const
{
assert(cJSON_AS4CPP_IsObject(m_json) || cJSON_AS4CPP_IsNull(m_json));
return m_json;
}
bool DocumentView::IsObject() const
{
return cJSON_AS4CPP_IsObject(m_json) != 0;
}
bool DocumentView::IsNull() const
{
return cJSON_AS4CPP_IsNull(m_json) != 0;
}
Aws::Map<Aws::String, DocumentView> DocumentView::GetAllObjects() const
{
Aws::Map<Aws::String, DocumentView> valueMap;
if (!m_json)
{
return valueMap;
}
for (auto iter = m_json->child; iter; iter = iter->next)
{
valueMap.emplace(std::make_pair(Aws::String(iter->string), DocumentView(iter)));
}
return valueMap;
}
bool DocumentView::ValueExists(const Aws::String& key) const
{
if (!cJSON_AS4CPP_IsObject(m_json))
{
return false;
}
auto item = cJSON_AS4CPP_GetObjectItemCaseSensitive(m_json, key.c_str());
return !(item == nullptr || cJSON_AS4CPP_IsNull(item));
}
bool DocumentView::KeyExists(const Aws::String& key) const
{
if (!cJSON_AS4CPP_IsObject(m_json))
{
return false;
}
return cJSON_AS4CPP_GetObjectItemCaseSensitive(m_json, key.c_str()) != nullptr;;
}
Aws::String DocumentView::WriteCompact() const
{
if (!m_json)
{
return "null";
}
auto temp = cJSON_AS4CPP_PrintUnformatted(m_json);
Aws::String out(temp);
cJSON_AS4CPP_free(temp);
return out;
}
Aws::String DocumentView::WriteReadable() const
{
if (!m_json)
{
return "null";
}
auto temp = cJSON_AS4CPP_Print(m_json);
Aws::String out(temp);
cJSON_AS4CPP_free(temp);
return out;
}
Document DocumentView::Materialize() const
{
return m_json;
}
|