-
Notifications
You must be signed in to change notification settings - Fork 6
/
test.cpp
3332 lines (3066 loc) · 180 KB
/
test.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 <gtest/gtest.h>
#include <list>
#include <map>
#include <numeric>
#include "cpp_yyjson.hpp"
#if defined(__GNUC__)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#pragma GCC diagnostic ignored "-Wunused-variable"
#pragma GCC diagnostic ignored "-Wunused-but-set-variable"
#endif
// NOLINTBEGIN
TEST(Writer, Constructor)
{
using namespace yyjson::writer; // NOLINT
// default constructor
static_assert(std::default_initializable<value>);
static_assert(!std::default_initializable<value_ref>);
static_assert(!std::default_initializable<const_value_ref>);
static_assert(std::default_initializable<array>);
static_assert(!std::default_initializable<array_ref>);
static_assert(!std::default_initializable<const_array_ref>);
static_assert(std::default_initializable<object>);
static_assert(!std::default_initializable<object_ref>);
static_assert(!std::default_initializable<const_object_ref>);
// copy constructor - value
static_assert(std::constructible_from<value, value&> && !value::constructor_copy_value<value&>);
static_assert(std::constructible_from<value, value_ref&> && !value::constructor_copy_value<value_ref&>);
static_assert(std::constructible_from<value, const_value_ref&> && value::constructor_copy_value<const_value_ref&>);
static_assert(std::constructible_from<value, const value&> && value::constructor_copy_value<const value&>);
static_assert(std::constructible_from<value, const value_ref&> && value::constructor_copy_value<const value_ref&>);
static_assert(std::constructible_from<value, const value_ref&> && value::constructor_copy_value<const value_ref&>);
static_assert(std::constructible_from<value, const const_value_ref&> &&
value::constructor_copy_value<const const_value_ref&>);
static_assert(std::constructible_from<value, value&&> && !value::constructor_copy_value<value>);
static_assert(std::constructible_from<value, value_ref&&> && !value::constructor_copy_value<value_ref>);
static_assert(std::constructible_from<value, const_value_ref&&> && value::constructor_copy_value<const_value_ref>);
static_assert(std::constructible_from<value_ref, value&>);
static_assert(std::constructible_from<value_ref, value_ref&>);
static_assert(!std::constructible_from<value_ref, const_value_ref&>);
static_assert(!std::constructible_from<value_ref, const value&>);
static_assert(std::constructible_from<value_ref, const value_ref&>);
static_assert(!std::constructible_from<value_ref, const const_value_ref&>);
static_assert(!std::constructible_from<value_ref, value&&>);
static_assert(std::constructible_from<value_ref, value_ref&&>);
static_assert(!std::constructible_from<value_ref, const_value_ref&&>);
static_assert(std::constructible_from<const_value_ref, value&>);
static_assert(std::constructible_from<const_value_ref, value_ref&>);
static_assert(std::constructible_from<const_value_ref, const_value_ref&>);
static_assert(std::constructible_from<const_value_ref, const value&>);
static_assert(std::constructible_from<const_value_ref, const value_ref&>);
static_assert(std::constructible_from<const_value_ref, const const_value_ref&>);
static_assert(!std::constructible_from<const_value_ref, value&&>);
static_assert(std::constructible_from<const_value_ref, value_ref&&>);
static_assert(std::constructible_from<const_value_ref, const_value_ref&&>);
static_assert(std::constructible_from<value, array&> && !value::constructor_copy_value<array&>);
static_assert(std::constructible_from<value, array_ref&> && !value::constructor_copy_value<array_ref&>);
static_assert(std::constructible_from<value, const_array_ref&> && value::constructor_copy_value<const_array_ref&>);
static_assert(std::constructible_from<value, const array&> && value::constructor_copy_value<const array&>);
static_assert(std::constructible_from<value, const array_ref&> && value::constructor_copy_value<const array_ref&>);
static_assert(std::constructible_from<value, const const_array_ref&> &&
value::constructor_copy_value<const const_array_ref&>);
static_assert(std::constructible_from<value, array&&> && !value::constructor_copy_value<array&&>);
static_assert(std::constructible_from<value, array_ref&&> && !value::constructor_copy_value<array_ref&&>);
static_assert(std::constructible_from<value, const_array_ref&&> &&
value::constructor_copy_value<const_array_ref&&>);
static_assert(std::constructible_from<value, object&> && !value::constructor_copy_value<object&>);
static_assert(std::constructible_from<value, object_ref&> && !value::constructor_copy_value<object_ref&>);
static_assert(std::constructible_from<value, const_object_ref&> &&
value::constructor_copy_value<const_object_ref&>);
static_assert(std::constructible_from<value, const object&> && value::constructor_copy_value<const object&>);
static_assert(std::constructible_from<value, const object_ref&> &&
value::constructor_copy_value<const object_ref&>);
static_assert(std::constructible_from<value, const const_object_ref&> &&
value::constructor_copy_value<const const_object_ref&>);
static_assert(std::constructible_from<value, object&&> && !value::constructor_copy_value<object&&>);
static_assert(std::constructible_from<value, object_ref&&> && !value::constructor_copy_value<object_ref&&>);
static_assert(std::constructible_from<value, const_object_ref&&> &&
value::constructor_copy_value<const_object_ref&&>);
// copy constructor - array
static_assert(std::constructible_from<array, array&> && !array::constructor_copy_value<array&>);
static_assert(std::constructible_from<array, array_ref&> && !array::constructor_copy_value<array_ref&>);
static_assert(std::constructible_from<array, const_array_ref&> && array::constructor_copy_value<const_array_ref&>);
static_assert(std::constructible_from<array, const array&> && array::constructor_copy_value<const array&>);
static_assert(std::constructible_from<array, const array_ref&> && array::constructor_copy_value<const array_ref&>);
static_assert(std::constructible_from<array, const const_array_ref&> &&
array::constructor_copy_value<const const_array_ref&>);
static_assert(std::constructible_from<array, array&&> && !array::constructor_copy_value<array>);
static_assert(std::constructible_from<array, array_ref&&> && !array::constructor_copy_value<array_ref>);
static_assert(std::constructible_from<array, const_array_ref&&> && array::constructor_copy_value<const_array_ref>);
static_assert(std::constructible_from<array_ref, array&>);
static_assert(std::constructible_from<array_ref, array_ref&>);
static_assert(!std::constructible_from<array_ref, const_array_ref&>);
static_assert(!std::constructible_from<array_ref, const array&>);
static_assert(std::constructible_from<array_ref, const array_ref&>);
static_assert(!std::constructible_from<array_ref, const const_array_ref&>);
static_assert(!std::constructible_from<array_ref, array&&>);
static_assert(std::constructible_from<array_ref, array_ref&&>);
static_assert(!std::constructible_from<array_ref, const_array_ref&&>);
static_assert(std::constructible_from<const_array_ref, array&>);
static_assert(std::constructible_from<const_array_ref, array_ref&>);
static_assert(std::constructible_from<const_array_ref, const_array_ref&>);
static_assert(std::constructible_from<const_array_ref, const array&>);
static_assert(std::constructible_from<const_array_ref, const array_ref&>);
static_assert(std::constructible_from<const_array_ref, const const_array_ref&>);
static_assert(!std::constructible_from<const_array_ref, array&&>);
static_assert(std::constructible_from<const_array_ref, array_ref&&>);
static_assert(std::constructible_from<const_array_ref, const_array_ref&&>);
static_assert(std::constructible_from<array, value&> && !array::constructor_copy_value<value&>);
static_assert(std::constructible_from<array, value_ref&> && !array::constructor_copy_value<value_ref&>);
static_assert(std::constructible_from<array, const_value_ref&> && array::constructor_copy_value<const_value_ref&>);
static_assert(std::constructible_from<array, const value&> && array::constructor_copy_value<const value&>);
static_assert(std::constructible_from<array, const value_ref&> && array::constructor_copy_value<const value_ref&>);
static_assert(std::constructible_from<array, const const_value_ref&> &&
array::constructor_copy_value<const const_value_ref&>);
static_assert(std::constructible_from<array, value&&> && !array::constructor_copy_value<value>);
static_assert(std::constructible_from<array, value_ref&&> && !array::constructor_copy_value<value_ref>);
static_assert(std::constructible_from<array, const_value_ref&&> && array::constructor_copy_value<const_value_ref>);
static_assert(std::constructible_from<array_ref, value&>);
static_assert(std::constructible_from<array_ref, value_ref&> && !array_ref::constructor_copy_value<value_ref&>);
static_assert(!std::constructible_from<array_ref, const_value_ref&>);
static_assert(!std::constructible_from<array_ref, const value&>);
static_assert(std::constructible_from<array_ref, const value_ref&> &&
!array_ref::constructor_copy_value<const value_ref&>);
static_assert(!std::constructible_from<array_ref, const const_value_ref&>);
static_assert(!std::constructible_from<array_ref, value&&>);
static_assert(std::constructible_from<array_ref, value_ref&&> && !array_ref::constructor_copy_value<value_ref>);
static_assert(!std::constructible_from<array_ref, const_value_ref&&>);
static_assert(std::constructible_from<const_array_ref, value&> && !const_array_ref::constructor_copy_value<value&>);
static_assert(std::constructible_from<const_array_ref, value_ref&>);
static_assert(std::constructible_from<const_array_ref, const_value_ref&> &&
!const_array_ref::constructor_copy_value<const_value_ref&>);
static_assert(std::constructible_from<const_array_ref, const value&>);
static_assert(std::constructible_from<const_array_ref, const value_ref&>);
static_assert(std::constructible_from<const_array_ref, const const_value_ref&> &&
!const_array_ref::constructor_copy_value<const const_value_ref&>);
static_assert(!std::constructible_from<const_array_ref, value&&>);
static_assert(std::constructible_from<const_array_ref, value_ref&&>);
static_assert(std::constructible_from<const_array_ref, const_value_ref&&> &&
!const_array_ref::constructor_copy_value<const_value_ref>);
// copy constructor - object
static_assert(std::constructible_from<object, object&> && !object::constructor_copy_value<object&>);
static_assert(std::constructible_from<object, object_ref&> && !object::constructor_copy_value<object_ref&>);
static_assert(std::constructible_from<object, const_object_ref&> &&
object::constructor_copy_value<const_object_ref&>);
static_assert(std::constructible_from<object, const object&> && object::constructor_copy_value<const object&>);
static_assert(std::constructible_from<object, const object_ref&> &&
object::constructor_copy_value<const object_ref&>);
static_assert(std::constructible_from<object, const const_object_ref&> &&
object::constructor_copy_value<const const_object_ref&>);
static_assert(std::constructible_from<object, object&&> && !object::constructor_copy_value<object>);
static_assert(std::constructible_from<object, object_ref&&> && !object::constructor_copy_value<object_ref>);
static_assert(std::constructible_from<object, const_object_ref&&> &&
object::constructor_copy_value<const_object_ref>);
static_assert(std::constructible_from<object_ref, object&>);
static_assert(std::constructible_from<object_ref, object_ref&>);
static_assert(!std::constructible_from<object_ref, const_object_ref&>);
static_assert(!std::constructible_from<object_ref, const object&>);
static_assert(std::constructible_from<object_ref, const object_ref&>);
static_assert(!std::constructible_from<object_ref, const const_object_ref&>);
static_assert(!std::constructible_from<object_ref, object&&>);
static_assert(std::constructible_from<object_ref, object_ref&&>);
static_assert(!std::constructible_from<object_ref, const_object_ref&&>);
static_assert(std::constructible_from<const_object_ref, object&>);
static_assert(std::constructible_from<const_object_ref, object_ref&>);
static_assert(std::constructible_from<const_object_ref, const_object_ref&>);
static_assert(std::constructible_from<const_object_ref, const object&>);
static_assert(std::constructible_from<const_object_ref, const object_ref&>);
static_assert(std::constructible_from<const_object_ref, const const_object_ref&>);
static_assert(!std::constructible_from<const_object_ref, object&&>);
static_assert(std::constructible_from<const_object_ref, object_ref&&>);
static_assert(std::constructible_from<const_object_ref, const_object_ref&&>);
static_assert(std::constructible_from<object, value&> && !object::constructor_copy_value<value&>);
static_assert(std::constructible_from<object, value_ref&> && !object::constructor_copy_value<value_ref&>);
static_assert(std::constructible_from<object, const_value_ref&> &&
object::constructor_copy_value<const_value_ref&>);
static_assert(std::constructible_from<object, const value&> && object::constructor_copy_value<const value&>);
static_assert(std::constructible_from<object, const value_ref&> &&
object::constructor_copy_value<const value_ref&>);
static_assert(std::constructible_from<object, const const_value_ref&> &&
object::constructor_copy_value<const const_value_ref&>);
static_assert(std::constructible_from<object, value&&> && !object::constructor_copy_value<value>);
static_assert(std::constructible_from<object, value_ref&&> && !object::constructor_copy_value<value_ref>);
static_assert(std::constructible_from<object, const_value_ref&&> &&
object::constructor_copy_value<const_value_ref>);
static_assert(std::constructible_from<object_ref, value&>);
static_assert(std::constructible_from<object_ref, value_ref&> && !object_ref::constructor_copy_value<value_ref&>);
static_assert(!std::constructible_from<object_ref, const_value_ref&>);
static_assert(!std::constructible_from<object_ref, const value&>);
static_assert(std::constructible_from<object_ref, const value_ref&> &&
!object_ref::constructor_copy_value<const value_ref&>);
static_assert(!std::constructible_from<object_ref, const const_value_ref&>);
static_assert(!std::constructible_from<object_ref, value&&>);
static_assert(std::constructible_from<object_ref, value_ref&&> && !object_ref::constructor_copy_value<value_ref>);
static_assert(!std::constructible_from<object_ref, const_value_ref&&>);
static_assert(std::constructible_from<const_object_ref, value&>);
static_assert(std::constructible_from<const_object_ref, value_ref&>);
static_assert(std::constructible_from<const_object_ref, const_value_ref&> &&
!const_object_ref::constructor_copy_value<const_value_ref&>);
static_assert(std::constructible_from<const_object_ref, const value&>);
static_assert(std::constructible_from<const_object_ref, const value_ref&>);
static_assert(std::constructible_from<const_object_ref, const const_value_ref&> &&
!const_object_ref::constructor_copy_value<const const_value_ref&>);
static_assert(!std::constructible_from<const_object_ref, value&&>);
static_assert(std::constructible_from<const_object_ref, value_ref&&>);
static_assert(std::constructible_from<const_object_ref, const_value_ref&&> &&
!const_object_ref::constructor_copy_value<const_value_ref>);
}
TEST(Writer, ValueConversion)
{
using yyjson::copy_string_t;
using namespace yyjson::writer; // NOLINT
// convert primitive type to value
static_assert(std::constructible_from<value, std::nullptr_t&&>);
static_assert(std::constructible_from<value, std::nullptr_t&>);
static_assert(std::constructible_from<value, const std::nullptr_t&>);
static_assert(std::constructible_from<value, bool&&>);
static_assert(std::constructible_from<value, bool&>);
static_assert(std::constructible_from<value, const bool&>);
static_assert(std::constructible_from<value, int&&>);
static_assert(std::constructible_from<value, int&>);
static_assert(std::constructible_from<value, const int&>);
static_assert(std::constructible_from<value, unsigned&&>);
static_assert(std::constructible_from<value, unsigned&>);
static_assert(std::constructible_from<value, const unsigned&>);
static_assert(std::constructible_from<value, std::string&&>);
static_assert(std::constructible_from<value, std::string&>);
static_assert(std::constructible_from<value, const std::string&>);
static_assert(std::constructible_from<value, std::string_view&&>);
static_assert(std::constructible_from<value, std::string_view&>);
static_assert(std::constructible_from<value, const std::string_view&>);
static_assert(std::constructible_from<value, char*>);
static_assert(std::constructible_from<value, const char*>);
// convert immutable json value
static_assert(std::constructible_from<value, const yyjson::reader::const_value_ref&>);
static_assert(std::constructible_from<value, const yyjson::reader::const_array_ref&>);
static_assert(std::constructible_from<value, const yyjson::reader::const_object_ref&>);
// convert array-like type to value
static_assert(std::constructible_from<value, const std::vector<int>&>);
static_assert(std::constructible_from<value, const std::list<int>&>);
static_assert(std::constructible_from<value, const std::vector<std::list<int>>&>);
static_assert(std::constructible_from<value, std::initializer_list<int>>);
static_assert(std::constructible_from<value, std::initializer_list<value>>);
static_assert(std::constructible_from<value, std::initializer_list<value_ref>>);
static_assert(std::constructible_from<value, std::initializer_list<const_value_ref>>);
static_assert(std::constructible_from<value, std::initializer_list<array>>);
static_assert(std::constructible_from<value, std::initializer_list<array_ref>>);
static_assert(std::constructible_from<value, std::initializer_list<const_array_ref>>);
static_assert(std::constructible_from<value, std::initializer_list<object>>);
static_assert(std::constructible_from<value, std::initializer_list<object_ref>>);
static_assert(std::constructible_from<value, std::initializer_list<const_object_ref>>);
static_assert(std::constructible_from<value, const std::vector<value>&>);
static_assert(std::constructible_from<value, const std::vector<array>&>);
static_assert(std::constructible_from<value, const std::vector<object>&>);
static_assert(std::constructible_from<value, const std::vector<yyjson::reader::const_value_ref>&>);
static_assert(std::constructible_from<value, const std::vector<yyjson::reader::const_array_ref>&>);
static_assert(std::constructible_from<value, const std::vector<yyjson::reader::const_object_ref>&>);
static_assert(std::constructible_from<value, std::initializer_list<yyjson::reader::const_value_ref>>);
static_assert(std::constructible_from<value, std::initializer_list<yyjson::reader::const_array_ref>>);
static_assert(std::constructible_from<value, std::initializer_list<yyjson::reader::const_object_ref>>);
// convert object-like type to value
static_assert(std::constructible_from<value, const std::map<std::string_view, int>&>);
static_assert(std::constructible_from<value, const std::map<std::string, int>&>);
static_assert(std::constructible_from<value, const std::map<const char*, int>&>);
static_assert(std::constructible_from<value, const std::map<std::string_view, value>&>);
static_assert(std::constructible_from<value, const std::map<std::string, array>&>);
static_assert(std::constructible_from<value, const std::map<const char*, object>&>);
static_assert(std::constructible_from<value, const std::map<std::string_view, std::map<std::string_view, int>>&>);
static_assert(std::constructible_from<value, const std::map<std::string, std::vector<value>>&>);
static_assert(
std::constructible_from<value, const std::map<const char*, std::map<std::string_view, std::vector<array>>>&>);
static_assert(std::constructible_from<value, std::initializer_list<key_value_pair>>);
static_assert(std::constructible_from<value, const std::map<std::string_view, yyjson::reader::const_value_ref>&>);
// convert primitive type to value (with copy string tag)
static_assert(std::constructible_from<value, std::nullptr_t&&, copy_string_t>);
static_assert(std::constructible_from<value, std::nullptr_t&, copy_string_t>);
static_assert(std::constructible_from<value, const std::nullptr_t&, copy_string_t>);
static_assert(std::constructible_from<value, bool&&, copy_string_t>);
static_assert(std::constructible_from<value, bool&, copy_string_t>);
static_assert(std::constructible_from<value, const bool&, copy_string_t>);
static_assert(std::constructible_from<value, int&&, copy_string_t>);
static_assert(std::constructible_from<value, int&, copy_string_t>);
static_assert(std::constructible_from<value, const int&, copy_string_t>);
static_assert(std::constructible_from<value, unsigned&&, copy_string_t>);
static_assert(std::constructible_from<value, unsigned&, copy_string_t>);
static_assert(std::constructible_from<value, const unsigned&, copy_string_t>);
static_assert(std::constructible_from<value, std::string&&, copy_string_t>);
static_assert(std::constructible_from<value, std::string&, copy_string_t>);
static_assert(std::constructible_from<value, const std::string&, copy_string_t>);
static_assert(std::constructible_from<value, std::string_view&&, copy_string_t>);
static_assert(std::constructible_from<value, std::string_view&, copy_string_t>);
static_assert(std::constructible_from<value, const std::string_view&, copy_string_t>);
static_assert(std::constructible_from<value, char*, copy_string_t>);
static_assert(std::constructible_from<value, const char*, copy_string_t>);
// unsupported conversion (value_ref)
static_assert(!std::constructible_from<value_ref, std::nullptr_t&&>);
static_assert(!std::constructible_from<value_ref, std::nullptr_t&>);
static_assert(!std::constructible_from<value_ref, const std::nullptr_t&>);
static_assert(!std::constructible_from<value_ref, bool&&>);
static_assert(!std::constructible_from<value_ref, bool&>);
static_assert(!std::constructible_from<value_ref, const bool&>);
static_assert(!std::constructible_from<value_ref, int&&>);
static_assert(!std::constructible_from<value_ref, int&>);
static_assert(!std::constructible_from<value_ref, const int&>);
static_assert(!std::constructible_from<value_ref, unsigned&&>);
static_assert(!std::constructible_from<value_ref, unsigned&>);
static_assert(!std::constructible_from<value_ref, const unsigned&>);
static_assert(!std::constructible_from<value_ref, std::string&&>);
static_assert(!std::constructible_from<value_ref, std::string&>);
static_assert(!std::constructible_from<value_ref, const std::string&>);
static_assert(!std::constructible_from<value_ref, std::string_view&&>);
static_assert(!std::constructible_from<value_ref, std::string_view&>);
static_assert(!std::constructible_from<value_ref, const std::string_view&>);
static_assert(!std::constructible_from<value_ref, char*>);
static_assert(!std::constructible_from<value_ref, const char*>);
// unsupported conversion (const_value_ref)
static_assert(!std::constructible_from<const value_ref, std::nullptr_t&&>);
static_assert(!std::constructible_from<const value_ref, std::nullptr_t&>);
static_assert(!std::constructible_from<const value_ref, const std::nullptr_t&>);
static_assert(!std::constructible_from<const value_ref, bool&&>);
static_assert(!std::constructible_from<const value_ref, bool&>);
static_assert(!std::constructible_from<const value_ref, const bool&>);
static_assert(!std::constructible_from<const value_ref, int&&>);
static_assert(!std::constructible_from<const value_ref, int&>);
static_assert(!std::constructible_from<const value_ref, const int&>);
static_assert(!std::constructible_from<const value_ref, unsigned&&>);
static_assert(!std::constructible_from<const value_ref, unsigned&>);
static_assert(!std::constructible_from<const value_ref, const unsigned&>);
static_assert(!std::constructible_from<const value_ref, std::string&&>);
static_assert(!std::constructible_from<const value_ref, std::string&>);
static_assert(!std::constructible_from<const value_ref, const std::string&>);
static_assert(!std::constructible_from<const value_ref, std::string_view&&>);
static_assert(!std::constructible_from<const value_ref, std::string_view&>);
static_assert(!std::constructible_from<const value_ref, const std::string_view&>);
static_assert(!std::constructible_from<const value_ref, char*>);
static_assert(!std::constructible_from<const value_ref, const char*>);
// constructor + type check
EXPECT_TRUE(value().is_null());
EXPECT_EQ(nullptr, *value().as_null());
EXPECT_TRUE(value(nullptr).is_null());
EXPECT_EQ(nullptr, *value(nullptr).as_null());
EXPECT_TRUE(value(true).is_bool());
EXPECT_EQ(true, *value(true).as_bool());
EXPECT_TRUE(value(false).is_bool());
EXPECT_EQ(false, *value(false).as_bool());
EXPECT_TRUE(value(true).is_true());
EXPECT_TRUE(value(false).is_false());
EXPECT_TRUE(value(1).is_num());
EXPECT_TRUE(value(1).is_int());
EXPECT_EQ(1, *value(1).as_int());
EXPECT_TRUE(value(1U).is_uint());
EXPECT_EQ(1U, *value(1U).as_uint());
EXPECT_TRUE(value(-1).is_num());
EXPECT_TRUE(value(-1).is_sint());
EXPECT_EQ(-1, *value(-1).as_sint());
EXPECT_TRUE(value(-1.5).is_num());
EXPECT_TRUE(value(-1.5).is_real());
EXPECT_EQ(-1.5, *value(-1.5).as_real());
EXPECT_TRUE(value("string").is_string());
EXPECT_EQ("string", *value("string").as_string());
{
auto str = std::string("string");
auto str_v = std::string_view(str.data());
auto val_str = value(str);
auto val_str_v = value(str_v);
auto val_str_cp = value(str, yyjson::copy_string);
auto val_str_v_cp = value(str_v, yyjson::copy_string);
EXPECT_TRUE(val_str.is_string());
EXPECT_EQ("string", *val_str.as_string());
EXPECT_TRUE(val_str_v.is_string());
EXPECT_EQ("string", *val_str_v.as_string());
EXPECT_TRUE(val_str_cp.is_string());
EXPECT_EQ("string", *val_str_cp.as_string());
EXPECT_TRUE(val_str_v_cp.is_string());
EXPECT_EQ("string", *val_str_v_cp.as_string());
str[0] = 'x';
EXPECT_EQ("xtring", *val_str.as_string());
EXPECT_EQ("xtring", *val_str_v.as_string());
EXPECT_EQ("string", *val_str_cp.as_string());
EXPECT_EQ("string", *val_str_v_cp.as_string());
auto val_str_mv = value(std::string(str));
EXPECT_TRUE(val_str_mv.is_string());
EXPECT_EQ("xtring", *val_str_mv.as_string());
}
// assign primitives for value
static_assert(std::is_assignable_v<value, std::nullptr_t>);
static_assert(std::is_assignable_v<value, bool>);
static_assert(std::is_assignable_v<value, int>);
static_assert(std::is_assignable_v<value, unsigned>);
static_assert(std::is_assignable_v<value, std::string>);
static_assert(std::is_assignable_v<value, std::string&>);
static_assert(std::is_assignable_v<value, const std::string&>);
static_assert(std::is_assignable_v<value, std::string_view>);
static_assert(std::is_assignable_v<value, char*>);
static_assert(std::is_assignable_v<value, const char*>);
static_assert(std::is_assignable_v<value&, std::nullptr_t>);
static_assert(std::is_assignable_v<value&, bool>);
static_assert(std::is_assignable_v<value&, int>);
static_assert(std::is_assignable_v<value&, unsigned>);
static_assert(std::is_assignable_v<value&, std::string>);
static_assert(std::is_assignable_v<value&, std::string&>);
static_assert(std::is_assignable_v<value&, const std::string&>);
static_assert(std::is_assignable_v<value&, std::string_view>);
static_assert(std::is_assignable_v<value&, char*>);
static_assert(std::is_assignable_v<value&, const char*>);
static_assert(std::is_assignable_v<value, std::tuple<std::nullptr_t, copy_string_t>&>);
static_assert(std::is_assignable_v<value, const std::tuple<bool, copy_string_t>&>);
static_assert(std::is_assignable_v<value, std::tuple<int, copy_string_t>>);
static_assert(std::is_assignable_v<value, std::tuple<unsigned, copy_string_t>>);
static_assert(std::is_assignable_v<value, std::tuple<std::string, copy_string_t>>);
static_assert(std::is_assignable_v<value, std::tuple<std::string&, copy_string_t>>);
static_assert(std::is_assignable_v<value, std::tuple<const std::string&, copy_string_t>>);
static_assert(std::is_assignable_v<value, std::tuple<std::string_view, copy_string_t>>);
static_assert(std::is_assignable_v<value, std::tuple<char*, copy_string_t>>);
static_assert(std::is_assignable_v<value, std::tuple<const char*, copy_string_t>>);
static_assert(std::is_assignable_v<value, std::tuple<value, copy_string_t>>);
auto cc = value();
cc = std::tuple<bool, copy_string_t>();
// assign primitives for value_ref
static_assert(std::is_assignable_v<value_ref, std::nullptr_t>);
static_assert(std::is_assignable_v<value_ref, bool>);
static_assert(std::is_assignable_v<value_ref, int>);
static_assert(std::is_assignable_v<value_ref, unsigned>);
static_assert(std::is_assignable_v<value_ref, std::string>);
static_assert(std::is_assignable_v<value_ref, std::string&>);
static_assert(std::is_assignable_v<value_ref, const std::string&>);
static_assert(std::is_assignable_v<value_ref, std::string_view>);
static_assert(std::is_assignable_v<value_ref, char*>);
static_assert(std::is_assignable_v<value_ref, const char*>);
static_assert(std::is_assignable_v<value_ref, std::nullptr_t>);
static_assert(std::is_assignable_v<value_ref&, bool>);
static_assert(std::is_assignable_v<value_ref&, int>);
static_assert(std::is_assignable_v<value_ref&, unsigned>);
static_assert(std::is_assignable_v<value_ref&, std::string>);
static_assert(std::is_assignable_v<value_ref&, std::string&>);
static_assert(std::is_assignable_v<value_ref&, const std::string&>);
static_assert(std::is_assignable_v<value_ref&, std::string_view>);
static_assert(std::is_assignable_v<value_ref&, char*>);
static_assert(std::is_assignable_v<value_ref&, const char*>);
// unsupported assign
static_assert(!std::is_assignable_v<const value&, std::nullptr_t>);
static_assert(!std::is_assignable_v<const value&, bool>);
static_assert(!std::is_assignable_v<const value&, int>);
static_assert(!std::is_assignable_v<const value&, unsigned>);
static_assert(!std::is_assignable_v<const value&, std::string>);
static_assert(!std::is_assignable_v<const value&, std::string&>);
static_assert(!std::is_assignable_v<const value&, const std::string&>);
static_assert(!std::is_assignable_v<const value&, std::string_view>);
static_assert(!std::is_assignable_v<const value&, char*>);
static_assert(!std::is_assignable_v<const value&, const char*>);
static_assert(!std::is_assignable_v<const value_ref&, bool>);
static_assert(!std::is_assignable_v<const value_ref&, int>);
static_assert(!std::is_assignable_v<const value_ref&, unsigned>);
static_assert(!std::is_assignable_v<const value_ref&, std::string>);
static_assert(!std::is_assignable_v<const value_ref&, std::string&>);
static_assert(!std::is_assignable_v<const value_ref&, const std::string&>);
static_assert(!std::is_assignable_v<const value_ref&, std::string_view>);
static_assert(!std::is_assignable_v<const value_ref&, char*>);
static_assert(!std::is_assignable_v<const value_ref&, const char*>);
static_assert(!std::is_assignable_v<const_value_ref, bool>);
static_assert(!std::is_assignable_v<const_value_ref, int>);
static_assert(!std::is_assignable_v<const_value_ref, unsigned>);
static_assert(!std::is_assignable_v<const_value_ref, std::string>);
static_assert(!std::is_assignable_v<const_value_ref, std::string&>);
static_assert(!std::is_assignable_v<const_value_ref, const std::string&>);
static_assert(!std::is_assignable_v<const_value_ref, std::string_view>);
static_assert(!std::is_assignable_v<const_value_ref, char*>);
static_assert(!std::is_assignable_v<const_value_ref, const char*>);
static_assert(!std::is_assignable_v<const_value_ref&, bool>);
static_assert(!std::is_assignable_v<const_value_ref&, int>);
static_assert(!std::is_assignable_v<const_value_ref&, unsigned>);
static_assert(!std::is_assignable_v<const_value_ref&, std::string>);
static_assert(!std::is_assignable_v<const_value_ref&, std::string&>);
static_assert(!std::is_assignable_v<const_value_ref&, const std::string&>);
static_assert(!std::is_assignable_v<const_value_ref&, std::string_view>);
static_assert(!std::is_assignable_v<const_value_ref&, char*>);
static_assert(!std::is_assignable_v<const_value_ref&, const char*>);
static_assert(!std::is_assignable_v<const const_value_ref&, bool>);
static_assert(!std::is_assignable_v<const const_value_ref&, int>);
static_assert(!std::is_assignable_v<const const_value_ref&, unsigned>);
static_assert(!std::is_assignable_v<const const_value_ref&, std::string>);
static_assert(!std::is_assignable_v<const const_value_ref&, std::string&>);
static_assert(!std::is_assignable_v<const const_value_ref&, const std::string&>);
static_assert(!std::is_assignable_v<const const_value_ref&, std::string_view>);
static_assert(!std::is_assignable_v<const const_value_ref&, char*>);
static_assert(!std::is_assignable_v<const const_value_ref&, const char*>);
// assign json
static_assert(std::is_assignable_v<value, value>);
static_assert(std::is_assignable_v<value, value&>);
static_assert(std::is_assignable_v<value, const value&>);
static_assert(std::is_assignable_v<value, value_ref>);
static_assert(std::is_assignable_v<value, value_ref&>);
static_assert(std::is_assignable_v<value, const value_ref&>);
static_assert(std::is_assignable_v<value, const_value_ref>);
static_assert(std::is_assignable_v<value, const_value_ref&>);
static_assert(std::is_assignable_v<value, const const_value_ref&>);
static_assert(std::is_assignable_v<value, array>);
static_assert(std::is_assignable_v<value, array&>);
static_assert(std::is_assignable_v<value, const array&>);
static_assert(std::is_assignable_v<value, array_ref>);
static_assert(std::is_assignable_v<value, array_ref&>);
static_assert(std::is_assignable_v<value, const array_ref&>);
static_assert(std::is_assignable_v<value, const_array_ref>);
static_assert(std::is_assignable_v<value, const_array_ref&>);
static_assert(std::is_assignable_v<value, const const_array_ref&>);
static_assert(std::is_assignable_v<value, object>);
static_assert(std::is_assignable_v<value, object&>);
static_assert(std::is_assignable_v<value, const object&>);
static_assert(std::is_assignable_v<value, object_ref>);
static_assert(std::is_assignable_v<value, object_ref&>);
static_assert(std::is_assignable_v<value, const object_ref&>);
static_assert(std::is_assignable_v<value, const_object_ref>);
static_assert(std::is_assignable_v<value, const_object_ref&>);
static_assert(std::is_assignable_v<value, const const_object_ref&>);
static_assert(std::is_assignable_v<value_ref, value>);
static_assert(std::is_assignable_v<value_ref, value&>);
static_assert(std::is_assignable_v<value_ref, const value&>);
static_assert(std::is_assignable_v<value_ref, value_ref>);
static_assert(std::is_assignable_v<value_ref, value_ref&>);
static_assert(std::is_assignable_v<value_ref, const value_ref&>);
static_assert(std::is_assignable_v<value_ref, const_value_ref>);
static_assert(std::is_assignable_v<value_ref, const_value_ref&>);
static_assert(std::is_assignable_v<value_ref, const const_value_ref&>);
static_assert(std::is_assignable_v<value_ref, array>);
static_assert(std::is_assignable_v<value_ref, array&>);
static_assert(std::is_assignable_v<value_ref, const array&>);
static_assert(std::is_assignable_v<value_ref, array_ref>);
static_assert(std::is_assignable_v<value_ref, array_ref&>);
static_assert(std::is_assignable_v<value_ref, const array_ref&>);
static_assert(std::is_assignable_v<value_ref, const_array_ref>);
static_assert(std::is_assignable_v<value_ref, const_array_ref&>);
static_assert(std::is_assignable_v<value_ref, const const_array_ref&>);
static_assert(std::is_assignable_v<value_ref, object>);
static_assert(std::is_assignable_v<value_ref, object&>);
static_assert(std::is_assignable_v<value_ref, const object&>);
static_assert(std::is_assignable_v<value_ref, object_ref>);
static_assert(std::is_assignable_v<value_ref, object_ref&>);
static_assert(std::is_assignable_v<value_ref, const object_ref&>);
static_assert(std::is_assignable_v<value_ref, const_object_ref>);
static_assert(std::is_assignable_v<value_ref, const_object_ref&>);
static_assert(std::is_assignable_v<value_ref, const const_object_ref&>);
// assign immutable json value
static_assert(std::is_assignable_v<value, const yyjson::reader::const_value_ref&>);
static_assert(std::is_assignable_v<value, const yyjson::reader::const_array_ref&>);
static_assert(std::is_assignable_v<value, const yyjson::reader::const_object_ref&>);
// assign array constructible
static_assert(std::is_assignable_v<value, const std::vector<int>&>);
static_assert(std::is_assignable_v<value, const std::list<int>&>);
static_assert(std::is_assignable_v<value, const std::vector<std::list<int>>&>);
static_assert(std::is_assignable_v<value, std::initializer_list<int>>);
static_assert(std::is_assignable_v<value, std::initializer_list<value>>);
static_assert(std::is_assignable_v<value, std::initializer_list<value_ref>>);
static_assert(std::is_assignable_v<value, std::initializer_list<const_value_ref>>);
static_assert(std::is_assignable_v<value, std::initializer_list<array>>);
static_assert(std::is_assignable_v<value, std::initializer_list<array_ref>>);
static_assert(std::is_assignable_v<value, std::initializer_list<const_array_ref>>);
static_assert(std::is_assignable_v<value, std::initializer_list<object>>);
static_assert(std::is_assignable_v<value, std::initializer_list<object_ref>>);
static_assert(std::is_assignable_v<value, std::initializer_list<const_object_ref>>);
static_assert(std::is_assignable_v<value, const std::vector<value>&>);
static_assert(std::is_assignable_v<value, const std::vector<array>&>);
static_assert(std::is_assignable_v<value, const std::vector<object>&>);
static_assert(std::is_assignable_v<value, const std::vector<yyjson::reader::const_value_ref>&>);
static_assert(std::is_assignable_v<value, const std::vector<yyjson::reader::const_array_ref>&>);
static_assert(std::is_assignable_v<value, const std::vector<yyjson::reader::const_object_ref>&>);
static_assert(std::is_assignable_v<value, std::initializer_list<yyjson::reader::const_value_ref>>);
static_assert(std::is_assignable_v<value, std::initializer_list<yyjson::reader::const_array_ref>>);
static_assert(std::is_assignable_v<value, std::initializer_list<yyjson::reader::const_object_ref>>);
// assign object constructible
static_assert(std::is_assignable_v<value, const std::map<std::string_view, int>&>);
static_assert(std::is_assignable_v<value, const std::map<std::string, int>&>);
static_assert(std::is_assignable_v<value, const std::map<const char*, int>&>);
static_assert(std::is_assignable_v<value, const std::map<std::string_view, value>&>);
static_assert(std::is_assignable_v<value, const std::map<std::string, array>&>);
static_assert(std::is_assignable_v<value, const std::map<const char*, object>&>);
static_assert(std::is_assignable_v<value, const std::map<std::string_view, std::map<std::string_view, int>>&>);
static_assert(std::is_assignable_v<value, const std::map<std::string, std::vector<value>>&>);
static_assert(
std::is_assignable_v<value, const std::map<const char*, std::map<std::string_view, std::vector<array>>>&>);
static_assert(std::is_assignable_v<value, std::initializer_list<key_value_pair>>);
static_assert(std::is_assignable_v<value, const std::map<std::string_view, yyjson::reader::const_value_ref>&>);
// assign + type check
auto v = value();
EXPECT_TRUE(v.is_null());
EXPECT_EQ(nullptr, *v.as_null());
v = true;
EXPECT_TRUE(v.is_bool());
EXPECT_EQ(true, *v.as_bool());
v = false;
EXPECT_TRUE(v.is_bool());
EXPECT_EQ(false, *v.as_bool());
EXPECT_TRUE(v.is_false());
v = true;
EXPECT_TRUE(v.is_true());
v = 1;
EXPECT_TRUE(v.is_num());
EXPECT_TRUE(v.is_int());
EXPECT_EQ(1, *v.as_int());
v = 1U;
EXPECT_TRUE(v.is_uint());
EXPECT_EQ(1U, *v.as_uint());
v = -1;
EXPECT_TRUE(v.is_num());
EXPECT_TRUE(v.is_sint());
EXPECT_EQ(-1, *v.as_sint());
v = -1.5;
EXPECT_TRUE(v.is_num());
EXPECT_TRUE(v.is_real());
EXPECT_EQ(-1.5, *v.as_real());
EXPECT_EQ(0, v.count_children());
v = std::vector<int>{};
EXPECT_EQ(0, v.count_children());
v = value();
EXPECT_TRUE(v.is_null());
EXPECT_EQ(nullptr, *v.as_null());
EXPECT_EQ(1, v.count_children());
v = {1, value(2), value(3)};
EXPECT_TRUE(v.is_array());
EXPECT_EQ(4, v.count_children());
v = {{"a", 1}, {"b", value(2)}, {"c", value(3)}};
EXPECT_TRUE(v.is_object());
EXPECT_EQ(7, v.count_children());
v = {{{"a", 1}, {"b", value(2)}, {"c", value(3)}}, yyjson::copy_string};
EXPECT_TRUE(v.is_object());
EXPECT_EQ(8, v.count_children());
// method return types
static_assert(std::same_as<decltype(std::declval<value&>().as_int()), std::optional<std::int64_t>>);
static_assert(std::same_as<decltype(std::declval<const value&>().as_int()), std::optional<std::int64_t>>);
static_assert(std::same_as<decltype(std::declval<value_ref&>().as_int()), std::optional<std::int64_t>>);
static_assert(std::same_as<decltype(std::declval<const value_ref&>().as_int()), std::optional<std::int64_t>>);
static_assert(std::same_as<decltype(std::declval<const_value_ref&>().as_int()), std::optional<std::int64_t>>);
static_assert(std::same_as<decltype(std::declval<const const_value_ref&>().as_int()), std::optional<std::int64_t>>);
static_assert(std::same_as<decltype(std::declval<value&>().as_uint()), std::optional<std::uint64_t>>);
static_assert(std::same_as<decltype(std::declval<const value&>().as_uint()), std::optional<std::uint64_t>>);
static_assert(std::same_as<decltype(std::declval<value_ref&>().as_uint()), std::optional<std::uint64_t>>);
static_assert(std::same_as<decltype(std::declval<const value_ref&>().as_uint()), std::optional<std::uint64_t>>);
static_assert(std::same_as<decltype(std::declval<const_value_ref&>().as_uint()), std::optional<std::uint64_t>>);
static_assert(
std::same_as<decltype(std::declval<const const_value_ref&>().as_uint()), std::optional<std::uint64_t>>);
static_assert(std::same_as<decltype(std::declval<value&>().as_sint()), std::optional<std::int64_t>>);
static_assert(std::same_as<decltype(std::declval<const value&>().as_sint()), std::optional<std::int64_t>>);
static_assert(std::same_as<decltype(std::declval<value_ref&>().as_sint()), std::optional<std::int64_t>>);
static_assert(std::same_as<decltype(std::declval<const value_ref&>().as_sint()), std::optional<std::int64_t>>);
static_assert(std::same_as<decltype(std::declval<const_value_ref&>().as_sint()), std::optional<std::int64_t>>);
static_assert(
std::same_as<decltype(std::declval<const const_value_ref&>().as_sint()), std::optional<std::int64_t>>);
static_assert(std::same_as<decltype(std::declval<value&>().as_bool()), std::optional<bool>>);
static_assert(std::same_as<decltype(std::declval<const value&>().as_bool()), std::optional<bool>>);
static_assert(std::same_as<decltype(std::declval<value_ref&>().as_bool()), std::optional<bool>>);
static_assert(std::same_as<decltype(std::declval<const value_ref&>().as_bool()), std::optional<bool>>);
static_assert(std::same_as<decltype(std::declval<const_value_ref&>().as_bool()), std::optional<bool>>);
static_assert(std::same_as<decltype(std::declval<const const_value_ref&>().as_bool()), std::optional<bool>>);
static_assert(std::same_as<decltype(std::declval<value&>().as_num()), std::optional<double>>);
static_assert(std::same_as<decltype(std::declval<const value&>().as_num()), std::optional<double>>);
static_assert(std::same_as<decltype(std::declval<value_ref&>().as_num()), std::optional<double>>);
static_assert(std::same_as<decltype(std::declval<const value_ref&>().as_num()), std::optional<double>>);
static_assert(std::same_as<decltype(std::declval<const_value_ref&>().as_num()), std::optional<double>>);
static_assert(std::same_as<decltype(std::declval<const const_value_ref&>().as_num()), std::optional<double>>);
static_assert(std::same_as<decltype(std::declval<value&>().as_real()), std::optional<double>>);
static_assert(std::same_as<decltype(std::declval<const value&>().as_real()), std::optional<double>>);
static_assert(std::same_as<decltype(std::declval<value_ref&>().as_real()), std::optional<double>>);
static_assert(std::same_as<decltype(std::declval<const value_ref&>().as_real()), std::optional<double>>);
static_assert(std::same_as<decltype(std::declval<const_value_ref&>().as_real()), std::optional<double>>);
static_assert(std::same_as<decltype(std::declval<const const_value_ref&>().as_real()), std::optional<double>>);
static_assert(std::same_as<decltype(std::declval<value&>().as_null()), std::optional<std::nullptr_t>>);
static_assert(std::same_as<decltype(std::declval<const value&>().as_null()), std::optional<std::nullptr_t>>);
static_assert(std::same_as<decltype(std::declval<value_ref&>().as_null()), std::optional<std::nullptr_t>>);
static_assert(std::same_as<decltype(std::declval<const value_ref&>().as_null()), std::optional<std::nullptr_t>>);
static_assert(std::same_as<decltype(std::declval<const_value_ref&>().as_null()), std::optional<std::nullptr_t>>);
static_assert(
std::same_as<decltype(std::declval<const const_value_ref&>().as_null()), std::optional<std::nullptr_t>>);
static_assert(std::same_as<decltype(std::declval<value&>().as_string()), std::optional<std::string_view>>);
static_assert(std::same_as<decltype(std::declval<const value&>().as_string()), std::optional<std::string_view>>);
static_assert(std::same_as<decltype(std::declval<value_ref&>().as_string()), std::optional<std::string_view>>);
static_assert(
std::same_as<decltype(std::declval<const value_ref&>().as_string()), std::optional<std::string_view>>);
static_assert(
std::same_as<decltype(std::declval<const_value_ref&>().as_string()), std::optional<std::string_view>>);
static_assert(
std::same_as<decltype(std::declval<const const_value_ref&>().as_string()), std::optional<std::string_view>>);
static_assert(std::same_as<decltype(std::declval<value&>().as_array()), std::optional<array_ref>>);
static_assert(std::same_as<decltype(std::declval<value_ref&>().as_array()), std::optional<array_ref>>);
static_assert(std::same_as<decltype(std::declval<const_value_ref&>().as_array()), std::optional<const_array_ref>>);
static_assert(std::same_as<decltype(std::declval<const value&>().as_array()), std::optional<const_array_ref>>);
static_assert(std::same_as<decltype(std::declval<const value_ref&>().as_array()), std::optional<const_array_ref>>);
static_assert(
std::same_as<decltype(std::declval<const const_value_ref&>().as_array()), std::optional<const_array_ref>>);
static_assert(std::same_as<decltype(std::declval<value&&>().as_array()), std::optional<array>>);
static_assert(std::same_as<decltype(std::declval<value_ref&&>().as_array()), std::optional<array_ref>>);
static_assert(std::same_as<decltype(std::declval<const_value_ref&&>().as_array()), std::optional<const_array_ref>>);
static_assert(std::same_as<decltype(std::declval<value&>().as_object()), std::optional<object_ref>>);
static_assert(std::same_as<decltype(std::declval<value_ref&>().as_object()), std::optional<object_ref>>);
static_assert(
std::same_as<decltype(std::declval<const_value_ref&>().as_object()), std::optional<const_object_ref>>);
static_assert(std::same_as<decltype(std::declval<const value&>().as_object()), std::optional<const_object_ref>>);
static_assert(
std::same_as<decltype(std::declval<const value_ref&>().as_object()), std::optional<const_object_ref>>);
static_assert(
std::same_as<decltype(std::declval<const const_value_ref&>().as_object()), std::optional<const_object_ref>>);
static_assert(std::same_as<decltype(std::declval<value&&>().as_object()), std::optional<object>>);
static_assert(std::same_as<decltype(std::declval<value_ref&&>().as_object()), std::optional<object_ref>>);
static_assert(
std::same_as<decltype(std::declval<const_value_ref&&>().as_object()), std::optional<const_object_ref>>);
}
TEST(Writer, ValueExamples)
{
using namespace yyjson;
// primitive values
{
auto b = true;
auto n = nullptr;
auto i = -1;
auto d = 3.14;
EXPECT_EQ(fmt::format("{}", b), value(b).write());
EXPECT_EQ("null", value(n).write());
EXPECT_EQ(fmt::format("{}", i), value(static_cast<std::int8_t>(i)).write());
EXPECT_EQ(fmt::format("{}", i), value(static_cast<std::int16_t>(i)).write());
EXPECT_EQ(fmt::format("{}", i), value(static_cast<std::int32_t>(i)).write());
EXPECT_EQ(fmt::format("{}", i), value(static_cast<std::int64_t>(i)).write());
EXPECT_EQ(fmt::format("{}", d), value(d).write());
}
// string
auto str1 = "abcdef";
auto str2 = "ghijkl";
auto cp_value = value(str1);
auto sv_value = value(std::string_view(str1));
EXPECT_EQ(fmt::format(R"("{}")", str1), cp_value.write());
EXPECT_EQ(fmt::format(R"("{}")", str1), sv_value.write());
// check life time
auto std_str = std::string(str1);
auto str_value = value(std_str);
EXPECT_EQ(fmt::format(R"("{}")", str1), str_value.write());
std_str = std::string(str2);
EXPECT_EQ(fmt::format(R"("{}")", str2), str_value.write());
str_value = value(std_str, copy_string);
EXPECT_EQ(fmt::format(R"("{}")", str2), str_value.write());
std_str = std::string(str1);
EXPECT_EQ(fmt::format(R"("{}")", str2), str_value.write());
// array
EXPECT_EQ("[1,2,3]", value(std::vector{1, 2, 3}).write());
EXPECT_EQ(R"(["a","b","c"])", value(std::vector{"a", "b", "c"}).write());
EXPECT_EQ("[true,false,true]", value(std::list{true, false, true}).write());
EXPECT_EQ("[2.74,3.14]", value(std::list{2.74, 3.14}).write());
EXPECT_EQ(R"([1,"2",3.0])", value(std::list{value(1), value("2"), value(3.0)}).write());
// nested array
EXPECT_EQ("[[1,2],[3,4]]", value(std::list<std::vector<int>>{{1, 2}, {3, 4}}).write());
EXPECT_EQ(R"([["a","b"],["c","d"]])", value(std::list<std::vector<std::string>>{{"a", "b"}, {"c", "d"}}).write());
EXPECT_EQ(R"([{"0":0,"1":1},{"2":2,"3":3}])",
value(std::vector<std::map<std::string, int>>{{{"0", 0}, {"1", 1}}, {{"2", 2}, {"3", 3}}}).write());
// key-value container
EXPECT_EQ(R"({"number":1.5,"error":null,"text":"abc"})",
value(std::vector<key_value_pair>{{"number", 1.5}, {"error", nullptr}, {"text", "abc"}}).write());
// conversion from map
EXPECT_EQ(R"({"first":1.5,"second":2.5,"third":3.5})",
value(std::map<std::string_view, double>{{"first", 1.5}, {"second", 2.5}, {"third", 3.5}}).write());
// nested map
auto&& nested_map_num = std::map<std::string, std::map<std::string, int>>{{"key0", {{"a", 0}, {"b", 1}}},
{"key1", {{"a", 2}, {"b", 3}}}};
EXPECT_EQ(R"({"key0":{"a":0,"b":1},"key1":{"a":2,"b":3}})", value(std::move(nested_map_num)).write());
// initializer list
value initlist_arr = {nullptr, true, "2", 3.0, {4.0, "5", false}, {{"7", 8}, {"9", {0}}}};
EXPECT_EQ(R"([null,true,"2",3.0,[4.0,"5",false],{"7":8,"9":[0]}])", fmt::format("{}", initlist_arr));
value initlist_obj = {{"pi", 3.141},
{"happy", true},
{"name", "Niels"},
{"nothing", nullptr},
{"answer", {{"everything", 42}}},
{"list", {0, 1, 2.5, false, "Hello"}},
{"object", {{"currency", "USD"}, {"value", 42.99}}}};
EXPECT_EQ(
R"({"pi":3.141,"happy":true,"name":"Niels","nothing":null,"answer":{"everything":42},"list":[0,1,2.5,false,"Hello"],"object":{"currency":"USD","value":42.99}})",
fmt::format("{}", initlist_obj));
// initializer list with const_value/const_array
{
const auto num_array = *value(std::list<std::vector<int>>{{1, 2}, {3, 4}}).as_array();
const auto const_val = num_array[0];
const auto const_arr = *num_array[1].as_array();
value initlist_arr2 = {nullptr, true, "2", 3.0, {4.0, "5", false}, {{"7", 8}, {"9", {0}}},
const_val, const_arr};
auto initlist_arr3 =
value({nullptr, true, "2", 3.0, {4.0, "5", false}, {{"7", 8}, {"9", {0}}}, const_val, const_arr});
EXPECT_EQ(R"([null,true,"2",3.0,[4.0,"5",false],{"7":8,"9":[0]},[1,2],[3,4]])",
fmt::format("{}", initlist_arr2));
}
// value container
auto val_vector = std::vector<value>{1, 1.5, true, nullptr, "aiueo", std::string("abcde")};
EXPECT_EQ(R"([1,1.5,true,null,"aiueo","abcde"])", fmt::format("{}", value(val_vector)));
// array container
auto arr_vector = std::vector<array>{{1, 1.5}, {true, nullptr}, {"aiueo", std::string("abcde")}};
EXPECT_EQ(R"([[1,1.5],[true,null],["aiueo","abcde"]])", fmt::format("{}", value(arr_vector)));
// object container
auto obj_vector = std::vector<object>{{{"1", 1.5}, {"true", nullptr}}, {{"aiueo", std::string("abcde")}}};
EXPECT_EQ(R"([{"1":1.5,"true":null},{"aiueo":"abcde"}])", fmt::format("{}", value(obj_vector)));
// nested array with copy_string
auto str_vector = std::vector{std::string("aaa"), std::string("bbb"), std::string("ccc")};
{
auto v_temp = std::vector<std::vector<std::string>>{str_vector};
auto result = value(v_temp);
std::ranges::fill(v_temp.front(), "BAD");
EXPECT_EQ(R"([["BAD","BAD","BAD"]])", fmt::format("{}", result));
}
{
auto v_temp = std::vector<std::vector<std::string>>{str_vector};
auto result = value(std::vector<std::vector<std::string>>(v_temp));
std::ranges::fill(v_temp.front(), "BAD");
EXPECT_EQ(R"([["aaa","bbb","ccc"]])", fmt::format("{}", result));
}
{
auto v_temp = std::vector<std::vector<std::string>>{str_vector};
auto result = value(v_temp, copy_string);
std::ranges::fill(v_temp.front(), "BAD");
EXPECT_EQ(R"([["aaa","bbb","ccc"]])", fmt::format("{}", result));
}
#if (!defined(__clang__) || __clang_major__ >= 16) || (__GNUC__ >= 12)
// transform view
EXPECT_EQ("[1,4,9]", fmt::format("{}", value(std::vector{1, 2, 3} |
std::ranges::views::transform([](auto x) { return x * x; }))));
EXPECT_EQ(
R"({"first":1.0,"second":2.0,"third":3.0})",
fmt::format("{}", value(std::map<std::string_view, double>{{"first", 1.5}, {"second", 2.5}, {"third", 3.5}} |
std::ranges::views::transform([](auto x) {
return decltype(x){x.first, x.second - 0.5};
}))));
#endif
// copy assign
{
value v0 = {nullptr, true, {"2", copy_string}, 3.0, {4.0, "5", false}, {{"7", 8}, {"9", {0}}}};
v0 = {nullptr, true, "2", 3.0, {4.0, {"5", copy_string}, false}, {{"7", 8}, {"9", {0}}}};
v0 = {nullptr, true, "2", 3.0, {4.0, "5", false}, {{{"7", 8}, {"9", {0}}}, copy_string}};
}
}
TEST(Writer, ArrayConversion)
{
using yyjson::copy_string_t;
using namespace yyjson::writer; // NOLINT
// convert immutable json value
static_assert(std::constructible_from<array, const yyjson::reader::const_value_ref&>);
static_assert(std::constructible_from<array, const yyjson::reader::const_array_ref&>);
static_assert(std::constructible_from<array, const yyjson::reader::const_object_ref&>);
// unsupported conversion
static_assert(!std::constructible_from<array, std::nullptr_t&&>);
static_assert(!std::constructible_from<array, std::nullptr_t&>);
static_assert(!std::constructible_from<array, const std::nullptr_t&>);
static_assert(!std::constructible_from<array, bool&&>);
static_assert(!std::constructible_from<array, bool&>);
static_assert(!std::constructible_from<array, const bool&>);
static_assert(!std::constructible_from<array, int&&>);
static_assert(!std::constructible_from<array, int&>);
static_assert(!std::constructible_from<array, const int&>);
static_assert(!std::constructible_from<array, unsigned&&>);
static_assert(!std::constructible_from<array, unsigned&>);
static_assert(!std::constructible_from<array, const unsigned&>);
static_assert(!std::constructible_from<array, std::string&&>);
static_assert(!std::constructible_from<array, std::string&>);
static_assert(!std::constructible_from<array, const std::string&>);
static_assert(!std::constructible_from<array, std::string_view&&>);
static_assert(!std::constructible_from<array, std::string_view&>);
static_assert(!std::constructible_from<array, const std::string_view&>);
static_assert(!std::constructible_from<array, char*>);
static_assert(!std::constructible_from<array, const char*>);
static_assert(!std::constructible_from<array_ref, std::nullptr_t&&>);
static_assert(!std::constructible_from<array_ref, std::nullptr_t&>);
static_assert(!std::constructible_from<array_ref, const std::nullptr_t&>);
static_assert(!std::constructible_from<array_ref, bool&&>);
static_assert(!std::constructible_from<array_ref, bool&>);
static_assert(!std::constructible_from<array_ref, const bool&>);
static_assert(!std::constructible_from<array_ref, int&&>);
static_assert(!std::constructible_from<array_ref, int&>);
static_assert(!std::constructible_from<array_ref, const int&>);
static_assert(!std::constructible_from<array_ref, unsigned&&>);
static_assert(!std::constructible_from<array_ref, unsigned&>);
static_assert(!std::constructible_from<array_ref, const unsigned&>);
static_assert(!std::constructible_from<array_ref, std::string&&>);
static_assert(!std::constructible_from<array_ref, std::string&>);
static_assert(!std::constructible_from<array_ref, const std::string&>);
static_assert(!std::constructible_from<array_ref, std::string_view&&>);
static_assert(!std::constructible_from<array_ref, std::string_view&>);
static_assert(!std::constructible_from<array_ref, const std::string_view&>);
static_assert(!std::constructible_from<array_ref, char*>);
static_assert(!std::constructible_from<array_ref, const char*>);
static_assert(!std::constructible_from<const_array_ref, std::nullptr_t&&>);
static_assert(!std::constructible_from<const_array_ref, std::nullptr_t&>);
static_assert(!std::constructible_from<const_array_ref, const std::nullptr_t&>);
static_assert(!std::constructible_from<const_array_ref, bool&&>);
static_assert(!std::constructible_from<const_array_ref, bool&>);
static_assert(!std::constructible_from<const_array_ref, const bool&>);
static_assert(!std::constructible_from<const_array_ref, int&&>);
static_assert(!std::constructible_from<const_array_ref, int&>);
static_assert(!std::constructible_from<const_array_ref, const int&>);
static_assert(!std::constructible_from<const_array_ref, unsigned&&>);
static_assert(!std::constructible_from<const_array_ref, unsigned&>);
static_assert(!std::constructible_from<const_array_ref, const unsigned&>);
static_assert(!std::constructible_from<const_array_ref, std::string&&>);
static_assert(!std::constructible_from<const_array_ref, std::string&>);
static_assert(!std::constructible_from<const_array_ref, const std::string&>);
static_assert(!std::constructible_from<const_array_ref, std::string_view&&>);
static_assert(!std::constructible_from<const_array_ref, std::string_view&>);
static_assert(!std::constructible_from<const_array_ref, const std::string_view&>);
static_assert(!std::constructible_from<const_array_ref, char*>);
static_assert(!std::constructible_from<const_array_ref, const char*>);
// convert array-like type to array
static_assert(std::constructible_from<array, const std::vector<int>&>);
static_assert(std::constructible_from<array, const std::list<int>&>);
static_assert(std::constructible_from<array, const std::vector<std::list<int>>&>);
static_assert(std::constructible_from<array, std::initializer_list<int>>);
static_assert(std::constructible_from<array, std::initializer_list<value>>);
static_assert(std::constructible_from<array, std::initializer_list<value_ref>>);
static_assert(std::constructible_from<array, std::initializer_list<const_value_ref>>);
static_assert(std::constructible_from<array, std::initializer_list<array>>);
static_assert(std::constructible_from<array, std::initializer_list<array_ref>>);
static_assert(std::constructible_from<array, std::initializer_list<const_array_ref>>);
static_assert(std::constructible_from<array, std::initializer_list<object>>);
static_assert(std::constructible_from<array, std::initializer_list<object_ref>>);
static_assert(std::constructible_from<array, std::initializer_list<const_object_ref>>);
static_assert(std::constructible_from<array, const std::vector<value>&>);
static_assert(std::constructible_from<array, const std::vector<array>&>);
static_assert(std::constructible_from<array, const std::vector<object>&>);
static_assert(std::constructible_from<array, const std::vector<yyjson::reader::const_value_ref>&>);
static_assert(std::constructible_from<array, const std::vector<yyjson::reader::const_array_ref>&>);
static_assert(std::constructible_from<array, const std::vector<yyjson::reader::const_object_ref>&>);
static_assert(std::constructible_from<array, std::initializer_list<yyjson::reader::const_value_ref>>);
static_assert(std::constructible_from<array, std::initializer_list<yyjson::reader::const_array_ref>>);
static_assert(std::constructible_from<array, std::initializer_list<yyjson::reader::const_object_ref>>);
// convert array-like type to array (with copy string tag)
static_assert(std::constructible_from<array, const std::vector<int>&, copy_string_t>);
static_assert(std::constructible_from<array, const std::list<int>&, copy_string_t>);
static_assert(std::constructible_from<array, const std::vector<std::list<int>>&, copy_string_t>);
static_assert(std::constructible_from<array, std::initializer_list<int>, copy_string_t>);
static_assert(std::constructible_from<array, std::initializer_list<value>, copy_string_t>);
static_assert(std::constructible_from<array, std::initializer_list<value_ref>, copy_string_t>);
static_assert(std::constructible_from<array, std::initializer_list<const_value_ref>, copy_string_t>);
static_assert(std::constructible_from<array, std::initializer_list<array>, copy_string_t>);
static_assert(std::constructible_from<array, std::initializer_list<array_ref>, copy_string_t>);
static_assert(std::constructible_from<array, std::initializer_list<const_array_ref>, copy_string_t>);