-
Notifications
You must be signed in to change notification settings - Fork 0
/
claujson.cpp
6786 lines (5423 loc) · 161 KB
/
claujson.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
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "claujson.h"
#include <future>
#include <set>
#include <execution>
#include "fmt/format.h"
#define claujson_inline _simdjson_inline
#define ERROR(msg) \
do { \
throw msg; \
/* error.make(__LINE__, StringView(msg)); */ \
} while (false)
#if __cpp_lib_string_view
#else
claujson::StringView operator""sv(const char* str, size_t sz) {
return claujson::StringView(str, sz);
}
uint64_t claujson::StringView::npos = -1;
bool operator==(const std::string& str, claujson::StringView sv) {
return strcmp(str.data(), sv.data()) == 0;
}
#endif
namespace claujson {
// todo? make Document class? like simdjson?
_Value Structured::data_null{ nullptr, false }; // valid is false..
uint64_t Structured::npos = -1; //
Log log;
// class PartialJson, only used in class LoadData.
class PartialJson : public Structured {
protected:
std::vector<_Value> arr_vec;
//
std::vector<Pair<_Value, _Value>> obj_data;
_Value virtualJson;
public:
virtual ~PartialJson();
private:
friend class LoadData;
friend class LoadData2;
friend class Object;
friend class Array;
PartialJson();
public:
virtual bool is_partial_json() const;
virtual bool is_object() const;
virtual bool is_array() const;
virtual uint64_t get_data_size() const;
virtual _Value& get_value_list(uint64_t idx);
private:
virtual _Value& get_key_list(uint64_t idx);
public:
virtual const _Value& get_value_list(uint64_t idx) const;
virtual const _Value& get_key_list(uint64_t idx) const;
virtual const _Value& get_const_key_list(uint64_t idx);
virtual const _Value& get_const_key_list(uint64_t idx) const;
virtual void clear(uint64_t idx);
virtual bool is_virtual() const;
virtual void clear();
virtual void reserve_data_list(uint64_t len);
virtual bool add_object_element(Value key, Value val);
virtual bool add_array_element(Value val);
virtual bool add_array(Ptr<Structured> arr);
virtual bool add_object(Ptr<Structured> obj);
virtual bool add_array(Value key, Ptr<Structured> arr);
virtual bool add_object(Value key, Ptr<Structured> obj);
virtual bool assign_value_element(uint64_t idx, Value val);
virtual bool assign_key_element(uint64_t idx, Value key);
virtual void erase(const _Value& key, bool real = false);
virtual void erase(uint64_t idx, bool real = false);
private:
virtual void MergeWith(PtrWeak<Structured> j, int start_offset);
virtual void add_item_type(int64_t key_buf_idx, int64_t key_next_buf_idx, int64_t val_buf_idx, int64_t val_next_buf_idx,
char* buf, uint64_t key_token_idx, uint64_t val_token_idx);
virtual void add_item_type(int64_t val_buf_idx, int64_t val_next_buf_idx,
char* buf, uint64_t val_token_idx);
virtual void add_user_type(int64_t key_buf_idx, int64_t key_next_buf_idx, char* buf,
_ValueType type, uint64_t key_token_idx);
virtual void add_user_type(_ValueType type);
virtual bool add_user_type(Value key, Ptr<Structured> j);
virtual bool add_user_type(Ptr<Structured> j);
};
class VirtualObject : public Object {
public:
virtual bool is_virtual() const;
virtual ~VirtualObject();
};
class VirtualArray : public Array {
public:
virtual bool is_virtual() const;
virtual ~VirtualArray();
};
std::ostream& operator<<(std::ostream& stream, const claujson::_Value& data) {
if (false == data.is_valid()) {
stream << "--not valid\n";
return stream;
}
switch (data._type) {
case claujson::_ValueType::INT:
stream << data._int_val;
break;
case claujson::_ValueType::UINT:
stream << data._uint_val;
break;
case claujson::_ValueType::FLOAT:
stream << data._float_val;
break;
case claujson::_ValueType::STRING:
case claujson::_ValueType::SHORT_STRING:
stream << "\"" << (data._str_val.data()) << "\"";
break;
case claujson::_ValueType::BOOL:
stream << data._bool_val;
break;
case claujson::_ValueType::NULL_:
stream << "null";
break;
case claujson::_ValueType::ARRAY:
case claujson::_ValueType::OBJECT:
{
// stream << "array_or_object";
auto* x = data.as_structured_ptr();
if (x->is_array()) {
stream << "[ ";
uint64_t sz = x->get_data_size();
for (uint64_t i = 0; i < sz; ++i) {
stream << x->get_value_list(i) << " ";
if (i < sz - 1) {
stream << " , ";
}
}
stream << "]\n";
}
else if (x->is_object()) {
stream << "{ ";
uint64_t sz = x->get_data_size();
for (uint64_t i = 0; i < sz; ++i) {
stream << x->get_key_list(i) << " : ";
stream << x->get_value_list(i) << " ";
if (i < sz - 1) {
stream << " , ";
}
}
stream << "}\n";
}
}
break;
}
return stream;
}
}
namespace claujson {
_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_structured()) {
x._array_or_object_ptr = this->as_structured_ptr()->clone();
}
return x;
}
_Value::operator bool() const {
return this->is_valid();
}
_Value::_Value(Structured* x) {
this->_int_val = 0;
this->_type = _ValueType::NONE;
set_ptr(x);
}
_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 {
auto* x = this->as_structured_ptr();
if (x) {
return x->is_array();
}
return false;
}
bool _Value::is_object() const {
auto* x = this->as_structured_ptr();
if (x) {
return x->is_object();
}
return false;
}
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;
}
claujson_inline StringView sub_route(StringView route, uint64_t found_idx, uint64_t new_idx) {
if (found_idx + 1 == new_idx) {
return ""sv;
}
return route.substr(found_idx + 1, new_idx - found_idx - 1);
}
#if __cpp_lib_char8_t
claujson_inline std::u8string_view sub_route(std::u8string_view route, uint64_t found_idx, uint64_t new_idx) {
if (found_idx + 1 == new_idx) {
return u8""sv;
}
return route.substr(found_idx + 1, new_idx - found_idx - 1);
}
#endif
claujson_inline bool to_uint_for_json_pointer(StringView x, uint64_t* val, _simdjson::internal::dom_parser_implementation* simdjson_imple) {
const char* buf = x.data();
uint64_t idx = 0;
uint64_t idx2 = x.size();
switch (x[0]) {
case '0':
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
std::unique_ptr<uint8_t[]> copy;
uint64_t temp[2] = { 0 };
const uint8_t* value = reinterpret_cast<const uint8_t*>(buf + idx);
{ // chk code...
copy = std::unique_ptr<uint8_t[]>(new (std::nothrow) uint8_t[idx2 - idx + _simdjson::_SIMDJSON_PADDING]); // x.size() + padding
if (copy.get() == nullptr) { return false; } // cf) new Json?
std::memcpy(copy.get(), &buf[idx], idx2 - idx);
std::memset(copy.get() + idx2 - idx, ' ', _simdjson::_SIMDJSON_PADDING);
value = copy.get();
}
auto x = simdjson_imple->parse_number(value, temp);
if (x != _simdjson::SUCCESS) {
log << warn << "parse number error. " << x << "\n";
return false;
}
long long int_val = 0;
unsigned long long uint_val = 0;
//double float_val = 0;
switch (static_cast<_simdjson::internal::tape_type>(temp[0] >> 56)) {
case _simdjson::internal::tape_type::INT64:
memcpy(&int_val, &temp[1], sizeof(uint64_t));
*val = int_val;
return true;
break;
case _simdjson::internal::tape_type::UINT64:
memcpy(&uint_val, &temp[1], sizeof(uint64_t));
*val = uint_val;
return true;
break;
case _simdjson::internal::tape_type::DOUBLE:
// error.
return false;
break;
}
}
break;
}
return false;
}
#if __cpp_lib_char8_t
bool to_uint_for_json_pointer(std::u8string_view x, uint64_t* val, _simdjson::internal::dom_parser_implementation* simdjson_imple) {
auto* buf = x.data();
uint64_t idx = 0;
uint64_t idx2 = x.size();
switch (x[0]) {
case '0':
case '1': case '2': case '3': case '4':
case '5': case '6': case '7': case '8': case '9':
{
std::unique_ptr<uint8_t[]> copy;
uint64_t temp[2] = { 0 };
const uint8_t* value = reinterpret_cast<const uint8_t*>(buf + idx);
{ // chk code...
copy = std::unique_ptr<uint8_t[]>(new (std::nothrow) uint8_t[idx2 - idx + _simdjson::_SIMDJSON_PADDING]); // x.size() + padding
if (copy.get() == nullptr) { return false; } // cf) new Json?
std::memcpy(copy.get(), &buf[idx], idx2 - idx);
std::memset(copy.get() + idx2 - idx, ' ', _simdjson::_SIMDJSON_PADDING);
value = copy.get();
}
if (auto x = simdjson_imple->parse_number(value, temp)
; x != _simdjson::SUCCESS) {
log << warn << "parse number error. " << x << "\n";
return false;
}
long long int_val = 0;
unsigned long long uint_val = 0;
//double float_val = 0;
switch (static_cast<_simdjson::internal::tape_type>(temp[0] >> 56)) {
case _simdjson::internal::tape_type::INT64:
memcpy(&int_val, &temp[1], sizeof(uint64_t));
*val = int_val;
return true;
break;
case _simdjson::internal::tape_type::UINT64:
memcpy(&uint_val, &temp[1], sizeof(uint64_t));
*val = uint_val;
return true;
break;
case _simdjson::internal::tape_type::DOUBLE:
// error.
return false;
break;
}
}
break;
}
return false;
}
#endif
// 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;
}
Structured* j = data->as_structured_ptr();
if (j->is_array()) { // array -> with idx
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 (j->is_object()) { // object -> with key
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;
}
const Structured* j = data->as_structured_ptr();
if (j->is_array()) { // array -> with idx
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 (j->is_object()) { // object -> with key
data = &((*j)[x]);
}
}
return *data;
}
void _Value::clear(bool remove_str) {
if (remove_str && is_str()) {
_str_val.clear();
_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_ptr(Structured* x) {
if (!is_valid()) {
return;
}
if (this->is_str()) {
_str_val.clear();
}
_array_or_object_ptr = x;
// _ValueType::ERROR -> _ValueType::NOT_ARRAY_AND_OBJECT ?
_type = x->is_array() ? _ValueType::ARRAY : (x->is_object() ? _ValueType::OBJECT : _ValueType::ERROR); // chk change DataType:: ~~ -> DataType:: ~~
}
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;
}
void _Value::set_str_in_parse(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:
case _ValueType::OBJECT:
{
const Structured* j = this->as_structured_ptr();
const Structured* k = other.as_structured_ptr();
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;
}
}
}
else 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;
}