-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbasic_json.hpp
2280 lines (1917 loc) · 60.9 KB
/
basic_json.hpp
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
#ifndef BIZWEN_BASIC_JSON_HPP
#define BIZWEN_BASIC_JSON_HPP
#include <cassert>
#include <cstddef>
#include <iterator>
#include <map>
#include <memory>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include <concepts>
#include <array>
#include <ranges>
// https://www.rfc-editor.org/rfc/rfc8259
namespace bizwen
{
struct nulljson_t
{
explicit constexpr nulljson_t() noexcept = default;
};
inline constexpr nulljson_t nulljson{};
// https://cplusplus.github.io/LWG/issue3917
// since the types of string, array and map are unknown at this point,
// memory allocation can only be done by instantiating with std::byte.
template <typename Number = double,
typename Integer = long long, typename UInteger = unsigned long long, typename Allocator = std::allocator<std::byte>>
class basic_json_node;
template <typename Node = basic_json_node<>, typename String = std::string,
typename Array = std::vector<Node>,
typename Object = std::map<String, Node>,
bool HasInteger = true, bool HasUInteger = true>
class basic_json;
namespace detail
{
template <typename Node, typename String,
typename Array,
typename Object,
bool HasInteger, bool HasUInteger>
struct basic_json_slice_common_base;
template <typename Alloc>
using ator_void_ptr_t = std::allocator_traits<Alloc>::void_pointer;
template <typename T, typename Alloc>
using ator_rebound_ptr_t = std::allocator_traits<Alloc>::template rebind_traits<T>::pointer;
template <typename T, typename Node>
using node_ator_rebound_ptr_t = std::allocator_traits<typename Node::allocator_type>::template rebind_traits<T>::pointer;
template <typename T, typename Node>
constexpr node_ator_rebound_ptr_t<T, Node> alloc_from_node(Node& node);
template <typename T, typename Node>
constexpr void dealloc_from_node(Node& node, node_ator_rebound_ptr_t<T, Node> p) noexcept;
template <typename T, typename Node>
struct node_variant_alloc_guard;
} // namespace detail
template <typename Node = basic_json_node<>, typename String = std::string,
typename Array = std::vector<Node>,
typename Object = std::map<String, Node>,
bool HasInteger = true, bool HasUInteger = true>
class basic_const_json_slice;
template <typename Node = basic_json_node<>, typename String = std::string,
typename Array = std::vector<Node>,
typename Object = std::map<String, Node>,
bool HasInteger = true, bool HasUInteger = true>
class basic_json_slice;
enum class json_errc
{
// for accessor
is_undefined = 1,
not_null,
not_boolean,
not_number,
not_integer,
not_uinteger,
not_string,
not_array,
not_object,
nonarray_indexing,
nonobject_indexing,
key_not_found,
// for modifier
is_empty,
not_undefined_or_null,
not_undefined_or_boolean,
not_undefined_or_number,
not_undefined_or_integer,
not_undefined_or_uinteger,
not_undefined_or_string,
not_undefined_or_array,
not_undefined_or_object,
// for deserializer
syntax_error,
number_overflow,
// for serializer
number_nan,
number_inf
};
class json_error: public std::runtime_error
{
public:
json_error(json_errc ec)
: std::runtime_error("")
, code_(ec)
{
}
json_errc code() const noexcept { return code_; }
char const* what() const noexcept override
{
switch (code_)
{
case json_errc::not_null:
return "JSON error: value isn't a null.";
case json_errc::not_boolean:
return "JSON error: value not a boolean.";
case json_errc::not_integer:
return "JSON error: value isn't an integer.";
case json_errc::not_uinteger:
return "JSON error: value isn't an unsigned integer.";
case json_errc::not_number:
return "JSON error: value isn't a number.";
case json_errc::not_string:
return "JSON error: value isn't a string.";
case json_errc::not_array:
return "JSON error: value isn't an array.";
case json_errc::not_object:
return "JSON error: value isn't an object.";
case json_errc::nonarray_indexing:
return "JSON error: value isn't an array but is accessed using operator[].";
case json_errc::nonobject_indexing:
return "JSON error: value isn't an object but is accessed using operator[].";
case json_errc::key_not_found:
return "JSON error: key does not exist.";
case json_errc::not_undefined_or_null:
return "JSON error: current value is not undefined or not null.";
case json_errc::not_undefined_or_boolean:
return "JSON error: current value is not undefined or not bool.";
case json_errc::not_undefined_or_number:
return "JSON error: current value is not undefined or not a number.";
case json_errc::not_undefined_or_string:
return "JSON error: current value is not undefined or not a string.";
default:
return "JSON error: unspecified error.";
}
}
private:
json_errc code_;
};
namespace detail
{
template <typename VoidPtr, typename FPoint, typename SInt, typename UInt>
class node_storage_variant
{
private:
static_assert(std::floating_point<FPoint>);
static_assert(std::signed_integral<SInt>);
static_assert(std::unsigned_integral<UInt>);
template <typename Number, typename Integer, typename UInteger, typename Allocator>
friend class bizwen::basic_json_node;
template <typename Node, typename String,
typename Array,
typename Object,
bool HasInteger, bool HasUInteger>
friend class bizwen::basic_json;
template <typename Node, typename String,
typename Array,
typename Object,
bool HasInteger, bool HasUInteger>
friend struct detail::basic_json_slice_common_base;
template <typename T, typename Node>
friend constexpr detail::node_ator_rebound_ptr_t<T, Node> detail::alloc_from_node(Node& node);
template <typename T, typename Node>
friend constexpr void detail::dealloc_from_node(Node& node, detail::node_ator_rebound_ptr_t<T, Node> p) noexcept;
template <typename T, typename Node>
friend struct detail::node_variant_alloc_guard;
template <typename Node, typename String,
typename Array,
typename Object,
bool HasInteger, bool HasUInteger>
friend class bizwen::basic_const_json_slice;
template <typename Node, typename String,
typename Array,
typename Object,
bool HasInteger, bool HasUInteger>
friend class bizwen::basic_json_slice;
enum class kind_t : unsigned char
{
undefined,
null,
true_value,
false_value,
number,
integer,
uinteger,
string,
array,
object
};
union
{
VoidPtr str_;
VoidPtr arr_;
VoidPtr obj_;
FPoint num_;
SInt int_;
UInt uint_;
};
kind_t kind_{};
constexpr void copy_replace_union(node_storage_variant const& other) noexcept
{
// pre: kind_ == other.kind_, no non-trivial variant member of the union constructed
switch (kind_)
{
case kind_t::string:
std::construct_at(std::addressof(str_), other.str_);
break;
case kind_t::array:
std::construct_at(std::addressof(arr_), other.arr_);
break;
case kind_t::object:
std::construct_at(std::addressof(obj_), other.obj_);
break;
case kind_t::number:
std::construct_at(&num_, other.num_);
break;
case kind_t::integer:
std::construct_at(&int_, other.int_);
break;
case kind_t::uinteger:
std::construct_at(&uint_, other.uint_);
break;
}
}
constexpr void move_replace_union(node_storage_variant const& other) noexcept
{
// pre: kind_ == other.kind_, no non-trivial variant member of the union constructed
switch (kind_)
{
case kind_t::string:
std::construct_at(std::addressof(str_), std::move(other.str_));
break;
case kind_t::array:
std::construct_at(std::addressof(arr_), std::move(other.arr_));
break;
case kind_t::object:
std::construct_at(std::addressof(obj_), std::move(other.obj_));
break;
case kind_t::number:
std::construct_at(&num_, other.num_);
break;
case kind_t::integer:
std::construct_at(&int_, other.int_);
break;
case kind_t::uinteger:
std::construct_at(&uint_, other.uint_);
break;
}
}
constexpr void destroy_union() noexcept
{
// pre: kind_ indicates the status of the union
switch (kind_)
{
case kind_t::string:
str_.~VoidPtr();
break;
case kind_t::array:
arr_.~VoidPtr();
break;
case kind_t::object:
obj_.~VoidPtr();
break;
default:
;
}
}
constexpr void set_undefined() noexcept
{
destroy_union();
kind_ = kind_t::undefined;
}
constexpr void set_null() noexcept
{
destroy_union();
kind_ = kind_t::null;
}
constexpr void set_bool(bool b) noexcept
{
destroy_union();
kind_ = b ? kind_t::true_value : kind_t::false_value;
}
constexpr void set_num(FPoint x) noexcept
{
if (kind_ == kind_t::number)
num_ = x;
else
{
destroy_union();
kind_ = kind_t::number;
std::construct_at(&num_, x);
}
}
constexpr void set_int(SInt n) noexcept
{
if (kind_ == kind_t::integer)
int_ = n;
else
{
destroy_union();
kind_ = kind_t::integer;
std::construct_at(&int_, n);
}
}
constexpr void set_uint(UInt n) noexcept
{
if (kind_ == kind_t::uinteger)
num_ = n;
else
{
destroy_union();
kind_ = kind_t::uinteger;
std::construct_at(&uint_, n);
}
}
constexpr void set_str_ptr(VoidPtr vp) noexcept
{
if (kind_ == kind_t::string)
str_ = vp;
else
{
destroy_union();
kind_ = kind_t::string;
std::construct_at(std::addressof(str_), std::move(vp));
}
}
constexpr void set_arr_ptr(VoidPtr vp) noexcept
{
if (kind_ == kind_t::array)
arr_ = vp;
else
{
destroy_union();
kind_ = kind_t::array;
std::construct_at(std::addressof(arr_), std::move(vp));
}
}
constexpr void set_obj_ptr(VoidPtr vp) noexcept
{
if (kind_ == kind_t::object)
obj_ = vp;
else
{
destroy_union();
kind_ = kind_t::object;
std::construct_at(std::addressof(obj_), std::move(vp));
}
}
public:
constexpr node_storage_variant() noexcept {}
// clang-format off
node_storage_variant(node_storage_variant const&) noexcept
requires std::is_trivially_copy_constructible_v<VoidPtr> = default;
// clang-format on
constexpr node_storage_variant(node_storage_variant const& other) noexcept
: kind_{ other.kind_ }
{
copy_replace_union(other);
}
// clang-format off
node_storage_variant(node_storage_variant&&) noexcept
requires std::is_trivially_move_constructible_v<VoidPtr> = default;
// clang-format on
constexpr node_storage_variant(node_storage_variant&& other) noexcept
: kind_{ other.kind_ }
{
move_replace_union(std::move(other));
}
// clang-format off
node_storage_variant& operator=(node_storage_variant const&) noexcept
requires std::is_trivially_copy_constructible_v<VoidPtr> &&
std::is_trivially_copy_assignable_v<VoidPtr> &&
std::is_trivially_destructible_v<VoidPtr> = default;
// clang-format on
constexpr node_storage_variant& operator=(node_storage_variant const& other) noexcept
{
if (kind_ == other.kind_)
{
switch (kind_)
{
case kind_t::string:
str_ = other.str_;
break;
case kind_t::array:
arr_ = other.arr_;
break;
case kind_t::object:
obj_ = other.obj_;
break;
case kind_t::number:
num_ = other.num_;
break;
case kind_t::integer:
int_ = other.int_;
break;
case kind_t::uinteger:
uint_ = other.uint_;
break;
}
}
else
{
destroy_union();
kind_ = other.kind_;
copy_replace_union(other);
}
return *this;
}
// clang-format off
node_storage_variant& operator=(node_storage_variant&&) noexcept
requires std::is_trivially_move_constructible_v<VoidPtr> &&
std::is_trivially_move_assignable_v<VoidPtr> &&
std::is_trivially_destructible_v<VoidPtr> = default;
// clang-format on
constexpr node_storage_variant& operator=(node_storage_variant&& other) noexcept
{
if (kind_ == other.kind_)
{
switch (kind_)
{
case kind_t::string:
str_ = std::move(other.str_);
break;
case kind_t::array:
arr_ = std::move(other.arr_);
break;
case kind_t::object:
obj_ = std::move(other.obj_);
break;
case kind_t::number:
num_ = other.num_;
break;
case kind_t::integer:
int_ = other.int_;
break;
case kind_t::uinteger:
uint_ = other.uint_;
break;
}
}
else
{
destroy_union();
kind_ = other.kind_;
move_replace_union(other);
}
return *this;
}
// clang-format off
~node_storage_variant() noexcept requires std::is_trivially_destructible_v<VoidPtr> = default;
// clang-format on
constexpr ~node_storage_variant() noexcept
{
destroy_union();
}
};
#if __has_cpp_attribute(no_unique_address)
#define BIZWEN_NO_UNIQUE_ADDRESS [[no_unique_address]]
#elif __has_cpp_attribute(msvc::no_unique_address)
#define BIZWEN_NO_UNIQUE_ADDRESS [[msvc::no_unique_address]]
#endif
#ifdef BIZWEN_NO_UNIQUE_ADDRESS
template <typename Number,
typename Integer, typename UInteger, typename Allocator>
struct basic_json_node_storage
{
BIZWEN_NO_UNIQUE_ADDRESS Allocator alloc_;
node_storage_variant<detail::ator_void_ptr_t<Allocator>, Number, Integer, UInteger> var_;
constexpr Allocator& get_allocator_ref() noexcept
{
return alloc_;
}
constexpr Allocator const& get_allocator_ref() const noexcept
{
return alloc_;
}
};
#else
template <typename Number,
typename Integer, typename UInteger, typename Allocator>
struct basic_json_node_storage
{
Allocator alloc_;
node_storage_variant<detail::ator_void_ptr_t<Allocator>, Number, Integer, UInteger> var_;
constexpr Allocator& get_allocator_ref() noexcept
{
return alloc_;
}
constexpr Allocator const& get_allocator_ref() const noexcept
{
return alloc_;
}
};
template <typename Number,
typename Integer, typename UInteger, typename Allocator>
requires std::is_class_v<Allocator> && (!std::is_final_v<Allocator>)
struct basic_json_node_storage<Number, Integer, UInteger, Allocator>: Allocator
{
node_storage_variant<detail::ator_void_ptr_t<Allocator>, Number, Integer, UInteger> var_;
constexpr Allocator& get_allocator_ref() noexcept
{
return (Allocator&)(*this);
}
constexpr Allocator const& get_allocator_ref() const noexcept
{
return (Allocator const&)(*this);
}
};
#endif
#undef BIZWEN_NO_UNIQUE_ADDRESS
} // namespace detail
template <typename Number,
typename Integer, typename UInteger, typename Allocator>
class basic_json_node
{
public:
using number_type = Number;
using integer_type = Integer;
using uinteger_type = UInteger;
using allocator_type = Allocator;
private:
template <typename Node, typename String,
typename Array,
typename Object,
bool HasInteger, bool HasUInteger>
friend class basic_json;
template <typename Node, typename String,
typename Array,
typename Object,
bool HasInteger, bool HasUInteger>
friend struct detail::basic_json_slice_common_base;
template <typename T, typename Node>
friend constexpr detail::node_ator_rebound_ptr_t<T, Node> detail::alloc_from_node(Node& node);
template <typename T, typename Node>
friend constexpr void detail::dealloc_from_node(Node& node, detail::node_ator_rebound_ptr_t<T, Node> p) noexcept;
template <typename T, typename Node>
friend struct detail::node_variant_alloc_guard;
template <typename Node, typename String,
typename Array,
typename Object,
bool HasInteger, bool HasUInteger>
friend class basic_const_json_slice;
template <typename Node, typename String,
typename Array,
typename Object,
bool HasInteger, bool HasUInteger>
friend class basic_json_slice;
using stor_var_t = detail::node_storage_variant<detail::ator_void_ptr_t<Allocator>, Number, Integer, UInteger>;
using kind_t = stor_var_t::kind_t;
detail::basic_json_node_storage<Number, Integer, UInteger, Allocator> stor_;
constexpr Allocator& get_allocator_ref() noexcept
{
return stor_.get_allocator_ref();
}
constexpr const Allocator& get_allocator_ref() const noexcept
{
return stor_.get_allocator_ref();
}
constexpr kind_t kind() const noexcept
{
return stor_.var_.kind_;
}
constexpr stor_var_t& stor_var() noexcept
{
return stor_.var_;
}
constexpr const stor_var_t& stor_var() const noexcept
{
return stor_.var_;
}
public:
// clang-format off
constexpr basic_json_node() requires std::default_initializable<Allocator> = default;
// clang-format on
constexpr explicit basic_json_node(Allocator const& a) noexcept
: stor_{ { a } }
{
}
constexpr basic_json_node(basic_json_node const&) = default;
constexpr basic_json_node(basic_json_node&&) noexcept = default;
constexpr basic_json_node& operator=(basic_json_node const&) = default;
constexpr basic_json_node& operator=(basic_json_node&&) noexcept = default;
};
namespace detail
{
template <typename Object>
concept transparently_comparable_associative = requires {
typename Object::key_compare;
typename Object::key_compare::is_transparent;
};
template <typename Object>
concept transparently_comparable_unordered_associative = requires {
typename Object::key_equal;
typename Object::key_equal::is_transparent;
typename Object::hasher;
typename Object::hasher::is_transparent;
};
template <typename Object>
concept transparently_comparable_json_object = transparently_comparable_associative<Object>
|| transparently_comparable_unordered_associative<Object>;
template <typename KeyStrLike, typename Object>
concept noncovertible_to_key_char_cptr = !std::is_convertible_v<KeyStrLike const&, typename Object::key_type::value_type const*>;
template <typename Node, typename String,
typename Array,
typename Object,
bool HasInteger, bool HasUInteger>
struct basic_json_slice_common_base
{
static inline constexpr bool has_integer = HasInteger;
static inline constexpr bool has_uinteger = HasUInteger;
using node_type = Node;
using object_type = Object;
using value_type = Node;
using array_type = Array;
using string_type = String;
using const_slice_type = basic_const_json_slice<node_type, string_type, array_type, object_type, has_integer, has_uinteger>;
using slice_type = basic_json_slice<node_type, string_type, array_type, object_type, has_integer, has_uinteger>;
using json_type = basic_json<node_type, string_type, array_type, object_type, has_integer, has_uinteger>;
using number_type = node_type::number_type;
using integer_type = node_type::integer_type;
using uinteger_type = node_type::uinteger_type;
using char_type = string_type::value_type;
using map_node_type = object_type::value_type;
using allocator_type = node_type::allocator_type;
using key_string_type = object_type::key_type;
using key_char_type = key_string_type::value_type;
node_type* node_{}; // made private in derived classes
private:
using traits_t = std::allocator_traits<allocator_type>;
using kind_t = node_type::kind_t;
using stor_var_t = node_type::stor_var_t;
using str_ptr_t = node_ator_rebound_ptr_t<string_type, node_type>;
using arr_ptr_t = node_ator_rebound_ptr_t<array_type, node_type>;
using obj_ptr_t = node_ator_rebound_ptr_t<object_type, node_type>;
friend json_type;
constexpr kind_t kind() const
{
// assert(node_);
// Lakos rule
if (!node_)
throw json_error(json_errc::is_empty);
return node_->kind();
}
constexpr stor_var_t const& stor_var() const noexcept
{
return node_->stor_var();
}
public:
[[nodiscard]] constexpr bool empty() const noexcept
{
return node_ != nullptr;
}
[[nodiscard]] constexpr bool undefined() const noexcept
{
return kind() == kind_t::undefined;
}
[[nodiscard]] constexpr bool string() const noexcept
{
return kind() == kind_t::string;
}
[[nodiscard]] constexpr bool null() const noexcept
{
return kind() == kind_t::null;
}
[[nodiscard]] constexpr bool boolean() const noexcept
{
auto k = kind();
return k == kind_t::true_value || k == kind_t::false_value;
}
[[nodiscard]] constexpr bool number() const noexcept
{
auto k = kind();
bool is_integer{};
bool is_uinteger{};
if constexpr (HasInteger)
{
is_integer = k == kind_t::integer;
}
if constexpr (HasUInteger)
{
is_uinteger = k == kind_t::uinteger;
}
return k == kind_t::number || is_integer || is_uinteger;
}
[[nodiscard]] constexpr bool object() const noexcept
{
return kind() == kind_t::object;
}
[[nodiscard]] constexpr bool array() const noexcept
{
return kind() == kind_t::array;
}
[[nodiscard]] constexpr bool integer() const noexcept
requires HasInteger
{
return kind() == kind_t::integer;
}
[[nodiscard]] constexpr bool uinteger() const noexcept
requires HasUInteger
{
return kind() == kind_t::uinteger;
}
constexpr explicit operator bool() const
{
if (!boolean())
throw json_error(json_errc::not_boolean);
auto k = kind();
return k == kind_t::true_value;
}
constexpr explicit operator number_type() const
{
auto k = kind();
auto s = stor_var();
if (k == kind_t::number)
return s.num_;
else if (k == kind_t::integer)
return s.int_;
else if (k == kind_t::uinteger)
return s.uint_;
throw json_error(json_errc::not_number);
}
constexpr explicit operator nulljson_t() const
{
if (!null())
throw json_error(json_errc::not_null);
return nulljson;
}
constexpr explicit operator string_type const&() const&
{
if (!string())
throw json_error(json_errc::not_string);
return *static_cast<str_ptr_t>(stor_var().str_);
}
constexpr explicit operator array_type const&() const&
{
if (!array())
throw json_error(json_errc::not_array);
return *static_cast<arr_ptr_t>(stor_var().arr_);
}
constexpr explicit operator object_type const&() const&
{
if (!object())
throw json_error(json_errc::not_object);
return *static_cast<obj_ptr_t>(stor_var().obj_);
}
constexpr explicit operator integer_type() const
requires HasInteger
{
if (!integer())
throw json_error(json_errc::not_integer);
return stor_var().int_;
}
constexpr explicit operator uinteger_type() const
requires HasUInteger
{
if (!uinteger())
throw json_error(json_errc::not_uinteger);
return stor_var().uint_;
}
// defined after the class body of basic_const_json_slice
constexpr auto operator[](key_string_type const& k) const -> const_slice_type;
template <typename KeyStrLike>
requires transparently_comparable_json_object<Object>
&& noncovertible_to_key_char_cptr<KeyStrLike, Object>
constexpr auto operator[](KeyStrLike const& k) const -> const_slice_type;
constexpr auto operator[](key_char_type const* k) const -> const_slice_type;
template <std::integral T>
constexpr auto operator[](T pos) const -> const_slice_type;
constexpr auto as_array() const
{
constexpr auto node_to_slice = [](node_type const& node) static noexcept {
return const_slice_type{ node };
};
return static_cast<array_type const&>(*this) | std::views::transform(node_to_slice);
}
constexpr auto as_object() const
{
constexpr auto pair_node_to_slice = [](map_node_type const& pair) static noexcept {
auto& [key, value]{ pair };
return std::pair<key_string_type const&, const_slice_type>{ key, value };
};
return static_cast<object_type const&>(*this) | std::views::transform(pair_node_to_slice);
}
};
}
template <typename Node, typename String,
typename Array,
typename Object,
bool HasInteger, bool HasUInteger>
class basic_const_json_slice: public detail::basic_json_slice_common_base<Node, String, Array, Object, HasInteger, HasUInteger>
{
private:
using base_type = detail::basic_json_slice_common_base<Node, String, Array, Object, HasInteger, HasUInteger>;
using base_type::node_;
public:
using typename base_type::json_type;
using typename base_type::node_type;
using typename base_type::slice_type;
constexpr void swap(basic_const_json_slice& rhs) noexcept
{
auto temp = node_;
node_ = rhs.node_;
rhs.node_ = temp;
}
friend constexpr void swap(basic_const_json_slice& lhs, basic_const_json_slice& rhs) noexcept
{
lhs.swap(rhs);
}
// Similar to iterators, default construction is allowed, but except for operator=,
// operations on default-constructed slice cause undefined behavior.
// The default constructor is intentionally provided for default arguments.
constexpr basic_const_json_slice() noexcept = default;
constexpr basic_const_json_slice(basic_const_json_slice&&) noexcept = default;
constexpr basic_const_json_slice(basic_const_json_slice const&) noexcept = default;
constexpr basic_const_json_slice(json_type const& j) noexcept
: base_type{ const_cast<node_type*>(std::addressof(j.node_)) }
{
}
constexpr basic_const_json_slice(node_type const& n) noexcept
: base_type{ const_cast<node_type*>(std::addressof(n)) }
{
}
constexpr basic_const_json_slice(slice_type const& s) noexcept
: base_type{ static_cast<base_type const&>(s) }
{
}
constexpr basic_const_json_slice& operator=(basic_const_json_slice const&) noexcept = default;
constexpr basic_const_json_slice& operator=(basic_const_json_slice&&) noexcept = default;
};
namespace detail
{
template <typename Node, typename String,
typename Array,
typename Object,
bool HasInteger, bool HasUInteger>
constexpr auto basic_json_slice_common_base<Node, String, Array, Object, HasInteger, HasUInteger>::operator[](key_string_type const& k) const -> const_slice_type
{
if (!object())
throw json_error(json_errc::nonobject_indexing);
auto const& o = *static_cast<obj_ptr_t>(stor_var().obj_);
auto i = o.find(k);
if (i == o.end())
throw json_error(json_errc::key_not_found);
auto& [_, v] = *i;