-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclaujson.h
679 lines (508 loc) · 14.7 KB
/
claujson.h
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
#pragma once
// 64bit.. DO NOT build 32bit! //
#include "claujson_internal.h"
#include "claujson_string.h"
#include "thread_pool.h"
#include "_simdjson.h" // modified simdjson // using simdjson 3.9.1
namespace claujson {
class _Value;
class Array;
class Object;
class PartialJson;
class StructuredPtr;
class alignas(16) _Value {
public:
static _Value empty_value;
static const uint64_t npos;
public:
// todo - check type of Data....
// using INT_t = int64_t;
// using UINT_t = uint64_t;
// using FlOAT_t = double;
// using STR_t = std::string;
// using BOOL_t = bool;
public:
friend std::ostream& operator<<(std::ostream& stream, const _Value& data);
friend bool ConvertString(Arena* pool, _Value& data, const char* text, uint64_t len);
friend class Object;
friend class Array;
private:
// do not change!
union {
struct {
union {
int64_t _int_val;
uint64_t _uint_val;
double _float_val;
Array* _array_ptr;
Object* _obj_ptr;
PartialJson* _pj_ptr;
String* _str_val;
bool _bool_val;
};
uint32_t temp;
_ValueType _type;
};
};
/// before version..
//union {
// int64_t _int_val = 0;
// uint64_t _uint_val;
// double _float_val;
// std::string* _str_val;
// Structured* _array_or_object_ptr;
// bool _bool_val;
//};
//_ValueType _type = _ValueType::NONE;
//bool _valid = true;
public:
_Value clone(Arena* pool) const;
explicit operator bool() const;
explicit _Value(Array* x);
explicit _Value(Object* x);
explicit _Value(PartialJson* x);
explicit _Value(StructuredPtr x);
explicit _Value(int x);
explicit _Value(unsigned int x);
explicit _Value(int64_t x);
explicit _Value(uint64_t x);
explicit _Value(double x);
explicit _Value(Arena* pool, StringView x);
#if __cpp_lib_char8_t
// C++20~
explicit _Value(Arena* pool, std::u8string_view x);
explicit _Value(Arena* pool, const char8_t* x);
#endif
private:
_Value(const char* x) = delete;
public:
explicit _Value(Arena* pool, const char* x);
explicit _Value(_Value*) = delete;
explicit _Value(bool x);
explicit _Value(std::nullptr_t x);
explicit _Value(std::nullptr_t, bool valid);
explicit _Value(String&& x) {
*this->_str_val = std::move(x);
}
public:
_ValueType type() const;
bool is_valid() const;
bool is_null() const;
bool is_primitive() const; // int, uint, float, bool(true, false), string, null
bool is_structured() const; // array or object (or used in inner, partialjson )
bool is_array() const;
bool is_object() const;
bool is_partial_json() const;
bool is_int() const;
bool is_uint() const;
bool is_float() const;
bool is_number() const {
return is_valid() && (is_int() || is_uint() || is_float());
}
bool is_bool() const;
bool is_str() const;
int64_t get_integer() const {
return int_val();
}
int64_t& get_integer() {
return int_val();
}
int64_t int_val() const;
uint64_t get_unsigned_integer() const {
return uint_val();
}
uint64_t& get_unsigned_integer() {
return uint_val();
}
uint64_t uint_val() const;
double get_floating() const {
return float_val();
}
double& get_floating() {
return float_val();
}
template <typename T>
T get_number() const {
if (is_float()) {
return static_cast<T>(_float_val);
}
return static_cast<T>(_uint_val);
}
double float_val() const;
int64_t& int_val();
uint64_t& uint_val();
double& float_val();
bool get_boolean() const {
return bool_val();
}
bool& get_boolean() {
return bool_val();
}
bool bool_val() const;
bool& bool_val();
_Value& json_pointerB(const std_vector<_Value>& routeDataVec);
const _Value& json_pointerB(const std_vector<_Value>& routeVec) const;
Array* as_array();
Object* as_object();
PartialJson* as_partial_json();
StructuredPtr as_structured_ptr();
const Array* as_array()const;
const Object* as_object()const;
const PartialJson* as_partial_json()const;
const StructuredPtr as_structured_ptr()const;
uint64_t find(const _Value& key) const; // find without key`s converting?
// _Value (type is String or Short_String) -> no need utf8, unicode check.
_Value& operator[](const _Value& key); // if not exist key, then nothing.
const _Value& operator[](const _Value& key) const; // if not exist key, then nothing.
_Value& operator[](uint64_t idx);
const _Value& operator[](uint64_t idx) const;
public:
void clear(bool remove_str);
String& get_string() {
return str_val();
}
String& str_val();
const String& get_string() const {
return str_val();
}
const String& str_val() const;
void set_int(long long x);
void set_uint(unsigned long long x);
void set_float(double x);
bool set_str(Arena* pool, const char* str, uint64_t len);
bool set_str(String str);
private:
void set_str_in_parse(Arena* pool, const char* str, uint64_t len);
public:
void set_bool(bool x);
void set_null();
void set_none();
// chk!! with clauscript++?
std::string convert_primitive_to_std_string() {
if (is_int()) {
return std::to_string(get_integer());
}
else if (is_uint()) {
return std::to_string(get_unsigned_integer());
}
else if (is_float()) {
return std::to_string(get_floating());
}
else if (is_bool()) {
return std::to_string(get_boolean());
}
else if (is_null()) {
return "null";
}
else if (is_str()) {
bool fail = false;
return get_string().get_std_string(fail);
}
else {
return "";
}
}
private:
void set_type(_ValueType type);
public:
~_Value();
_Value(const _Value& other) = delete;
_Value(_Value&& other) noexcept;
_Value();
bool operator==(const _Value& other) const;
bool operator!=(const _Value& other) const;
bool operator<(const _Value& other) const;
_Value& operator=(const _Value& other) = delete;
_Value& operator=(_Value&& other) noexcept;
public:
StructuredPtr as_structured();
bool is_virtual() const;
};
class Value {
private:
_Value x;
public:
Value() noexcept { }
Value(_Value&& x) noexcept : x(std::move(x)) {
//
}
Value(Value&& x) noexcept : x(std::move(x.x)) {
//
}
~Value() noexcept;
public:
Value& operator=(const Value&) = delete;
Value(const Value& other) = delete;
public:
_Value& Get() noexcept { return x; }
const _Value& Get() const noexcept { return x; }
};
class _ValueView {
private:
claujson::_Value* p = nullptr;
public:
_ValueView() {}
_ValueView(const claujson::_Value& x) {
p = const_cast<claujson::_Value*>(&x);
}
claujson::_Value* operator->() {
return p;
}
const claujson::_Value* operator->() const {
return p;
}
claujson::_Value& operator*() {
return *p;
}
const claujson::_Value& operator*() const {
return *p;
}
bool operator<(_ValueView other) const {
return (*p) < (*other.p);
}
};
class parser;
class Document {
public:
friend class parser;
private:
_Value x;
Arena* pool; // getter? public?
public:
Document() noexcept { pool = new (std::nothrow) Arena(); }
Document(_Value&& x) noexcept : x(std::move(x)) {
pool = new (std::nothrow) Arena();
}
~Document() noexcept;
public:
Document& operator=(const Document&) = delete;
Document(const _Value&) = delete;
public:
_Value& Get() noexcept { return x; }
const _Value& Get() const noexcept { return x; }
Arena* GetAllocator() noexcept {
return pool;
}
const Arena* GetAllocator() const noexcept {
return pool;
}
};
}
namespace claujson {
class StructuredPtr {
public:
friend class LoadData2;
friend class PartialJson;
friend class _Value;
friend class Array;
friend class Object;
static const uint64_t npos;
static _Value empty_value;
private:
union {
Array* arr = nullptr;
Object* obj;
PartialJson* pj;
};
uint32_t type = 0;
public:
StructuredPtr(_Value& x);
StructuredPtr(const _Value& x);
StructuredPtr(const StructuredPtr& other) {
arr = other.arr;
type = other.type;
}
StructuredPtr() {
arr = nullptr;
type = 0;
}
StructuredPtr(Array* arr, Object* obj, PartialJson* pj)
{
if (arr) {
this->arr = arr;
type = 1;
}
else if (obj) {
this->obj = obj;
type = 2;
}
else if (pj) {
this->pj = pj;
type = 3;
}
}
StructuredPtr(std::nullptr_t) : arr(nullptr), type(0) {
//
}
StructuredPtr(Array* arr) : arr(arr), type(1)
{
}
StructuredPtr(Object* obj) : obj(obj), type(2)
{
}
StructuredPtr(PartialJson* pj) : pj(pj), type(3)
{
//
}
StructuredPtr(const Array* arr) : arr(const_cast<Array*>(arr)), type(1)
{
//
}
StructuredPtr(const Object* obj) : obj(const_cast<Object*>(obj)), type(2)
{
//
}
StructuredPtr(const PartialJson* pj) : pj(const_cast<PartialJson*>(pj)), type(3)
{
//
}
uint64_t get_data_size() const;
uint64_t size() const;
bool empty() const;
_Value& get_value_list(uint64_t idx);
_Value& get_key_list(uint64_t idx);
const _Value& get_value_list(uint64_t idx)const;
const _Value& get_key_list(uint64_t idx)const;
bool insert(uint64_t idx, Value val); // from Array
const _Value& get_const_key_list(uint64_t idx);
const _Value& get_const_key_list(uint64_t idx) const;
bool change_key(const _Value& key, Value&& next_key);
bool change_key(uint64_t idx, Value&& next_key);
bool has_pool() const;
explicit operator bool() const {
return arr;
}
bool operator==(const StructuredPtr& other) const {
return arr == other.arr && type == other.type;
}
bool is_array() const {
return type == 1;
}
bool is_object() const {
return type == 2;
}
bool is_partial_json() const {
return type == 3;
}
bool is_nullptr() const {
return type == 0;
}
bool is_user_type() const {
return is_array() || is_object();
}
bool chk_key_dup(uint64_t* idx) const;
uint64_t find_by_key(const _Value& key) const; // find without key`s converting ( \uxxxx )
_Value& operator[](const _Value& key); // if not exist key, then _Value <- is not valid.
const _Value& operator[](const _Value& key) const; // if not exist key, then _Value <- is not valid.
bool add_array_element(Value v);
bool add_object_element(Value key, Value v);
uint64_t find_by_value(const _Value& value, uint64_t start = 0) const; // find without key`s converting ( \uxxxx )
_Value& operator[](uint64_t idx);
const _Value& operator[](uint64_t idx) const;
// pj`s parent is nullptr.
StructuredPtr get_parent();
void erase(uint64_t idx, bool real = false);
void erase(const _Value& key, bool real = false);
bool operator==(std::nullptr_t) {
return !arr;
}
bool operator==(StructuredPtr p) {
return arr == p.arr && type == p.type;
}
bool operator!=(std::nullptr_t) {
return arr;
}
void operator=(std::nullptr_t) {
arr = nullptr;
type = 0;
}
void operator=(const StructuredPtr& other) {
arr = other.arr;
type = other.type;
}
void Delete();
void clear();
void clear(uint64_t idx); // clear child[idx] ?
bool assign_value(uint64_t idx, Value val);
void MergeWith(StructuredPtr j, int start_offset);
void reserve_data_list(uint64_t sz);
private:
// need rename param....!
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, Arena* pool);
void add_item_type(int64_t val_buf_idx, int64_t val_next_buf_idx,
char* buf, uint64_t val_token_idx, Arena* pool);
void add_user_type(int64_t key_buf_idx, int64_t key_next_buf_idx, char* buf,
_ValueType type, uint64_t key_token_idx, Arena* pool
);
//
void add_user_type(_ValueType type, Arena* pool
); // int type -> enum?
public:
bool is_virtual() const;
// private: + friend?
private:
void set_parent(StructuredPtr p);
};
class LoadData;
class LoadData2;
class Array;
class Object;
class PartialJson; // rename?
}
#include "claujson_array.h"
#include "claujson_object.h"
#include "claujson_partialjson.h"
namespace claujson {
class parser {
private:
_simdjson::dom::parser_for_claujson test_;
std::unique_ptr<ThreadPool> pool;
public:
parser(int thr_num = 0);
public:
// parse json file.
std::pair<bool, uint64_t> parse(const std::string& fileName, Document& d, uint64_t thr_num);
//std::pair<bool, uint64_t> parse2(const std::string& fileName, Document2*& j, uint64_t thr_num);
// parse json str.
std::pair<bool, uint64_t> parse_str(StringView str, Document& d, uint64_t thr_num);
#if __cpp_lib_char8_t
// C++20~
std::pair<bool, uint64_t> parse_str(std::u8string_view str, Document& d, uint64_t thr_num);
#endif
};
class writer {
private:
std::unique_ptr<ThreadPool> pool;
public:
writer(int thr_num = 0);
public:
std::string write_to_str(const _Value& global, bool prettty = false);
std::string write_to_str2(const _Value& global, bool prettty = false);
void write(const std::string& fileName, const _Value& global, bool pretty = false);
void write_parallel(Arena* pool, const std::string& fileName, _Value& j, uint64_t thr_num, bool pretty = false);
void write_parallel2(const std::string& fileName, const _Value& j, uint64_t thr_num, bool pretty = false);
};
[[nodiscard]]
_Value diff(Arena* pool, const _Value& x, const _Value& y);
_Value& patch(Arena* pool, _Value& x, const _Value& diff);
void clean(_Value& x); //
std::pair<bool, std::string> convert_to_string_in_json(StringView x);
//bool convert_number(StringView x, claujson::_Value& data);
//bool convert_string(StringView x, claujson::_Value& data);
bool is_valid_string_in_json(StringView x);
#if __cpp_lib_char8_t
std::pair<bool, std::string> convert_to_string_in_json(std::u8string_view x);
bool is_valid_string_in_json(std::u8string_view x);
#endif
}
#define claujson_inline _simdjson_inline
#define ERROR(msg) \
do { \
throw msg; \
/* error.make(__LINE__, StringView(msg)); */ \
} while (false)
namespace claujson {
claujson::_Value& Convert(Arena* pool, claujson::_Value& data, uint64_t buf_idx, uint64_t next_buf_idx, bool key,
char* buf, uint64_t token_idx, bool& err);
}