-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclaujson_value.cpp
830 lines (683 loc) · 17.1 KB
/
claujson_value.cpp
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
#include "claujson.h"
namespace claujson {
extern Log log;
_Value _Value::clone() const {
if (!is_valid()) {
return _Value(nullptr, false);
}
_Value x;
x._type = this->_type;
if (x.is_str()) {
x._str_val = (this->_str_val);
}
else {
x._int_val = this->_int_val;
}
if (x.is_array()) {
x._array_ptr = this->as_array()->clone();
}
else if (x.is_object()) {
x._obj_ptr = this->as_object()->clone();
}
return x;
}
_Value::operator bool() const {
return this->is_valid();
}
_Value::_Value(Array* x) {
this->_int_val = 0;
this->_type = _ValueType::ARRAY;
this->_array_ptr = (x);
}
_Value::_Value(Object* x) {
this->_int_val = 0;
this->_type = _ValueType::OBJECT;
this->_obj_ptr = (x);
}
_Value::_Value(PartialJson* x) {
this->_int_val = 0;
this->_type = _ValueType::PARTIAL_JSON;
this->_pj_ptr = (x);
}
_Value::_Value(StructuredPtr x) {
this->_int_val = 0;
if (x.is_array()) {
this->_type = _ValueType::ARRAY;
this->_array_ptr = x.arr;
}
else if (x.is_object()) {
this->_type = _ValueType::OBJECT;
this->_obj_ptr = x.obj;
}
else if (x.is_partial_json()) {
this->_type = _ValueType::PARTIAL_JSON;
this->_pj_ptr = x.pj;
}
else {
this->_type = _ValueType::ERROR;
}
}
_Value::_Value(int x) {
this->_int_val = 0;
this->_type = _ValueType::NONE;
set_int(x);
}
_Value::_Value(unsigned int x) {
this->_int_val = 0;
this->_type = _ValueType::NONE;
set_uint(x);
}
_Value::_Value(int64_t x) {
this->_int_val = 0;
this->_type = _ValueType::NONE;
set_int(x);
}
_Value::_Value(uint64_t x) {
this->_int_val = 0;
this->_type = _ValueType::NONE;
set_uint(x);
}
_Value::_Value(double x) {
this->_int_val = 0;
this->_type = _ValueType::NONE;
set_float(x);
}
_Value::_Value(StringView x) {
this->_int_val = 0;
this->_type = _ValueType::NONE;
if (!set_str(x.data(), x.size())) {
set_type(_ValueType::NOT_VALID);
}
}
#if __cpp_lib_char8_t
// C++20~
_Value::_Value(std::u8string_view x) {
this->_int_val = 0;
this->_type = _ValueType::NONE;
if (!set_str(reinterpret_cast<const char*>(x.data()), x.size())) {
set_type(_ValueType::NOT_VALID);
}
}
_Value::_Value(const char8_t* x) {
std::u8string_view sv(x);
this->_int_val = 0;
this->_type = _ValueType::NONE;
if (!set_str(reinterpret_cast<const char*>(sv.data()), sv.size())) {
set_type(_ValueType::NOT_VALID);
}
}
#endif
_Value::_Value(const char* x) {
this->_int_val = 0;
this->_type = _ValueType::NONE;
if (!set_str(x, strlen(x))) {
set_type(_ValueType::NOT_VALID);
}
}
_Value::_Value(bool x) {
this->_int_val = 0;
this->_type = _ValueType::NONE;
set_bool(x);
}
_Value::_Value(std::nullptr_t x) {
this->_int_val = 0;
this->_type = _ValueType::NONE;
set_type(_ValueType::NULL_);
}
_Value::_Value(std::nullptr_t, bool valid) {
this->_int_val = 0;
this->_type = _ValueType::NONE;
set_type(_ValueType::NULL_);
if (!valid) {
set_type(_ValueType::NOT_VALID);
}
}
_ValueType _Value::type() const {
return _type;
}
bool _Value::is_valid() const {
return type() != _ValueType::NOT_VALID && type() != _ValueType::ERROR;
}
bool _Value::is_null() const {
return is_valid() && type() == _ValueType::NULL_;
}
bool _Value::is_primitive() const {
return is_valid() && !is_structured();
}
bool _Value::is_structured() const {
return is_valid() && (type() == _ValueType::ARRAY || type() == _ValueType::OBJECT);
}
bool _Value::is_array() const {
return is_valid() && type() == _ValueType::ARRAY;
}
bool _Value::is_object() const {
return is_valid() && type() == _ValueType::OBJECT;
}
bool _Value::is_partial_json() const {
return is_valid() && type() == _ValueType::PARTIAL_JSON;
}
bool _Value::is_int() const {
return is_valid() && type() == _ValueType::INT;
}
bool _Value::is_uint() const {
return is_valid() && type() == _ValueType::UINT;
}
bool _Value::is_float() const {
return is_valid() && type() == _ValueType::FLOAT;
}
bool _Value::is_bool() const {
return is_valid() && (type() == _ValueType::BOOL);
}
bool _Value::is_str() const {
return is_valid() && (type() == _ValueType::STRING || type() == _ValueType::SHORT_STRING);
}
int64_t _Value::int_val() const {
return _int_val;
}
uint64_t _Value::uint_val() const {
return _uint_val;
}
double _Value::float_val() const {
return _float_val;
}
int64_t& _Value::int_val() {
return _int_val;
}
uint64_t& _Value::uint_val() {
return _uint_val;
}
double& _Value::float_val() {
return _float_val;
}
bool _Value::bool_val() const {
if (!is_bool()) {
return false;
}
return _bool_val;
}
bool& _Value::bool_val() {
return _bool_val;
}
// this _Value is Array or Object.
_Value& _Value::json_pointerB(const std_vector<_Value>& routeVec) {
static _Value unvalid_data(nullptr, false);
if (is_structured() == false) {
return unvalid_data;
}
// the whole document.
if (routeVec.empty()) {
return *this;
}
// 4. find Data with route. and return
_Value* data = this;
for (uint64_t i = 0; i < routeVec.size(); ++i) {
bool fail = false;
auto& x = routeVec[i];
if (fail) {
// log << warn..
return unvalid_data;
}
if (data->is_primitive()) {
return unvalid_data;
}
if (data->is_array()) { // array -> with idx
Array* j = data->as_array();
uint64_t idx = x.get_unsigned_integer();
//bool found = false;
//uint64_t arr_size = j->get_data_size();
data = &j->get_value_list(idx);
}
else if (data->is_object()) { // object -> with key
Object* j = data->as_object();
data = &((*j)[x]);
}
}
return *data;
}
const _Value& _Value::json_pointerB(const std_vector<_Value>& routeVec) const { // option-> StringView route?
static _Value unvalid_data(nullptr, false);
if (is_structured() == false) {
return unvalid_data;
}
// the whole document.
if (routeVec.empty()) {
return *this;
}
// 4. find Data with route. and return
const _Value* data = this;
for (uint64_t i = 0; i < routeVec.size(); ++i) {
bool fail = false;
auto& x = routeVec[i];
if (fail) {
// log << warn..
return unvalid_data;
}
if (data->is_primitive()) {
return unvalid_data;
}
if (data->is_array()) { // array -> with idx
const Array* j = data->as_array();
uint64_t idx = x.get_unsigned_integer();
//bool found = false;
//uint64_t arr_size = j->get_data_size();
data = &j->get_value_list(idx);
}
else if (data->is_object()) { // object -> with key
const Object* j = data->as_object();
data = &((*j)[x]);
}
}
return *data;
}
void _Value::clear(bool remove_str) {
if (remove_str && is_str()) {
_str_val.clear();
_int_val = 0;
temp = 0;
_type = _ValueType::NONE;
}
else if (is_str()) {
//
}
else {
_int_val = 0;
temp = 0;
_type = _ValueType::NONE;
}
}
String& _Value::str_val() {
// type check...
return _str_val;
}
const String& _Value::str_val() const {
// type check...
return _str_val;
}
void _Value::set_int(long long x) {
if (!is_valid()) {
return;
}
if (is_str()) {
_str_val.clear();
}
_int_val = x;
_type = _ValueType::INT;
}
void _Value::set_uint(unsigned long long x) {
if (!is_valid()) {
return;
}
if (is_str()) {
_str_val.clear();
}
_uint_val = x;
_type = _ValueType::UINT;
}
void _Value::set_float(double x) {
if (!is_valid()) {
return;
}
if (is_str()) {
_str_val.clear();
}
_float_val = x;
_type = _ValueType::FLOAT;
}
bool _Value::set_str(const char* str, uint64_t len) {
bool convert = true;
if (!is_valid()) {
return false;
}
if (!convert) {
_str_val = String(str, Static_Cast<uint64_t, uint32_t>(len));
return true;
}
const uint64_t block_size = 1024;
uint8_t buf_src[block_size + _simdjson::_SIMDJSON_PADDING];
uint8_t buf_dest[block_size + _simdjson::_SIMDJSON_PADDING];
if (len >= block_size) {
uint8_t* buf_src = (uint8_t*)calloc(len + 1 + _simdjson::_SIMDJSON_PADDING, sizeof(uint8_t));
uint8_t* buf_dest = (uint8_t*)calloc(len + 1 + _simdjson::_SIMDJSON_PADDING, sizeof(uint8_t));
if (!buf_src || !buf_dest) {
if (buf_src) { free(buf_src); }
if (buf_dest) { free(buf_dest); }
return false;
}
memset(buf_src, '"', len + 1 + _simdjson::_SIMDJSON_PADDING);
memset(buf_dest, '"', len + 1 + _simdjson::_SIMDJSON_PADDING);
memcpy(buf_src, str, len);
buf_src[len] = '"';
// chk... fallback..
{
bool valid = _simdjson::validate_utf8(reinterpret_cast<char*>(buf_src), len);
if (!valid) {
free(buf_src);
free(buf_dest);
log << warn << "not valid utf8" << "\n";
log << warn << "Error in Convert for string, validate...";
return false;
}
}
auto* x = _simdjson::parse_string(buf_src, buf_dest, false);
if (x == nullptr) {
free(buf_src);
free(buf_dest);
log << warn << "Error in Convert for string";
return false;
}
else {
*x = '\0';
uint32_t string_length = uint32_t(x - buf_dest);
if (is_str() == false) {
_str_val = String((char*)buf_dest, string_length);
}
else {
_str_val = String((char*)buf_dest, string_length);
}
}
free(buf_src);
free(buf_dest);
}
else {
memset(buf_src, '"', block_size + _simdjson::_SIMDJSON_PADDING);
memset(buf_dest, '"', block_size + _simdjson::_SIMDJSON_PADDING);
memcpy(buf_src, str, len);
buf_src[len] = '"';
{
bool valid = _simdjson::validate_utf8(reinterpret_cast<char*>(buf_src), len);
if (!valid) {
log << warn << "not valid utf8" << "\n";
log << warn << "Error in Convert for string, validate...";
return false;
}
}
auto* x = _simdjson::parse_string(buf_src, buf_dest, false);
if (x == nullptr) {
log << warn << "Error in Convert for string";
return false;
}
else {
*x = '\0';
uint32_t string_length = uint32_t(x - buf_dest);
if (!is_str()) {
_str_val = String((char*)buf_dest, string_length);
}
else {
_str_val = String((char*)buf_dest, string_length);
}
}
}
//_type = _ValueType::STRING;
return true;
}
bool _Value::set_str(String str) {
if (!is_valid()) {
return false;
}
if (is_str()) {
_str_val.clear();
}
_str_val = std::move(str);
return true;
}
void _Value::set_str_in_parse(const char* str, uint64_t len) {
_str_val = String(str, Static_Cast<uint64_t, uint32_t>(len));
}
void _Value::set_bool(bool x) {
if (!is_valid()) {
return;
}
if (is_str()) {
_str_val.clear();
}
_bool_val = x;
{
set_type(_ValueType::BOOL);
}
}
void _Value::set_null() {
if (!is_valid()) {
return;
}
if (is_str()) {
_str_val.clear();
}
set_type(_ValueType::NULL_);
}
void _Value::set_type(_ValueType type) {
this->_type = type;
}
_Value::~_Value() {
if (is_str()) {
_str_val.clear();
}
}
_Value::_Value(_Value&& other) noexcept
: _type(_ValueType::NONE)
{
if (!other.is_valid()) {
return;
}
if (other.is_str()) {
_str_val = std::move(other._str_val);
}
else {
std::swap(_int_val, other._int_val);
std::swap(this->_type, other._type);
}
}
_Value::_Value() : _int_val(0), _type(_ValueType::NONE) {}
bool _Value::operator==(const _Value& other) const { // chk array or object?
if (this->_type == other._type) {
switch (this->_type) {
case _ValueType::STRING:
case _ValueType::SHORT_STRING:
return this->_str_val == other._str_val;
break;
case _ValueType::INT:
return this->_int_val == other._int_val;
break;
case _ValueType::UINT:
return this->_uint_val == other._uint_val;
break;
case _ValueType::FLOAT:
return this->_float_val == other._float_val;
break;
case _ValueType::BOOL:
return this->_bool_val == other._bool_val;
break;
case _ValueType::ARRAY:
{
const Array* j = this->as_array();
const Array* k = other.as_array();
const uint64_t sz_j = j->get_data_size();
const uint64_t sz_k = k->get_data_size();
if (sz_j != sz_k) { return false; }
const uint64_t sz = sz_j;
if (j->is_array() && k->is_array()) {
for (uint64_t i = 0; i < sz; ++i) {
if (j->get_value_list(i) != k->get_value_list(i)) {
return false;
}
}
}
}
break;
case _ValueType::OBJECT:
{
const Object* j = this->as_object();
const Object* k = other.as_object();
const uint64_t sz_j = j->get_data_size();
const uint64_t sz_k = k->get_data_size();
if (sz_j != sz_k) { return false; }
const uint64_t sz = sz_j;
if (j->is_object() && k->is_object()) {
for (uint64_t i = 0; i < sz; ++i) {
if (j->get_value_list(i) != k->get_value_list(i)) {
return false;
}
if (j->get_key_list(i) != k->get_key_list(i)) {
return false;
}
}
}
else {
return false;
}
}
break;
}
return true;
}
return false;
}
bool _Value::operator!=(const _Value& other) const {
return !((*this) == other);
}
bool _Value::operator<(const _Value& other) const {
if (this->_type == other._type) {
switch (this->_type) {
case _ValueType::STRING:
case _ValueType::SHORT_STRING:
return this->_str_val < other._str_val;
break;
case _ValueType::INT:
return this->_int_val < other._int_val;
break;
case _ValueType::UINT:
return this->_uint_val < other._uint_val;
break;
case _ValueType::FLOAT:
return this->_float_val < other._float_val;
break;
case _ValueType::BOOL:
return this->_bool_val < other._bool_val;
break;
}
}
return false;
}
_Value& _Value::operator=(_Value&& other) noexcept {
if (this == &other) {
return *this;
}
if (!is_valid()) {
return *this;
}
std::swap(this->_type, other._type);
std::swap(this->_int_val, other._int_val);
std::swap(this->temp, other.temp);
clean(other);
return *this;
}
StructuredPtr _Value::as_structured() {
return StructuredPtr{ as_array(), as_object(), as_partial_json() };
}
bool _Value::is_virtual() const {
if (is_array()) {
return as_array()->is_virtual();
}
if (is_object()) {
return as_object()->is_virtual();
}
return false;
}
Array* _Value::as_array() {
if (is_array()) {
return static_cast<Array*>(_array_ptr);
}
return nullptr;
}
Object* _Value::as_object() {
if (is_object()) {
return static_cast<Object*>(_obj_ptr);
}
return nullptr;
}
PartialJson* _Value::as_partial_json() {
if (is_partial_json()) {
return static_cast<PartialJson*>(_pj_ptr);
}
return nullptr;
}
StructuredPtr _Value::as_structured_ptr() {
if (is_array()) {
return { _array_ptr };
}
else if (is_object()) {
return { _obj_ptr };
}
else if (is_partial_json()) {
return { _pj_ptr };
}
return { nullptr };
}
const Array* _Value::as_array() const {
if (is_array()) {
return const_cast<const Array*>(_array_ptr);
}
return nullptr;
}
const Object* _Value::as_object() const {
if (is_object()) {
return const_cast<const Object*>(_obj_ptr);
}
return nullptr;
}
const PartialJson* _Value::as_partial_json() const {
if (is_partial_json()) {
return const_cast<const PartialJson*>(_pj_ptr);
}
return nullptr;
}
const StructuredPtr _Value::as_structured_ptr()const {
if (is_array()) {
return { _array_ptr };
}
else if (is_object()) {
return { _obj_ptr };
}
else if (is_partial_json()) {
return { _pj_ptr };
}
return { nullptr };
}
_Value& _Value::operator[](uint64_t idx) {
static _Value empty_value(nullptr, false);
if (is_array()) {
return as_array()->operator[](idx);
}
if (is_object()) {
return as_object()->operator[](idx);
}
return empty_value;
}
const _Value& _Value::operator[](uint64_t idx) const {
static const _Value empty_value(nullptr, false);
if (is_array()) {
return as_array()->operator[](idx);
}
if (is_object()) {
return as_object()->operator[](idx);
}
return empty_value;
}
uint64_t _Value::find(const _Value& key) const { // find without key`s converting?
if (is_object()) {
return as_object()->find(key);
}
return npos;
}
_Value& _Value::operator[](const _Value& key) { // if not exist key, then nothing.
if (is_object()) {
return as_object()->operator[](key);
}
return empty_value;
}
const _Value& _Value::operator[](const _Value& key) const { // if not exist key, then nothing.
if (is_object()) {
return as_object()->operator[](key);
}
return empty_value;
}
}