-
Notifications
You must be signed in to change notification settings - Fork 3
/
json.hpp
2224 lines (1999 loc) · 79.5 KB
/
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
#pragma once
#include "fly/concepts/concepts.hpp"
#include "fly/types/json/concepts.hpp"
#include "fly/types/json/detail/array_util.hpp"
#include "fly/types/json/detail/json_iterator.hpp"
#include "fly/types/json/detail/json_reverse_iterator.hpp"
#include "fly/types/json/json_exception.hpp"
#include "fly/types/json/types.hpp"
#include "fly/types/numeric/literals.hpp"
#include "fly/types/string/concepts.hpp"
#include "fly/types/string/format.hpp"
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <exception>
#include <functional>
#include <initializer_list>
#include <map>
#include <optional>
#include <string>
#include <type_traits>
#include <variant>
#include <vector>
// Helper macros to choose the correct string literal prefix to use for JSON string types.
#define FLY_JSON_CHR(ch) FLY_CHR(fly::json_char_type, ch)
#define FLY_JSON_STR(str) FLY_STR(fly::json_char_type, str)
#define FLY_JSON_ARR(arr) FLY_ARR(fly::json_char_type, arr)
namespace fly {
/**
* Class to represent JSON values defined by https://www.json.org.
*
* This class is designed to treat JSON as a first-class container, and to feel as easy to use as
* a Python dictionary.
*
* There are a myriad of user-friendly initializers to create a JSON value from any compatible type:
*
* A JSON string is a Unicode string. Internally, strings are stored with UTF-8 encoding.
* However, all methods that accept a JSON string will accept ASCII, UTF-8, UTF-16, and UTF-32
* encoded strings. All strings are validated for strict Unicode compliance and converted to
* UTF-8. Further, the provided string type may be an STL string, a null-terminated character
* array, or a string view. For example:
*
* fly::Json json = "This is an ASCII string";
* fly::Json json = u8"This is a UTF-8 string";
* fly::Json json = u"This is a UTF-16 string";
* fly::Json json = U"This is a UTF-32 string";
* fly::Json json = L"This is a wide string"; // UTF-16 on Windows, UTF-32 on Linux & macOS.
*
* std::string string = "This is an ASCII string";
* fly::Json json = string;
*
* std::u32string string = "This is a UTF-32 string";
* std::u32string_view view = string;
* fly::Json json = view;
*
* A JSON object may be created from a std::map, std::unordered_multimap, etc., as long as the
* map key is a JSON string (where any of the above Unicode encodings are valid). Further,
* initializer lists with mixed types are also valid. For example:
*
* std::map<std::string, int> map = {{"key1", 1}, {"key2", 2}};
* fly::Json json = map;
*
* std::unordered_map<std::u16string, int> map = {{u"key1", 1}, {u"key2", 2}};
* fly::Json json = map;
*
* fly::Json json = {{"key1", nullptr}, {u8"key2", L"value2"}, {U"key3", 123.89f}};
*
* A JSON array may be created from a std::vector, std::list, std::array, etc. Further,
* initializer lists with mixed types are also valid. For example:
*
* std::vector<std::string> array = {"value1", "value2"};
* fly::Json json = array;
*
* std::array<std::u8string, 2> array = {u8"value1", u8"value2"};
* fly::Json json = array;
*
* fly::Json json = {"value1", u8"value2", nullptr, 123.89f};
*
* A JSON boolean, number, or null value may be created from analogous C++ plain-old-data types.
* Internally, 64-bit integers are used for storing integer numbers and long doubles for
* floating-point numbers. The signedness of the 64-bit integer is the same as the integer from
* which the JSON value is created. For example:
*
* fly::Json json = true;
* fly::Json json = -12389;
* fly::Json json = 123.89f;
* fly::Json json = nullptr;
*
* A JSON value may be converted to any compatible C++ type. Attempting to convert a JSON value to
* an incompatible type is considered exceptional. Further, conversions must be explicit. To define
* conversion operators implicitly would introduce ambiguity in which conversion operator the
* compiler should choose. For example:
*
* fly::Json json = { 1, 2, 3, 4 };
* std::vector<int> vector(json); // Would not compile if conversions were implicit.
*
* Which JSON conversion operator should be called for the std::vector constructor? Conversions from
* a JSON value to std::vector and std::size_t are defined, creating ambiguity in which std::vector
* constructor would be called (copy constructor or count constructor), even though the std::size_t
* converter would actually throw an exception. Explicit conversion operators remove this ambiguity.
*
* Converting a JSON string to a string type permits the same Unicode flexibility as creating
* the JSON string. For example:
*
* fly::Json json = "This is an ASCII string";
* std::string string(json);
* std::u8string string(json);
* std::wstring string(json);
*
* A restriction is that while creating a JSON value from a character array is allowed,
* converting a JSON value to a character array is not allowed. There is no safe way to do the
* following without allocating memory that the caller must remember to free:
*
* fly::Json json = "string";
* char *string = json; // Will not compile.
*
* Converting other JSON types works similarly. Like JSON strings, the keys of the C++ type may
* be any compatible string type.
*
* fly::Json json = {{"key1", 1}, {u8"key2", "2"}};
* std::map<std::u32string, int> map(json); // map = {{U"key1", 1}, {U"key2", 2}}
*
* fly::Json json = {"value1", u8"value2", nullptr, 123.89f};
* std::vector<std::string> array(json); // array = {"value1", "value2", "null", "123.89"}
*
* fly::Json json = true;
* bool value(json);
*
* fly::Json json = 12389;
* std::uint16_t value(json);
*
* fly::Json json = -123.89f;
* float value(json);
*
* Some leniency is allowed for converting a JSON value to a type which differs from the type
* of the JSON value itself:
*
* JSON strings may be converted to numeric values if the string represents a number. For
* example, the string "12389" may be converted to an integer. The string "abc" may not.
*
* Numeric JSON types may be converted to a string type.
*
* All JSON types may be converted to a boolean. String, object, and array JSON values will
* convert based on whether the value is empty. JSON numbers will convert based on whether
* the value is non-zero. Null JSON values always convert to false.
*
* JSON numbers may be converted to any numeric type. For example, a floating-point JSON
* value may be converted to an integer.
*
* Lastly, this class defines the canonical interfaces of STL container types. This includes element
* accessor, iterator, modifier, and capacity/lookup operations.
*
* @author Timothy Flynn ([email protected])
* @version September 24, 2017
*/
class Json
{
public:
/**
* Aliases for canonical STL container member types.
*/
using value_type = Json;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using allocator_type = std::allocator<value_type>;
using reference = value_type &;
using const_reference = value_type const &;
using pointer = typename std::allocator_traits<allocator_type>::pointer;
using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
using iterator = detail::JsonIterator<Json>;
using const_iterator = detail::JsonIterator<Json const>;
using reverse_iterator = detail::JsonReverseIterator<iterator>;
using const_reverse_iterator = detail::JsonReverseIterator<const_iterator>;
/**
* Default constructor. Intializes the Json instance to a null value.
*/
Json() = default;
/**
* Null constructor. Intializes the Json instance to a null value.
*
* @param value The null value.
*/
Json(json_null_type value) noexcept;
/**
* String constructor. Intializes the Json instance to a string value. The SFINAE declaration
* allows construction of a string value from any string-like type (e.g. std::string, char8_t[],
* std::u16string_view).
*
* @tparam T The string-like type.
*
* @param value The string-like value.
*
* @throws JsonException If the string-like value is not valid.
*/
template <JsonStringLike T>
Json(T value) noexcept(false);
/**
* Object constructor. Intializes the Json instance to an object's values. The SFINAE
* declaration allows construction of an object value from any object-like type (e.g. std::map,
* std::multimap).
*
* @tparam T The object-like type.
*
* @param value The object-like value.
*
* @throws JsonException If an object key is not a valid string.
*/
template <JsonObject T>
Json(T value) noexcept(false);
/**
* Array constructor. Intializes the Json instance to an array's values. The SFINAE declaration
* allows construction of an array value from any array-like type (e.g. std::list, std::vector).
*
* @tparam T The array-like type.
*
* @param value The array-like value.
*
* @throws JsonException If an string-like value in the array is not valid.
*/
template <JsonArray T>
Json(T value) noexcept(false);
/**
* Boolean constructor. Intializes the Json instance to a boolean value. The SFINAE declaration
* forbids construction of a boolean value from any non-boolean type (e.g. int could be
* implicitly cast to bool).
*
* @tparam T The boolean type.
*
* @param value The boolean value.
*/
template <JsonBoolean T>
Json(T value) noexcept;
/**
* Signed integer constructor. Intializes the Json instance to a signed integer value. The
* SFINAE declaration allows construction of a signed integer value from any signed type (e.g.
* char, int, int64_t).
*
* @tparam T The signed type.
*
* @param value The signed value.
*/
template <JsonSignedInteger T>
Json(T value) noexcept;
/**
* Unsigned integer constructor. Intializes the Json instance to an unsigned integer value. The
* SFINAE declaration allows construction of an unsigned integer value from any unsigned type
* (e.g. unsigned char, unsigned int, uint64_t).
*
* @tparam T The unsigned type.
*
* @param value The unsigned value.
*/
template <JsonUnsignedInteger T>
Json(T value) noexcept;
/**
* Floating-point constructor. Intializes the Json instance to a floating-point value. The
* SFINAE declaration allows construction of a floating-point value from any floating-point type
* (e.g. float, double).
*
* @tparam T The floating-point type.
*
* @param value The floating-point value.
*/
template <JsonFloatingPoint T>
Json(T value) noexcept;
/**
* Copy constructor. Intializes the Json instance with the type and value of another Json
* instance.
*
* @param json The Json instance to copy.
*/
Json(const_reference json) noexcept;
/**
* Move constructor. Intializes the Json instance with the type and value of another Json
* instance. The other Json instance is set to a null value.
*
* @param json The Json instance to move.
*/
Json(Json &&json) noexcept;
/**
* Initializer list constructor. Intializes the Json instance with an initializer list. Creates
* either an object or an array instance. If all values in the initializer list are object-like
* (see IsObjectLike()), then the Json instance is created as an object. Otherwise, it is
* created as an array.
*
* @param initializer The initializer list.
*/
Json(std::initializer_list<Json> initializer) noexcept;
/**
* Copy assignment operator. Intializes the Json instance with the type and value of another
* Json instance, using the copy-and-swap idiom.
*
* @param json The Json instance to copy-and-swap.
*
* @return A reference to this Json instance.
*/
reference operator=(Json json) noexcept;
/**
* Serialize the Json instance to a string.
*
* @return The serialized Json instance.
*/
json_string_type serialize() const;
/**
* @return True if the Json instance is null.
*/
bool is_null() const;
/**
* @return True if the Json instance is a string.
*/
bool is_string() const;
/**
* @return True if the Json instance is an object.
*/
bool is_object() const;
/**
* Determine if the Json instance is object-like. This is mostly useful for constructing a Json
* instance from an initializer list. If this instance is an array with two elements, and the
* first element is a string, then this instance is object-like.
*
* @return True if the Json instance is object-like.
*/
bool is_object_like() const;
/**
* @return True if the Json instance is an array.
*/
bool is_array() const;
/**
* @return True if the Json instance is a boolean.
*/
bool is_boolean() const;
/**
* @return True if the Json instance is a signed integer.
*/
bool is_signed_integer() const;
/**
* @return True if the Json instance is an unsigned integer.
*/
bool is_unsigned_integer() const;
/**
* @return True if the Json instance is a float.
*/
bool is_float() const;
//==============================================================================================
//
// Conversion operators
//
//==============================================================================================
/**
* Null conversion operator. Converts the Json instance to a null type.
*
* @return The Json instance as a number.
*
* @throws JsonException If the Json instance is not null.
*/
explicit operator json_null_type() const noexcept(false);
/**
* String conversion operator. Converts the Json instance to a string. The SFINAE declaration
* allows conversion to any string type (e.g. std::string, std::u8string). Also allows for
* converting from a numeric type (e.g. 12389) to a string type.
*
* Note that although a Json instance can be constructed from a character array, it is not
* allowed to directly convert a Json instance into a character array.
*
* @tparam T The string type.
*
* @return The Json instance as a string.
*
* @throws JsonException If the Json instance is not a string, or the stored value could not be
* converted to the target string type.
*/
template <JsonString T>
explicit operator T() const &noexcept(false);
/**
* String move-conversion operator. If the Json instance is a string, transfers ownership of
* the stored value to the caller. The Json instance is set to a null value.
*
* @return The Json instance's stored string value.
*
* @throws JsonException If the Json instance is not a string.
*/
explicit operator json_string_type() &&noexcept(false);
/**
* Object conversion operator. Converts the Json instance to an object. The SFINAE declaration
* allows conversion to any object-like type (e.g. std::map, std::multimap).
*
* @tparam T The object-like type.
*
* @return The Json instance as the object-like type.
*
* @throws JsonException If the Json instance is not an object, or a stored element could not be
* converted to the target object's value type.
*/
template <JsonObject T>
explicit operator T() const &noexcept(false);
/**
* Object move-conversion operator. If the Json instance is an object, transfers ownership of
* the stored value to the caller. The Json instance is set to a null value.
*
* @return The Json instance's stored object value.
*
* @throws JsonException If the Json instance is not an object.
*/
explicit operator json_object_type() &&noexcept(false);
/**
* Array conversion operator. Converts the Json instance to an array. The SFINAE declaration
* allows conversion to any array-like type (e.g. std::list, std::vector). This excludes
* std::array, which due to being an aggregate type, has its own explicit conversion operator.
*
* @tparam T The array-like type.
*
* @return The Json instance as the array-like type.
*
* @throws JsonException If the Json instance is not an array, or a stored element could not be
* converted to the target array's value type.
*/
template <JsonArray T>
explicit operator T() const &noexcept(false);
/**
* Array move-conversion operator. If the Json instance is an array, transfers ownership of
* the stored value to the caller. The Json instance is set to a null value.
*
* @return The Json instance's stored array value.
*
* @throws JsonException If the Json instance is not an array.
*/
explicit operator json_array_type() &&noexcept(false);
/**
* Array conversion operator. Converts the Json instance to a std::array. If the Json instance
* has more values than the std::array can hold, the values are dropped. If the Json instance
* has less values than the std::array can hold, the remainder are value-initialized.
*
* @tparam T The std::array value type.
* @tparam N The std::array size.
*
* @return The Json instance as a std::array.
*
* @throws JsonException If the Json instance is not an array, or a stored element could not be
* converted to the target array's value type.
*/
template <typename T, std::size_t N>
explicit operator std::array<T, N>() const noexcept(false);
/**
* Boolean conversion operator. Converts the Json instance to a boolean. For strings, objects,
* and arrays, returns true if the value is non-empty. For signed integers, unsigned integers,
* and floats, returns true if the value is non-zero. For booleans, returns the boolean value.
* For null, returns false.
*
* @tparam T The boolean type.
*
* @return The Json instance as a boolean.
*/
template <JsonBoolean T>
explicit operator T() const noexcept;
/**
* Numeric conversion operator. Converts the Json instance to a numeric type. The SFINAE
* declaration allows conversion to any numeric type type (e.g. char, uint64_t, float) from
* the Json instance. Allows for converting between signed integers, unsigned integers, and
* floats. Also allows for converting from a numeric-like string (e.g. "12389") to a numeric
* type.
*
* @tparam T The numeric type.
*
* @return The Json instance as the numeric type.
*
* @throws JsonException If the Json instance is not numeric, or the stored value could not be
* converted to the target numeric type.
*/
template <JsonNumber T>
explicit operator T() const noexcept(false);
//==============================================================================================
//
// Element accessors
//
//==============================================================================================
/**
* Object read-only accessor. The SFINAE declaration allows lookups with any string-like type
* (e.g. std::string, char8_t[], std::u16string_view).
*
* If the Json instance is an object, perform a lookup on the object with a key value.
*
* @tparam T The string-like key type.
*
* @param key The key value to lookup.
*
* @return A reference to the Json instance at the key value.
*
* @throws JsonException If the Json instance is not an object, or the key value does not exist,
* or the key value is invalid.
*/
template <JsonStringLike T>
reference at(T key);
/**
* Object read-only accessor. The SFINAE declaration allows lookups with any string-like type
* (e.g. std::string, char8_t[], std::u16string_view).
*
* If the Json instance is an object, perform a lookup on the object with a key value.
*
* @tparam T The string-like key type.
*
* @param key The key value to lookup.
*
* @return A reference to the Json instance at the key value.
*
* @throws JsonException If the Json instance is not an object, or the key value does not exist,
* or the key value is invalid.
*/
template <JsonStringLike T>
const_reference at(T key) const;
/**
* Array read-only accessor.
*
* If the Json instance is an array, perform a lookup on the array with an index.
*
* @param index The index to lookup.
*
* @return A reference to the Json instance at the index.
*
* @throws JsonException If the Json instance is not an array or the index does not exist.
*/
reference at(size_type index);
/**
* Array read-only accessor.
*
* If the Json instance is an array, perform a lookup on the array with an index.
*
* @param index The index to lookup.
*
* @return A reference to the Json instance at the index.
*
* @throws JsonException If the Json instance is not an array or the index does not exist.
*/
const_reference at(size_type index) const;
/**
* Object access operator. The SFINAE declaration allows lookups with any string-like type
* (e.g. std::string, char8_t[], std::u16string_view).
*
* If the Json instance is an object, perform a lookup on the object with a key value. If the
* key value is not found, a null Json instance will be created for that key.
*
* If the Json instance is null, it is first converted to an object.
*
* @tparam T The string-like key type.
*
* @param key The key value to lookup.
*
* @return A reference to the Json instance at the key value.
*
* @throws JsonException If the Json instance is neither an object nor null, or the key value is
* invalid.
*/
template <JsonStringLike T>
reference operator[](T key);
/**
* Object read-only access operator. The SFINAE declaration allows lookups with any string-like
* type (e.g. std::string, char8_t[], std::u16string_view).
*
* If the Json instance is an object, perform a lookup on the object with a key value.
*
* @param key The key value to lookup.
*
* @return A reference to the Json instance at the key value.
*
* @throws JsonException If the Json instance is not an object, or the key value does not exist,
* or the key value is invalid.
*/
template <JsonStringLike T>
const_reference operator[](T key) const;
/**
* Array access operator.
*
* If the Json instance is an array, perform a lookup on the array with an index. If the index
* is not found, the array is filled with null values up to and including the index.
*
* If the Json instance is null, it is first converted to an array.
*
* @param index The index to lookup.
*
* @return A reference to the Json instance at the index.
*
* @throws JsonException If the Json instance is neither an array nor null.
*/
reference operator[](size_type index);
/**
* Array read-only access operator.
*
* If the Json instance is an array, perform a lookup on the array with an index.
*
* @param index The index to lookup.
*
* @return A reference to the Json instance at the index.
*
* @throws JsonException If the Json instance is not an array or the index does not exist.
*/
const_reference operator[](size_type index) const;
/**
* Obtain a reference to the first element in the Json instance. Only valid if the Json instance
* is an object or an array.
*
* For JSON objects, the returned reference will be to the value part of the front element's
* key-value pair.
*
* @return A reference to the first element.
*
* @throws JsonException If the Json instance is not an object or array, or if the Json instance
* is empty.
*/
reference front();
/**
* Obtain a reference to the first element in the Json instance. Only valid if the Json instance
* is an object or an array.
*
* For JSON objects, the returned reference will be to the value part of the front element's
* key-value pair.
*
* @return A reference to the first element.
*
* @throws JsonException If the Json instance is not an object or array, or if the Json instance
* is empty.
*/
const_reference front() const;
/**
* Obtain a reference to the last element in the Json instance. Only valid if the Json instance
* is an object or an array.
*
* For JSON objects, the returned reference will be to the value part of the back element's
* key-value pair.
*
* @return A reference to the last element.
*
* @throws JsonException If the Json instance is not an object or array, or if the Json instance
* is empty.
*/
reference back();
/**
* Obtain a reference to the last element in the Json instance. Only valid if the Json instance
* is an object or an array.
*
* For JSON objects, the returned reference will be to the value part of the back element's
* key-value pair.
*
* @return A reference to the last element.
*
* @throws JsonException If the Json instance is not an object or array, or if the Json instance
* is empty.
*/
const_reference back() const;
//==============================================================================================
//
// Iterators
//
//==============================================================================================
/**
* Retrieve an iterator to the beginning of the Json instance.
*
* @return The retrieved iterator.
*
* @throws JsonException If the Json instance is not an object or array.
*/
iterator begin();
/**
* Retrieve a constant iterator to the beginning of the Json instance.
*
* @return The retrieved iterator.
*
* @throws JsonException If the Json instance is not an object or array.
*/
const_iterator begin() const;
/**
* Retrieve a constant iterator to the beginning of the Json instance.
*
* @return The retrieved iterator.
*
* @throws JsonException If the Json instance is not an object or array.
*/
const_iterator cbegin() const;
/**
* Retrieve an iterator to the end of the Json instance.
*
* @return The retrieved iterator.
*
* @throws JsonException If the Json instance is not an object or array.
*/
iterator end();
/**
* Retrieve a constant iterator to the end of the Json instance.
*
* @return The retrieved iterator.
*
* @throws JsonException If the Json instance is not an object or array.
*/
const_iterator end() const;
/**
* Retrieve a constant iterator to the end of the Json instance.
*
* @return The retrieved iterator.
*
* @throws JsonException If the Json instance is not an object or array.
*/
const_iterator cend() const;
/**
* Retrieve a reverse iterator to the beginning of the reversed Json instance.
*
* @return The retrieved iterator.
*
* @throws JsonException If the Json instance is not an object or array.
*/
reverse_iterator rbegin();
/**
* Retrieve a constant reverse iterator to the beginning of the reversed Json instance.
*
* @return The retrieved iterator.
*
* @throws JsonException If the Json instance is not an object or array.
*/
const_reverse_iterator rbegin() const;
/**
* Retrieve a constant reverse iterator to the beginning of the reversed Json instance.
*
* @return The retrieved iterator.
*
* @throws JsonException If the Json instance is not an object or array.
*/
const_reverse_iterator crbegin() const;
/**
* Retrieve a reverse iterator to the end of the reversed Json instance.
*
* @return The retrieved iterator.
*
* @throws JsonException If the Json instance is not an object or array.
*/
reverse_iterator rend();
/**
* Retrieve a constant reverse iterator to the end of the reversed Json instance.
*
* @return The retrieved iterator.
*
* @throws JsonException If the Json instance is not an object or array.
*/
const_reverse_iterator rend() const;
/**
* Retrieve a constant reverse iterator to the end of the reversed Json instance.
*
* @return The retrieved iterator.
*
* @throws JsonException If the Json instance is not an object or array.
*/
const_reverse_iterator crend() const;
//==============================================================================================
//
// Capacity
//
//==============================================================================================
/**
* Check if the Json instance contains zero elements.
*
* If the Json instance is null, returns true.
*
* If the Json instance is a string, object, or array, returns whether the stored container is
* empty.
*
* If the Json instance is a boolean or numeric, returns false.
*
* @return True if the instance is empty.
*/
bool empty() const;
/**
* Get the number of elements in the Json instance.
*
* If the Json instance is null, returns 0.
*
* If the Json instance is a string, returns the length of the string.
*
* If the Json instance is an object or array, returns the number of elements stored in the
* object or array.
*
* If the Json instance is a boolean or numeric, returns 1.
*
* @return The size of the Json instance.
*/
size_type size() const;
/**
* Resize the Json instance to contain the provided number of elements. Only valid if the Json
* instance is a string or array.
*
* @param size The new size of the Json instance.
*
* @throws JsonException If the Json instance is not a string or array.
*/
void resize(size_type size);
/**
* Get the number of elements that the Json instance has currently allocated space for.
*
* If the Json instance is null, returns 0.
*
* If the Json instance is a string or array, returns the number of elements allocated for the
* string or array.
*
* If the Json instance is an object, returns the number of elements stored in the object
* (effectively the same as invoking Json::size).
*
* If the Json instance is a boolean or numeric, returns 1.
*
* @return The capacity of the Json instance.
*/
size_type capacity() const;
/**
* Increase the capacity of the Json instance to a value that's greater or equal to the provided
* capacity. Only valid if the Json instance is a string or array.
*
* @param capacity The new capacity of the Json instance.
*
* @throws JsonException If the Json instance is not a string or array.
*/
void reserve(size_type capacity);
//==============================================================================================
//
// Modifiers
//
//==============================================================================================
/**
* Clear the contents of the Json instance.
*
* If the Json instance is an object, array, or string, clears the stored container.
*
* If the Json instance is a boolean, sets to false.
*
* If the Json instance is numeric, sets to zero.
*/
void clear();
/**
* Insert a copy of a key-value pair into the Json instance. Only valid if the Json instance is
* an object. The SFINAE declaration allows inserting a value with a key of any string-like type
* (e.g. std::string, char8_t[], std::u16string_view).
*
* @tparam Key The string-like type of the inserted value's key.
*
* @param key The key of the value to insert.
* @param value The value to insert at the given key.
*
* @return An iterator-boolean pair. The boolean indicates whether the insertion was successful.
* If so, the iterator is that of the inserted element. If not, the iterator points to
* the element which prevented the insertion.
*
* @throws JsonException If the Json instance is not an object or the key value is invalid.
*/
template <JsonStringLike Key>
std::pair<iterator, bool> insert(Key key, Json const &value);
/**
* Insert a moved key-value pair into the Json instance. Only valid if the Json instance is an
* object. The SFINAE declaration allows inserting a value with a key of any string-like type
* (e.g. std::string, char8_t[], std::u16string_view).
*
* @tparam Key The string-like type of the inserted value's key.
*
* @param key The key of the value to insert.
* @param value The value to insert at the given key.
*
* @return An iterator-boolean pair. The boolean indicates whether the insertion was successful.
* If so, the iterator is that of the inserted element. If not, the iterator points to
* the element which prevented the insertion.
*
* @throws JsonException If the Json instance is not an object or the key value is invalid.
*/
template <JsonStringLike Key>
std::pair<iterator, bool> insert(Key key, Json &&value);
/**
* Insert all values into the Json instance in the range [first, last). Only valid if the Json
* instance is an object.
*
* @param first The beginning of the range of values to insert.
* @param last The end of the range of values to insert.
*
* @throws JsonException If the Json instance is not an object, the key value is invalid, the
* provided iterators are not for the same Json instance, or the provided iterators are
* for non-object Json instances.
*/
void insert(const_iterator first, const_iterator last);
/**
* Insert a copy of a Json value into the Json instance before the provided position. Only valid
* if the Json instance is an array.
*
* @param position The iterator position before which the value should be inserted.
* @param value The Json value to insert.
*
* @return An iterator pointed at the inserted element.
*
* @throws JsonException If the Json instance is not an array or the provided position is not
* for this Json instance.
*/
iterator insert(const_iterator position, Json const &value);
/**
* Insert a moved Json value into the Json instance before the provided position. Only valid if
* if the Json instance is an array.
*
* @param position The iterator position before which the value should be inserted.
* @param value The Json value to insert.
*
* @return An iterator pointed at the inserted element.
*
* @throws JsonException If the Json instance is not an array or the provided position is not
* for this Json instance.
*/
iterator insert(const_iterator position, Json &&value);
/**
* Insert a number of copies of a Json value into the Json instance before the provided
* position. Only valid if the Json instance is an array.
*
* @param position The iterator position before which the value should be inserted.
* @param count The number of copies to insert.
* @param value The Json value to insert.
*
* @return An iterator pointed at the first element inserted.
*
* @throws JsonException If the Json instance is not an array or the provided position is not
* for this Json instance.
*/
iterator insert(const_iterator position, size_type count, Json const &value);
/**
* Insert all values into the Json instance in the range [first, last) before the provided