/
read.hpp
2693 lines (2448 loc) · 104 KB
/
read.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
// Glaze Library
// For the license information refer to glaze.hpp
#pragma once
#include <charconv>
#include <climits>
#include <cwchar>
#include <filesystem>
#include <iterator>
#include <ranges>
#include <type_traits>
#include "glaze/core/common.hpp"
#include "glaze/core/opts.hpp"
#include "glaze/core/read.hpp"
#include "glaze/file/file_ops.hpp"
#include "glaze/json/json_t.hpp"
#include "glaze/json/skip.hpp"
#include "glaze/reflection/reflect.hpp"
#include "glaze/util/for_each.hpp"
#include "glaze/util/strod.hpp"
#include "glaze/util/type_traits.hpp"
#include "glaze/util/variant.hpp"
namespace glz
{
// forward declare from json/quoted.hpp to avoid circular include
template <class T>
struct quoted_t;
namespace detail
{
// Unless we can mutate the input buffer we need somewhere to store escaped strings for key lookup, etc.
// We don't put this in the context because we don't want to continually reallocate.
GLZ_ALWAYS_INLINE std::string& string_buffer() noexcept
{
static thread_local std::string buffer(256, '\0');
return buffer;
}
// The string_buffer() often gets resized, but we want our decode buffer to
// only ever grow on resizing. So, we make it its own buffer.
GLZ_ALWAYS_INLINE std::string& string_decode_buffer() noexcept
{
static thread_local std::string buffer(512, '\0');
return buffer;
}
// We use an error buffer to avoid multiple allocations in the case that errors occur multiple times.
GLZ_ALWAYS_INLINE std::string& error_buffer() noexcept
{
static thread_local std::string buffer(256, '\0');
return buffer;
}
template <>
struct read<json>
{
template <auto Opts, class T, is_context Ctx, class It0, class It1>
GLZ_ALWAYS_INLINE static void op(T&& value, Ctx&& ctx, It0&& it, It1&& end) noexcept
{
if constexpr (std::is_const_v<std::remove_reference_t<T>>) {
if constexpr (Opts.error_on_const_read) {
ctx.error = error_code::attempt_const_read;
}
else {
// do not read anything into the const value
skip_value<Opts>(std::forward<Ctx>(ctx), std::forward<It0>(it), std::forward<It1>(end));
}
}
else {
using V = std::remove_cvref_t<T>;
from_json<V>::template op<Opts>(std::forward<T>(value), std::forward<Ctx>(ctx), std::forward<It0>(it),
std::forward<It1>(end));
}
}
// This unknown key handler should not be given unescaped keys, that is for the user to handle.
template <auto Opts, class T, is_context Ctx, class It0, class It1>
GLZ_ALWAYS_INLINE static void handle_unknown(const sv& key, T&& value, Ctx&& ctx, It0&& it, It1&& end) noexcept
{
using ValueType = std::decay_t<decltype(value)>;
if constexpr (detail::has_unknown_reader<ValueType>) {
constexpr auto& reader = meta_unknown_read_v<ValueType>;
using ReaderType = meta_unknown_read_t<ValueType>;
if constexpr (std::is_member_object_pointer_v<ReaderType>) {
using MemberType = typename member_value<ReaderType>::type;
if constexpr (detail::map_subscriptable<MemberType>) {
read<json>::op<Opts>((value.*reader)[key], ctx, it, end);
}
else {
static_assert(false_v<T>, "target must have subscript operator");
}
}
else if constexpr (std::is_member_function_pointer_v<ReaderType>) {
using ReturnType = typename return_type<ReaderType>::type;
if constexpr (std::is_void_v<ReturnType>) {
using TupleType = typename inputs_as_tuple<ReaderType>::type;
if constexpr (glz::tuple_size_v<TupleType> == 2) {
std::decay_t<glz::tuple_element_t<1, TupleType>> input{};
read<json>::op<Opts>(input, ctx, it, end);
if (bool(ctx.error)) [[unlikely]]
return;
(value.*reader)(key, input);
}
else {
static_assert(false_v<T>, "method must have 2 args");
}
}
else {
static_assert(false_v<T>, "method must have void return");
}
}
else {
static_assert(false_v<T>, "unknown_read type not handled");
}
}
else {
skip_value<Opts>(ctx, it, end);
}
}
};
template <class T>
requires(glaze_value_t<T> && !specialized_with_custom_read<T>)
struct from_json<T>
{
template <auto Opts, class Value, is_context Ctx, class It0, class It1>
GLZ_ALWAYS_INLINE static void op(Value&& value, Ctx&& ctx, It0&& it, It1&& end) noexcept
{
using V = std::decay_t<decltype(get_member(std::declval<Value>(), meta_wrapper_v<T>))>;
from_json<V>::template op<Opts>(get_member(std::forward<Value>(value), meta_wrapper_v<T>),
std::forward<Ctx>(ctx), std::forward<It0>(it), std::forward<It1>(end));
}
};
template <is_member_function_pointer T>
struct from_json<T>
{
template <auto Opts>
GLZ_ALWAYS_INLINE static void op(auto&&, is_context auto&& ctx, auto&&...) noexcept
{
ctx.error = error_code::attempt_member_func_read;
}
};
template <is_bitset T>
struct from_json<T>
{
template <auto Opts>
GLZ_ALWAYS_INLINE static void op(auto&& value, is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
GLZ_MATCH_QUOTE;
const auto n = value.size();
for (size_t i = 1; it < end; ++i, ++it) {
if (*it == '"') {
++it;
return;
}
if (i > n) {
ctx.error = error_code::exceeded_static_array_size;
return;
}
if (*it == '0') {
value[n - i] = 0;
}
else if (*it == '1') {
value[n - i] = 1;
}
else [[unlikely]] {
ctx.error = error_code::syntax_error;
return;
}
}
ctx.error = error_code::expected_quote;
}
};
template <>
struct from_json<skip>
{
template <auto Opts>
GLZ_ALWAYS_INLINE static void op(auto&&, is_context auto&& ctx, auto&&... args) noexcept
{
skip_value<Opts>(ctx, args...);
}
};
template <is_reference_wrapper T>
struct from_json<T>
{
template <auto Opts, class... Args>
GLZ_ALWAYS_INLINE static void op(auto&& value, Args&&... args) noexcept
{
using V = std::decay_t<decltype(value.get())>;
from_json<V>::template op<Opts>(value.get(), std::forward<Args>(args)...);
}
};
template <>
struct from_json<hidden>
{
template <auto Opts>
GLZ_ALWAYS_INLINE static void op(auto&&, is_context auto&& ctx, auto&&...) noexcept
{
ctx.error = error_code::attempt_read_hidden;
}
};
template <complex_t T>
struct from_json<T>
{
template <auto Options>
GLZ_ALWAYS_INLINE static void op(auto&& v, is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
constexpr auto Opts = ws_handled_off<Options>();
if constexpr (!Options.ws_handled) {
GLZ_SKIP_WS;
}
match<'['>(ctx, it);
if (bool(ctx.error)) [[unlikely]]
return;
auto* ptr = reinterpret_cast<typename T::value_type*>(&v);
static_assert(sizeof(T) == sizeof(typename T::value_type) * 2);
read<json>::op<Opts>(ptr[0], ctx, it, end);
if (bool(ctx.error)) [[unlikely]]
return;
GLZ_SKIP_WS;
GLZ_MATCH_COMMA;
read<json>::op<Opts>(ptr[1], ctx, it, end);
if (bool(ctx.error)) [[unlikely]]
return;
GLZ_SKIP_WS;
match<']'>(ctx, it, end);
}
};
template <always_null_t T>
struct from_json<T>
{
template <auto Opts>
GLZ_ALWAYS_INLINE static void op(auto&&, is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
if constexpr (!Opts.ws_handled) {
GLZ_SKIP_WS;
}
match<"null", Opts>(ctx, it, end);
}
};
template <bool_t T>
struct from_json<T>
{
template <auto Opts>
GLZ_ALWAYS_INLINE static void op(bool_t auto&& value, is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
if constexpr (Opts.quoted_num) {
GLZ_SKIP_WS;
GLZ_MATCH_QUOTE;
}
if constexpr (!Opts.ws_handled) {
GLZ_SKIP_WS;
}
if (size_t(end - it) < 4) [[unlikely]] {
ctx.error = error_code::expected_true_or_false;
return;
}
uint64_t c{};
// Note that because our buffer must be null terminated, we can read one more index without checking:
std::memcpy(&c, it, 5);
constexpr uint64_t u_true = 0b00000000'00000000'00000000'00000000'01100101'01110101'01110010'01110100;
constexpr uint64_t u_false = 0b00000000'00000000'00000000'01100101'01110011'01101100'01100001'01100110;
// We have to wipe the 5th character for true testing
if ((c & 0xFF'FF'FF'00'FF'FF'FF'FF) == u_true) {
value = true;
it += 4;
}
else {
if (c != u_false) [[unlikely]] {
ctx.error = error_code::expected_true_or_false;
return;
}
value = false;
it += 5;
}
if constexpr (Opts.quoted_num) {
GLZ_MATCH_QUOTE;
}
}
};
template <num_t T>
struct from_json<T>
{
template <auto Opts, class It>
GLZ_ALWAYS_INLINE static void op(auto&& value, is_context auto&& ctx, It&& it, auto&& end) noexcept
{
if constexpr (Opts.quoted_num) {
GLZ_SKIP_WS;
GLZ_MATCH_QUOTE;
}
if constexpr (!Opts.ws_handled) {
GLZ_SKIP_WS;
}
using V = std::decay_t<decltype(value)>;
if constexpr (int_t<V>) {
static constexpr auto maximum = uint64_t((std::numeric_limits<V>::max)());
if constexpr (std::is_unsigned_v<V>) {
if constexpr (std::same_as<V, uint64_t>) {
if (*it == '-') [[unlikely]] {
ctx.error = error_code::parse_number_failure;
return;
}
static_assert(sizeof(*it) == sizeof(char));
const char* cur = reinterpret_cast<const char*>(it);
const char* beg = cur;
if constexpr (std::is_volatile_v<decltype(value)>) {
// Hardware may interact with value changes, so we parse into a temporary and assign in one
// place
uint64_t i{};
auto s = parse_int<uint64_t, Opts.force_conformance>(i, cur);
if (!s) [[unlikely]] {
ctx.error = error_code::parse_number_failure;
return;
}
value = i;
}
else {
auto s = parse_int<decay_keep_volatile_t<decltype(value)>, Opts.force_conformance>(value, cur);
if (!s) [[unlikely]] {
ctx.error = error_code::parse_number_failure;
return;
}
}
it += (cur - beg);
}
else {
uint64_t i{};
if (*it == '-') [[unlikely]] {
ctx.error = error_code::parse_number_failure;
return;
}
static_assert(sizeof(*it) == sizeof(char));
const char* cur = reinterpret_cast<const char*>(it);
const char* beg = cur;
auto s = parse_int<std::decay_t<decltype(i)>, Opts.force_conformance>(i, cur);
if (!s) [[unlikely]] {
ctx.error = error_code::parse_number_failure;
return;
}
if (i > maximum) [[unlikely]] {
ctx.error = error_code::parse_number_failure;
return;
}
value = V(i);
it += (cur - beg);
}
}
else {
uint64_t i{};
int sign = 1;
if (*it == '-') {
sign = -1;
++it;
}
static_assert(sizeof(*it) == sizeof(char));
const char* cur = reinterpret_cast<const char*>(it);
const char* beg = cur;
auto s = parse_int<decay_keep_volatile_t<decltype(i)>, Opts.force_conformance>(i, cur);
if (!s) [[unlikely]] {
ctx.error = error_code::parse_number_failure;
return;
}
if (sign == -1) {
static constexpr auto min_abs = uint64_t((std::numeric_limits<V>::max)()) + 1;
if (i > min_abs) [[unlikely]] {
ctx.error = error_code::parse_number_failure;
return;
}
value = V(sign * i);
}
else {
if (i > maximum) [[unlikely]] {
ctx.error = error_code::parse_number_failure;
return;
}
value = V(i);
}
it += (cur - beg);
}
}
else {
if constexpr (is_float128<V>) {
auto [ptr, ec] = std::from_chars(it, end, value);
if (ec != std::errc()) {
ctx.error = error_code::parse_number_failure;
return;
}
it = ptr;
}
else {
if constexpr (std::is_volatile_v<decltype(value)>) {
// Hardware may interact with value changes, so we parse into a temporary and assign in one place
V temp;
auto s = parse_float<V, Opts.force_conformance>(value, it);
if (!s) [[unlikely]] {
ctx.error = error_code::parse_number_failure;
return;
}
value = temp;
}
else {
auto s = parse_float<V, Opts.force_conformance>(value, it);
if (!s) [[unlikely]] {
ctx.error = error_code::parse_number_failure;
return;
}
}
}
}
if constexpr (Opts.quoted_num) {
GLZ_MATCH_QUOTE;
}
}
};
template <string_t T>
struct from_json<T>
{
template <auto Opts, class It, class End>
requires(Opts.is_padded)
GLZ_ALWAYS_INLINE static void op(auto& value, is_context auto&& ctx, It&& it, End&& end) noexcept
{
if constexpr (Opts.number) {
auto start = it;
skip_number<Opts>(ctx, it, end);
if (bool(ctx.error)) [[unlikely]] {
return;
}
value.append(start, size_t(it - start));
}
else {
if constexpr (!Opts.opening_handled) {
if constexpr (!Opts.ws_handled) {
GLZ_SKIP_WS;
}
GLZ_MATCH_QUOTE;
}
if constexpr (not Opts.raw_string) {
auto& temp = string_decode_buffer();
auto* p = temp.data();
auto* p_end = p + temp.size() - padding_bytes;
while (true) {
if (p >= p_end) [[unlikely]] {
// the rare case of running out of temp buffer
const auto distance = size_t(p - temp.data());
temp.resize(temp.size() * 2);
p = temp.data() + distance; // reset p from new memory
p_end = temp.data() + temp.size() - padding_bytes;
}
std::memcpy(p, it, 8);
uint64_t swar;
std::memcpy(&swar, p, 8);
// auto next = has_quote(swar) | has_escape(swar) | is_less_32(swar);
// We use this optimized code for the sake of GCC and MSVC
uint64_t next = 0x2222222222222222ull ^ swar;
uint64_t a = 0x5C5C5C5C5C5C5C5Cull ^ swar;
uint64_t b = 0xE0E0E0E0E0E0E0E0ull & swar;
constexpr uint64_t c = 0xFEFEFEFEFEFEFEFFull;
next += c;
a += c;
a |= next;
b += c;
b |= a;
next = 0x8080808080808080ull & (~swar);
next &= b;
if (next) {
next = countr_zero(next) >> 3;
it += next;
if (*it == '"') {
value.assign(temp.data(), size_t((p + next) - temp.data()));
++it;
return;
}
if ((*it & 0b11100000) == 0) [[unlikely]] {
ctx.error = error_code::syntax_error;
return;
}
++it; // skip the escape
if (*it == 'u') {
++it;
p += next;
if (!handle_unicode_code_point(it, p)) [[unlikely]] {
ctx.error = error_code::unicode_escape_conversion_failure;
return;
}
}
else {
p += next;
*p = char_unescape_table[*it];
if (*p == 0) [[unlikely]] {
ctx.error = error_code::invalid_escape;
return;
}
++p;
++it;
}
}
else {
it += 8;
p += 8;
}
}
}
else {
// raw_string
auto start = it;
skip_string_view<Opts>(ctx, it, end);
if (bool(ctx.error)) [[unlikely]]
return;
value.assign(start, size_t(it - start));
++it;
}
}
}
template <auto Opts, class It, class End>
requires(not Opts.is_padded)
GLZ_ALWAYS_INLINE static void op(auto& value, is_context auto&& ctx, It&& it, End&& end) noexcept
{
if constexpr (Opts.number) {
auto start = it;
skip_number<Opts>(ctx, it, end);
if (bool(ctx.error)) [[unlikely]] {
return;
}
value.append(start, size_t(it - start));
}
else {
if constexpr (!Opts.opening_handled) {
if constexpr (!Opts.ws_handled) {
GLZ_SKIP_WS;
}
GLZ_MATCH_QUOTE;
}
if constexpr (not Opts.raw_string) {
// A surrogate pair unicode code point may require 12 characters
// So we need to have this much space available in our read buffer
const auto end12 = end - 12;
auto& temp = string_decode_buffer();
auto* p = temp.data();
auto* p_end = p + temp.size() - padding_bytes;
while (true) {
if (p >= p_end) [[unlikely]] {
// the rare case of running out of temp buffer
const auto distance = size_t(p - temp.data());
temp.resize(temp.size() * 2);
p = temp.data() + distance; // reset p from new memory
p_end = temp.data() + temp.size() - padding_bytes;
}
if (it > end12) {
break;
}
std::memcpy(p, it, 8);
uint64_t swar;
std::memcpy(&swar, p, 8);
// auto next = has_quote(swar) | has_escape(swar) | is_less_32(swar);
// We use this optimized code for the sake of GCC and MSVC
uint64_t next = 0x2222222222222222ull ^ swar;
uint64_t a = 0x5C5C5C5C5C5C5C5Cull ^ swar;
uint64_t b = 0xE0E0E0E0E0E0E0E0ull & swar;
constexpr uint64_t c = 0xFEFEFEFEFEFEFEFFull;
next += c;
a += c;
a |= next;
b += c;
b |= a;
next = 0x8080808080808080ull & (~swar);
next &= b;
if (next) {
next = countr_zero(next) >> 3;
it += next;
if (*it == '"') {
value.assign(temp.data(), size_t((p + next) - temp.data()));
++it;
return;
}
if ((*it & 0b11100000) == 0) [[unlikely]] {
ctx.error = error_code::syntax_error;
return;
}
++it; // skip the escape
if (*it == 'u') {
++it;
p += next;
if (!handle_unicode_code_point(it, p)) [[unlikely]] {
ctx.error = error_code::unicode_escape_conversion_failure;
return;
}
}
else {
p += next;
*p = char_unescape_table[*it];
if (*p == 0) [[unlikely]] {
ctx.error = error_code::invalid_escape;
return;
}
++p;
++it;
}
}
else {
it += 8;
p += 8;
}
}
// we know we won't run out of space in our temp buffer because we subtract padding_bytes
while (it[-1] == '\\') [[unlikely]] {
// if we ended on an escape character then we need to rewind
// because we lost our context
--it;
--p;
}
while (it < end) [[likely]] {
*p = *it;
if (*it == '"') {
value.assign(temp.data(), size_t(p - temp.data()));
++it;
return;
}
else if (*it == '\\') {
++it; // skip the escape
if (*it == 'u') {
++it;
if (!handle_unicode_code_point(it, p, end)) [[unlikely]] {
ctx.error = error_code::unicode_escape_conversion_failure;
return;
}
}
else {
*p = char_unescape_table[*it];
if (*p == 0) [[unlikely]] {
ctx.error = error_code::invalid_escape;
return;
}
++p;
++it;
}
}
else {
++it;
++p;
}
}
ctx.error = error_code::unexpected_end;
}
else {
// raw_string
auto start = it;
skip_string_view<Opts>(ctx, it, end);
if (bool(ctx.error)) [[unlikely]]
return;
value.assign(start, size_t(it - start));
++it;
}
}
}
};
template <class T>
requires(string_view_t<T> || char_array_t<T>)
struct from_json<T>
{
template <auto Opts, class It, class End>
GLZ_ALWAYS_INLINE static void op(auto& value, is_context auto&& ctx, It&& it, End&& end) noexcept
{
if constexpr (!Opts.opening_handled) {
if constexpr (!Opts.ws_handled) {
GLZ_SKIP_WS;
}
GLZ_MATCH_QUOTE;
}
auto start = it;
if constexpr (string_view_t<T>) {
skip_string_view<Opts>(ctx, it, end);
if (bool(ctx.error)) [[unlikely]]
return;
value = {start, size_t(it - start)};
++it;
}
else if constexpr (char_array_t<T>) {
skip_string_view<Opts>(ctx, it, end);
if (bool(ctx.error)) [[unlikely]]
return;
const size_t n = it - start;
if ((sizeof(value) - 1) < n) {
ctx.error = error_code::unexpected_end;
return;
}
std::memcpy(value, start, n);
value[n] = '\0';
}
}
};
template <char_t T>
struct from_json<T>
{
template <auto Opts>
GLZ_ALWAYS_INLINE static void op(auto& value, is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
if constexpr (!Opts.opening_handled) {
if constexpr (!Opts.ws_handled) {
GLZ_SKIP_WS;
}
GLZ_MATCH_QUOTE;
}
if (*it == '\\') [[unlikely]] {
++it;
switch (*it) {
case '\0': {
ctx.error = error_code::unexpected_end;
return;
}
case '"':
case '\\':
case '/':
value = *it++;
break;
case 'b':
value = '\b';
++it;
break;
case 'f':
value = '\f';
++it;
break;
case 'n':
value = '\n';
++it;
break;
case 'r':
value = '\r';
++it;
break;
case 't':
value = '\t';
++it;
break;
case 'u': {
ctx.error = error_code::unicode_escape_conversion_failure;
return;
}
default: {
ctx.error = error_code::invalid_escape;
return;
}
}
}
else {
if (it == end) [[unlikely]] {
ctx.error = error_code::unexpected_end;
return;
}
value = *it++;
}
GLZ_MATCH_QUOTE;
}
};
template <class T>
requires(glaze_enum_t<T> && !specialized_with_custom_read<T>)
struct from_json<T>
{
template <auto Opts>
GLZ_ALWAYS_INLINE static void op(auto& value, is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
if constexpr (!Opts.ws_handled) {
GLZ_SKIP_WS;
}
const auto key = parse_key(ctx, it, end); // TODO: Use more optimal enum key parsing
if (bool(ctx.error)) [[unlikely]]
return;
static constexpr auto frozen_map = detail::make_string_to_enum_map<T>();
const auto& member_it = frozen_map.find(key);
if (member_it != frozen_map.end()) {
value = member_it->second;
}
else [[unlikely]] {
ctx.error = error_code::unexpected_enum;
}
}
};
template <class T>
requires(std::is_enum_v<T> && !glaze_enum_t<T> && !specialized_with_custom_read<T>)
struct from_json<T>
{
template <auto Opts>
GLZ_ALWAYS_INLINE static void op(auto& value, is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
// read<json>::op<Opts>(*reinterpret_cast<std::underlying_type_t<std::decay_t<decltype(value)>>*>(&value),
// ctx, it, end);
std::underlying_type_t<std::decay_t<T>> x{};
read<json>::op<Opts>(x, ctx, it, end);
value = static_cast<std::decay_t<T>>(x);
}
};
template <func_t T>
struct from_json<T>
{
template <auto Opts>
GLZ_ALWAYS_INLINE static void op(auto& /*value*/, is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
if constexpr (!Opts.ws_handled) {
GLZ_SKIP_WS;
}
GLZ_MATCH_QUOTE;
skip_string_view<Opts>(ctx, it, end);
if (bool(ctx.error)) [[unlikely]]
return;
GLZ_MATCH_QUOTE;
}
};
template <class T>
struct from_json<basic_raw_json<T>>
{
template <auto Opts>
GLZ_ALWAYS_INLINE static void op(auto&& value, is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
auto it_start = it;
skip_value<Opts>(ctx, it, end);
if (bool(ctx.error)) [[unlikely]]
return;
value.str = {it_start, static_cast<size_t>(it - it_start)};
}
};
template <class T>
struct from_json<basic_text<T>>
{
template <auto Opts>
GLZ_ALWAYS_INLINE static void op(auto&& value, is_context auto&&, auto&& it, auto&& end) noexcept
{
value.str = {it, static_cast<size_t>(end - it)}; // read entire contents as string
it = end;
}
};
// for set types
template <class T>
requires(readable_array_t<T> && !emplace_backable<T> && !resizable<T> && emplaceable<T>)
struct from_json<T>
{
template <auto Options>
GLZ_FLATTEN static void op(auto& value, is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
constexpr auto Opts = ws_handled_off<Options>();
if constexpr (!Options.ws_handled) {
GLZ_SKIP_WS;
}
match<'['>(ctx, it);
if (bool(ctx.error)) [[unlikely]]
return;
GLZ_SKIP_WS;
value.clear();
if (*it == ']') [[unlikely]] {
++it;
return;
}
while (true) {
using V = range_value_t<T>;
V v;
read<json>::op<Opts>(v, ctx, it, end);
if (bool(ctx.error)) [[unlikely]]
return;
value.emplace(std::move(v));
GLZ_SKIP_WS;
if (*it == ']') {
++it;
return;
}
GLZ_MATCH_COMMA;
}
}
};
// for types like std::vector, std::array, std::deque, etc.
template <class T>
requires(readable_array_t<T> && (emplace_backable<T> || !resizable<T>) && !emplaceable<T>)
struct from_json<T>
{
template <auto Options>
GLZ_FLATTEN static void op(auto&& value, is_context auto&& ctx, auto&& it, auto&& end) noexcept
{
constexpr auto Opts = ws_handled_off<Options>();
if constexpr (!Options.ws_handled) {
GLZ_SKIP_WS;
}
match<'['>(ctx, it);
if (bool(ctx.error)) [[unlikely]]
return;
const auto ws_start = it;
GLZ_SKIP_WS;
if (*it == ']') [[unlikely]] {
++it;
if constexpr (resizable<T>) {
value.clear();
if constexpr (Opts.shrink_to_fit) {
value.shrink_to_fit();
}
}
return;
}
const size_t ws_size = size_t(it - ws_start);
const auto n = value.size();
auto value_it = value.begin();
for (size_t i = 0; i < n; ++i) {
read<json>::op<ws_handled<Opts>()>(*value_it++, ctx, it, end);
if (bool(ctx.error)) [[unlikely]]
return;
GLZ_SKIP_WS;
if (*it == ',') [[likely]] {
++it;
if constexpr (!Opts.minified) {
if (ws_size && ws_size < size_t(end - it)) {
skip_matching_ws(ws_start, it, ws_size);
}
}
GLZ_SKIP_WS;
}
else if (*it == ']') {
++it;
if constexpr (erasable<T>) {
value.erase(value_it,
value.end()); // use erase rather than resize for non-default constructible elements