-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsonobjs.h
1692 lines (1626 loc) · 49.7 KB
/
jsonobjs.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
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
// Copyright David Lawrence Bien 1997 - 2021.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// https://www.boost.org/LICENSE_1_0.txt).
// jsonobjs.h
// JSON objects for reading/writing using jsonstrm.h.
// dbien 01APR2020
#include <vector>
#include <map>
#include <compare>
#include "jsonstrm.h"
#include "strwrsv.h"
__BIENUTIL_BEGIN_NAMESPACE
// predeclare
template <class t_tyChar>
class JsoValue;
template <class t_tyChar>
class _JsoObject;
template <class t_tyChar>
class _JsoArray;
template <class t_tyChar, bool t_kfConst>
class JsoIterator;
// This exception will get thrown if the user of the read cursor does something inappropriate given the current context.
class json_objects_bad_usage_exception : public _t__Named_exception<__JSONSTRM_DEFAULT_ALLOCATOR>
{
typedef json_objects_bad_usage_exception _TyThis;
typedef _t__Named_exception<__JSONSTRM_DEFAULT_ALLOCATOR> _TyBase;
public:
json_objects_bad_usage_exception( const char * _pc )
: _TyBase( _pc )
{
}
json_objects_bad_usage_exception(const string_type &__s)
: _TyBase(__s)
{
}
json_objects_bad_usage_exception(const char *_pcFmt, va_list _args)
: _TyBase(_pcFmt, _args)
{
}
};
// By default we will always add the __FILE__, __LINE__ even in retail for debugging purposes.
#define THROWJSONBADUSAGE(MESG, ...) ExceptionUsage<json_objects_bad_usage_exception>::ThrowFileLineFunc(__FILE__, __LINE__, FUNCTION_PRETTY_NAME, MESG, ##__VA_ARGS__)
// JsoIterator:
// This iterator may be iterating an object or an array.
// For an object we iterate in key order.
// For an array we iterate in index order.
template <class t_tyChar, bool t_kfConst>
class JsoIterator
{
typedef JsoIterator _tyThis;
public:
typedef std::conditional_t<t_kfConst, typename _JsoObject<t_tyChar>::_tyConstIterator, typename _JsoObject<t_tyChar>::_tyIterator> _tyObjectIterator;
typedef std::conditional_t<t_kfConst, typename _JsoArray<t_tyChar>::_tyConstIterator, typename _JsoArray<t_tyChar>::_tyIterator> _tyArrayIterator;
typedef JsoValue<t_tyChar> _tyJsoValue;
typedef std::conditional_t<t_kfConst, const _tyJsoValue, _tyJsoValue> _tyQualJsoValue;
typedef typename _JsoObject<t_tyChar>::_tyMapValueType _tyKeyValueType; // This type is only used by objects.
typedef std::conditional_t<t_kfConst, const _tyKeyValueType, _tyKeyValueType> _tyQualKeyValueType;
~JsoIterator()
{
if (!!m_pvIterator)
_DestroyIterator(m_pvIterator);
}
JsoIterator() = default;
JsoIterator(const JsoIterator &_r)
: m_fObjectIterator(_r.m_fObjectIterator)
{
m_fObjectIterator ? _CreateIterator(_r.GetObjectIterator()) : _CreateIterator(_r.GetArrayIterator());
}
JsoIterator(const JsoIterator &&_rr)
: m_fObjectIterator(_rr.m_fObjectIterator)
{
m_fObjectIterator ? _CreateIterator(std::move(_rr.GetObjectIterator())) : _CreateIterator(std::move(_rr.GetArrayIterator()));
}
explicit JsoIterator(_tyObjectIterator const &_rit)
: m_fObjectIterator(true)
{
_CreateIterator(_rit);
}
JsoIterator(_tyObjectIterator &&_rrit)
: m_fObjectIterator(true)
{
_CreateIterator(std::move(_rrit));
}
explicit JsoIterator(_tyArrayIterator const &_rit)
: m_fObjectIterator(false)
{
_CreateIterator(_rit);
}
JsoIterator(_tyArrayIterator &&_rrit)
: m_fObjectIterator(false)
{
_CreateIterator(std::move(_rrit));
}
JsoIterator &operator=(JsoIterator const &_r)
{
if (this != &_r)
{
Clear();
m_fObjectIterator = _r.m_fObjectIterator;
m_fObjectIterator ? _CreateIterator(_r.GetObjectIterator()) : _CreateIterator(_r.GetArrayIterator());
}
return *this;
}
JsoIterator &operator=(JsoIterator &&_rr)
{
if (this != &_rr)
{
Clear();
swap(_rr);
}
return *this;
}
bool FIsObjectIterator() const
{
return m_fObjectIterator;
}
bool FIsArrayIterator() const
{
return !m_fObjectIterator;
}
EJsonValueType JvtGetValueType() const
{
return m_fObjectIterator ? ejvtObject : ejvtArray;
}
void swap(JsoIterator &_r)
{
std::swap(m_fObjectIterator, _r.m_fObjectIterator);
std::swap(m_pvIterator, _r.m_pvIterator);
}
void Clear()
{
if (!!m_pvIterator)
{
void *pv = m_pvIterator;
m_pvIterator = nullptr;
_DestroyIterator(pv);
}
}
JsoIterator &operator++()
{
if (m_fObjectIterator)
++GetObjectIterator();
else
++GetArrayIterator();
return *this;
}
JsoIterator operator++(int)
{
if (m_fObjectIterator)
return JsoIterator(GetObjectIterator()++);
else
return JsoIterator(GetArrayIterator()++);
}
JsoIterator &operator--()
{
if (m_fObjectIterator)
--GetObjectIterator();
else
--GetArrayIterator();
return *this;
}
JsoIterator operator--(int)
{
if (m_fObjectIterator)
return JsoIterator(GetObjectIterator()--);
else
return JsoIterator(GetArrayIterator()--);
}
typename _JsoArray<t_tyChar>::difference_type operator-(const JsoIterator &_r) const
{
if (m_fObjectIterator)
THROWJSONBADUSAGE("Not valid for object iterator.");
return GetArrayIterator() - _r.GetArrayIterator();
}
const _tyObjectIterator &GetObjectIterator() const
{
return const_cast<_tyThis *>(this)->GetObjectIterator();
}
_tyObjectIterator &GetObjectIterator()
{
if (!m_pvIterator)
THROWJSONBADUSAGE("Not connected to iterator.");
if (!m_fObjectIterator)
THROWJSONBADUSAGE("Called on array.");
return *(_tyObjectIterator *)m_pvIterator;
}
const _tyArrayIterator &GetArrayIterator() const
{
return const_cast<_tyThis *>(this)->GetArrayIterator();
}
_tyArrayIterator &GetArrayIterator()
{
if (!m_pvIterator)
THROWJSONBADUSAGE("Not connected to iterator.");
if (m_fObjectIterator)
THROWJSONBADUSAGE("Called on object.");
return *(_tyArrayIterator *)m_pvIterator;
}
// This always returns the value for both objects and arrays since it has to return the same thing.
_tyQualJsoValue &operator*() const
{
return m_fObjectIterator ? GetObjectIterator()->second : *GetArrayIterator();
}
_tyQualJsoValue *operator->() const
{
return m_fObjectIterator ? &GetObjectIterator()->second : &*GetArrayIterator();
}
_tyQualKeyValueType &GetKeyValue() const
{
return *GetObjectIterator();
}
bool operator==(const _tyThis &_r) const
{
if (m_fObjectIterator != _r.m_fObjectIterator)
return false;
return m_fObjectIterator ? (_r.GetObjectIterator() == GetObjectIterator()) : (_r.GetArrayIterator() == GetArrayIterator());
}
bool operator!=(const _tyThis &_r) const
{
return !this->operator==(_r);
}
protected:
void _CreateIterator(_tyObjectIterator const &_rit)
{
Assert(!m_pvIterator);
Assert(m_fObjectIterator);
m_pvIterator = DBG_NEW _tyObjectIterator(_rit);
}
void _CreateIterator(_tyObjectIterator &&_rrit)
{
Assert(!m_pvIterator);
Assert(m_fObjectIterator);
m_pvIterator = DBG_NEW _tyObjectIterator(std::move(_rrit));
}
void _CreateIterator(_tyArrayIterator const &_rit)
{
Assert(!m_pvIterator);
Assert(!m_fObjectIterator);
m_pvIterator = DBG_NEW _tyArrayIterator(_rit);
}
void _CreateIterator(_tyArrayIterator &&_rrit)
{
Assert(!m_pvIterator);
Assert(!m_fObjectIterator);
m_pvIterator = DBG_NEW _tyArrayIterator(std::move(_rrit));
}
void _DestroyIterator(void *_pv)
{
Assert(!!_pv);
if (m_fObjectIterator)
delete (_tyObjectIterator *)_pv;
else
delete (_tyArrayIterator *)_pv;
}
void *m_pvIterator{nullptr}; // This is a pointer to a dynamically allocated iterator of the appropriate type.
bool m_fObjectIterator{true};
};
// JsoValue:
// Every JSON object is a value. In fact every single JSON object is represented by the class JsoValue because that is the best spacewise
// way of doing things. We embed the string/object/array within this class to implement the different JSON objects.
#if defined(__amd64__) || defined(_M_AMD64) || defined(__x86_64__) || defined(__ia64__)
#pragma pack(push, 8) // Ensure that we pack this on an 8 byte boundary for 64bit compilation.
#else
#pragma pack(push, 4) // Ensure that we pack this on an 4 byte boundary for 32bit compilation.
#endif
template <class t_tyChar>
class JsoValue
{
typedef JsoValue _tyThis;
public:
typedef t_tyChar _tyChar;
typedef JsonCharTraits<_tyChar> _tyCharTraits;
typedef const _tyChar *_tyLPCSTR;
typedef std::basic_string<_tyChar> _tyStdStr;
typedef StrWRsv<_tyStdStr> _tyStrWRsv; // string with reserve buffer.
typedef _JsoObject<_tyChar> _tyJsoObject;
typedef _JsoArray<_tyChar> _tyJsoArray;
typedef JsonFormatSpec<_tyCharTraits> _tyJsonFormatSpec;
typedef JsoIterator<_tyChar, false> iterator;
typedef JsoIterator<_tyChar, true> const_iterator;
~JsoValue()
{
_ClearValue();
}
JsoValue(EJsonValueType _jvt = ejvtJsonValueTypeCount)
{
if (ejvtJsonValueTypeCount != _jvt)
_AllocateValue(_jvt);
}
JsoValue(const JsoValue &_r)
{
*this = _r;
}
JsoValue(JsoValue &&_rr)
{
if (_rr.JvtGetValueType() != ejvtJsonValueTypeCount)
{
switch (_rr.JvtGetValueType())
{
case ejvtNumber:
case ejvtString:
new (m_rgbyValBuf) _tyStrWRsv(std::move(_rr.StrGet()));
break;
case ejvtObject:
new (m_rgbyValBuf) _tyJsoObject(std::move(_rr._ObjectGet()));
break;
case ejvtArray:
new (m_rgbyValBuf) _tyJsoArray(std::move(_rr._ArrayGet()));
break;
default:
Assert(0); // random value...
case ejvtNull:
case ejvtTrue:
case ejvtFalse:
case ejvtJsonValueTypeCount:
break;
}
m_jvtType = _rr.JvtGetValueType(); // throw-safety.
}
}
JsoValue &operator=(const JsoValue &_r)
{
if (this != &_r)
{
SetValueType(_r.JvtGetValueType());
switch (JvtGetValueType())
{
case ejvtNull:
case ejvtTrue:
case ejvtFalse:
break; // nothing to do
case ejvtNumber:
case ejvtString:
StrGet() = _r.StrGet();
break;
case ejvtObject:
_ObjectGet() = _r._ObjectGet();
break;
case ejvtArray:
_ArrayGet() = _r._ArrayGet();
break;
default:
Assert(0); // random value...
[[fallthrough]];
case ejvtJsonValueTypeCount:
break; // assigned to an empty object.
}
}
return *this;
}
JsoValue &operator=(JsoValue &&_rr)
{
if (this != &_rr)
{
Clear();
if (_rr.JvtGetValueType() != ejvtJsonValueTypeCount)
{
switch (_rr.JvtGetValueType())
{
case ejvtNumber:
case ejvtString:
new (m_rgbyValBuf) _tyStrWRsv(std::move(_rr.StrGet()));
break;
case ejvtObject:
new (m_rgbyValBuf) _tyJsoObject(std::move(_rr._ObjectGet()));
break;
case ejvtArray:
new (m_rgbyValBuf) _tyJsoArray(std::move(_rr._ArrayGet()));
break;
default:
Assert(0); // random value...
case ejvtNull:
case ejvtTrue:
case ejvtFalse:
case ejvtJsonValueTypeCount:
break;
}
m_jvtType = _rr.JvtGetValueType(); // throw-safety.
}
}
return *this;
}
void swap(JsoValue &_r)
{
std::swap(m_jvtType, _r.m_jvtType);
if (_r._FHasBufData() || _FHasBufData())
{
uint8_t rgbyValBuf[s_kstSizeValBuf];
memcpy(rgbyValBuf, _r.m_rgbyValBuf, sizeof(m_rgbyValBuf));
memcpy(_r.m_rgbyValBuf, m_rgbyValBuf, sizeof(m_rgbyValBuf));
memcpy(m_rgbyValBuf, rgbyValBuf, sizeof(m_rgbyValBuf));
}
}
bool _FHasBufData() const
{
return m_jvtType >= ejvtFirstJsonValueless && m_jvtType <= ejvtLastJsonSpecifiedValue;
}
EJsonValueType JvtGetValueType() const
{
return m_jvtType;
}
void SetValueType(const EJsonValueType _jvt)
{
if (_jvt != m_jvtType)
{
_ClearValue();
_AllocateValue(_jvt);
}
}
void Clear()
{
SetValueType(ejvtJsonValueTypeCount);
}
// Compare objects:
bool operator==(const _tyThis &_r) const
{
return FCompare(_r);
}
bool operator!=(const _tyThis &_r) const
{
return !FCompare(_r);
}
bool FCompare(_tyThis const &_r) const
{
bool fSame = JvtGetValueType() == _r.JvtGetValueType(); // Arbitrary comparison between different types.
if (fSame)
{
switch (JvtGetValueType())
{
case ejvtNull:
case ejvtTrue:
case ejvtFalse:
break;
case ejvtNumber:
case ejvtString:
// Note that I don't mean to compare numbers as numbers - only as the strings they are represented in.
fSame = (StrGet() == _r.StrGet());
break;
case ejvtObject:
fSame = (_ObjectGet() == _r._ObjectGet());
break;
case ejvtArray:
fSame = (_ArrayGet() == _r._ArrayGet());
break;
default:
case ejvtJsonValueTypeCount:
THROWJSONBADUSAGE("Invalid value type [%hhu].", JvtGetValueType());
break;
}
}
return fSame;
}
std::strong_ordering operator<=>(_tyThis const &_r) const
{
return ICompare(_r);
}
std::strong_ordering ICompare(_tyThis const &_r) const
{
auto comp = (int)JvtGetValueType() <=> (int)_r.JvtGetValueType(); // Arbitrary comparison between different types.
if (0 == comp)
{
switch (JvtGetValueType())
{
case ejvtNull:
case ejvtTrue:
case ejvtFalse:
break;
case ejvtNumber:
case ejvtString:
// Note that I don't mean to compare numbers as numbers - only as the strings they are represented in.
comp = StrGet() <=> _r.StrGet();
break;
case ejvtObject:
comp = _ObjectGet() <=> _r._ObjectGet();
break;
case ejvtArray:
comp = _ArrayGet() <=> _r._ArrayGet();
break;
default:
case ejvtJsonValueTypeCount:
THROWJSONBADUSAGE("Invalid value type [%hhu].", JvtGetValueType());
break;
}
}
return comp;
}
bool FEmpty() const
{
return JvtGetValueType() == ejvtJsonValueTypeCount;
}
bool FIsNullOrEmpty() const
{
return FIsNull() || FEmpty();
}
bool FIsNull() const
{
return ejvtNull == m_jvtType;
}
bool FIsBoolean() const
{
return (ejvtTrue == m_jvtType) || (ejvtFalse == m_jvtType);
}
bool FIsTrue() const
{
return ejvtTrue == m_jvtType;
}
bool FIsFalse() const
{
return ejvtFalse == m_jvtType;
}
bool FIsString() const
{
return (ejvtString == m_jvtType);
}
bool FIsNumber() const
{
return (ejvtNumber == m_jvtType);
}
bool FIsAggregate() const
{
return FIsObject() || FIsArray();
}
bool FIsObject() const
{
return ejvtObject == m_jvtType;
}
bool FIsArray() const
{
return ejvtArray == m_jvtType;
}
size_t GetSize() const
{
if (!FIsAggregate())
THROWJSONBADUSAGE("Called on non-aggregate.");
if (FIsObject())
return _ObjectGet().GetSize();
else
return _ArrayGet().GetSize();
}
void GetBoolValue(bool &_rf) const
{
if (ejvtTrue == m_jvtType)
_rf = true;
else if (ejvtFalse == m_jvtType)
_rf = false;
else
THROWJSONBADUSAGE("Called on non-boolean.");
}
const _tyStrWRsv &StrGet() const
{
Assert( FIsString() || FIsNumber() );
if (!FIsString() && !FIsNumber())
THROWJSONBADUSAGE("Called on non-string/num.");
return *static_cast<const _tyStrWRsv *>((const void *)m_rgbyValBuf);
}
_tyStrWRsv &StrGet()
{
Assert( FIsString() || FIsNumber() );
if (!FIsString() && !FIsNumber())
THROWJSONBADUSAGE("Called on non-string/num.");
return *static_cast<_tyStrWRsv *>((void *)m_rgbyValBuf);
}
const _tyJsoObject &_ObjectGet() const
{
Assert( FIsObject() );
if (!FIsObject())
THROWJSONBADUSAGE("Called on non-Object.");
return *static_cast<const _tyJsoObject *>((const void *)m_rgbyValBuf);
}
_tyJsoObject &_ObjectGet()
{
Assert( FIsObject() );
if (!FIsObject())
THROWJSONBADUSAGE("Called on non-Object.");
return *static_cast<_tyJsoObject *>((void *)m_rgbyValBuf);
}
const _tyJsoArray &_ArrayGet() const
{
Assert( FIsArray() );
if (!FIsArray())
THROWJSONBADUSAGE("Called on non-Array.");
return *static_cast<const _tyJsoArray *>((const void *)m_rgbyValBuf);
}
_tyJsoArray &_ArrayGet()
{
Assert( FIsArray() );
if (!FIsArray())
THROWJSONBADUSAGE("Called on non-Array.");
return *static_cast<_tyJsoArray *>((void *)m_rgbyValBuf);
}
// Various number conversion methods.
template <class t_tyNum>
void _GetValue(_tyLPCSTR _pszFmt, t_tyNum &_rNumber) const
{
Assert( FIsNumber() );
if (ejvtNumber != JvtGetValueType())
THROWJSONBADUSAGE("Not at a numeric value type.");
// The presumption is that sscanf won't read past any decimal point if scanning a non-floating point number.
int iRet = sscanf(StrGet().c_str(), _pszFmt, &_rNumber);
Assert(1 == iRet); // Due to the specification of number we expect this to always succeed.
}
void GetValue(uint8_t &_rby) const { _GetValue("%hhu", _rby); }
void GetValue(int8_t &_rsby) const { _GetValue("%hhd", _rsby); }
void GetValue(uint16_t &_rus) const { _GetValue("%hu", _rus); }
void GetValue(int16_t &_rss) const { _GetValue("%hd", _rss); }
void GetValue(uint32_t &_rui) const { _GetValue("%u", _rui); }
void GetValue(int32_t &_rsi) const { _GetValue("%d", _rsi); }
void GetValue(uint64_t &_rul) const { _GetValue("%llu", _rul); }
void GetValue(int64_t &_rsl) const { _GetValue("%lld", _rsl); }
void GetValue(float &_rfl) const { _GetValue("%e", _rfl); }
void GetValue(double &_rdbl) const { _GetValue("%e", _rdbl); }
void GetValue(long double &_rldbl) const { _GetValue("%Le", _rldbl); }
// Setting methods: These overwrite the existing element at this location.
void SetEmpty() // Note that this is not the same as the NullValue - see below.
{
SetValueType(ejvtJsonValueTypeCount);
}
void SetNullValue()
{
SetValueType(ejvtNull);
}
void SetBoolValue(bool _f)
{
SetValueType(_f ? ejvtTrue : ejvtFalse);
}
_tyThis &operator=(bool _f)
{
SetBoolValue(_f);
}
void SetStringValue(_tyLPCSTR _psz, size_t _stLen = (std::numeric_limits<size_t>::max)())
{
if (_stLen == (std::numeric_limits<size_t>::max)())
_stLen = StrNLen(_psz);
SetValueType(ejvtString);
StrGet().assign(_psz, _stLen);
}
// String methods for the current character type.
template <class t_tyStr>
void SetStringValue(t_tyStr const &_rstr)
requires ( TAreSameSizeTypes_v< typename t_tyStr::value_type, _tyChar > )
{
SetStringValue( (const _tyChar *)&_rstr[0], _rstr.length()) ;
}
// String methods requiring conversion. No reason for the move method since we must convert anyway.
template <class t_tyStr>
void SetStringValue(t_tyStr const& _rstr)
requires ( !TAreSameSizeTypes_v< typename t_tyStr::value_type, _tyChar > )
{
_tyStrWRsv strConverted;
ConvertString(strConverted, _rstr);
SetStringValue(std::move(strConverted));
}
template <class t_tyStr>
void SetStringValue(t_tyStr &&_rrstr)
requires ( is_same_v< typename t_tyStr::value_type, _tyChar > )
{
SetValueType(ejvtString);
StrGet() = std::move( _rrstr );
}
template < class t_tyStr >
_tyThis &operator=( const t_tyStr &_rstr )
requires( TIsCharType_v< typename t_tyStr::value_type > )
{
SetStringValue( _rstr );
return *this;
}
template < class t_tyStr >
_tyThis &operator=(_tyStdStr &&_rrstr)
requires( TIsCharType_v< typename t_tyStr::value_type > )
{
SetStringValue( std::move( _rrstr ) );
return *this;
}
void SetValue(uint8_t _by)
{
_SetValue("%hhu", _by);
}
void SetValue(int8_t _sby)
{
_SetValue("%hhd", _sby);
}
void SetValue(uint16_t _us)
{
_SetValue("%hu", _us);
}
_tyThis &operator=(uint16_t _us)
{
SetValue(_us);
return *this;
}
void SetValue(int16_t _ss)
{
_SetValue("%hd", _ss);
}
_tyThis &operator=(int16_t _ss)
{
SetValue(_ss);
return *this;
}
void SetValue(uint32_t _ui)
{
_SetValue("%u", _ui);
}
_tyThis &operator=(uint32_t _ui)
{
SetValue(_ui);
return *this;
}
void SetValue(int32_t _si)
{
_SetValue("%d", _si);
}
_tyThis &operator=(int32_t _si)
{
SetValue(_si);
return *this;
}
void SetValue(uint64_t _ul)
{
_SetValue("%llu", _ul);
}
_tyThis &operator=(uint64_t _ul)
{
SetValue(_ul);
return *this;
}
void SetValue(int64_t _sl)
{
_SetValue("%lld", _sl);
}
_tyThis &operator=(int64_t _sl)
{
SetValue(_sl);
return *this;
}
void SetValue(double _dbl)
{
_SetValue("%f", _dbl);
}
_tyThis &operator=(double _dbl)
{
SetValue(_dbl);
return *this;
}
void SetValue(long double _ldbl)
{
_SetValue("%Lf", _ldbl);
}
_tyThis &operator=(long double _ldbl)
{
SetValue(_ldbl);
return *this;
}
#if defined( WIN32 ) || defined ( __APPLE__ ) // Linux seems to handle this correctly without - and there's an endless loop with...
// Translate long and unsigned long appropriately by platform size:
void SetValue(long _l)
requires ( sizeof( int32_t ) == sizeof( long ) )
{
return SetValue((int32_t)_l);
}
_tyThis &operator =(long _l)
requires ( sizeof( int32_t ) == sizeof( long ) )
{
SetValue((int32_t)_l);
return *this;
}
void SetValue(long _l)
requires (sizeof(int64_t) == sizeof(long))
{
return SetValue((int64_t)_l);
}
_tyThis &operator =(long _l)
requires (sizeof(int64_t) == sizeof(long))
{
SetValue((int64_t)_l);
return *this;
}
void SetValue(unsigned long _l)
requires (sizeof(uint32_t) == sizeof(unsigned long))
{
return SetValue((uint32_t)_l);
}
_tyThis &operator =(unsigned long _l)
requires (sizeof(uint32_t) == sizeof(unsigned long))
{
SetValue((uint32_t)_l);
return *this;
}
void SetValue(unsigned long _l)
requires (sizeof(uint64_t) == sizeof(unsigned long))
{
return SetValue((uint64_t)_l);
}
_tyThis &operator =(unsigned long _l)
requires (sizeof(uint64_t) == sizeof(unsigned long))
{
SetValue((uint64_t)_l);
return *this;
}
#endif //WIN32 || APPLE
// Read JSON from a string - throws upon finding bad JSON, etc.
template <class t_tyStr>
void FromString(t_tyStr const &_rstr)
{
FromString(&_rstr[0], _rstr.length()); // works for string views.
}
void FromString(const _tyChar *_psz, size_t _stLen)
{
typedef JsonFixedMemInputStream<_tyCharTraits> _tyJsonInputStream;
typedef JsonReadCursor<_tyJsonInputStream> _tyJsonReadCursor;
_tyJsonInputStream jisFixed(_psz, _stLen);
_tyJsonReadCursor jrc;
jisFixed.AttachReadCursor(jrc);
FromJSONStream(jrc);
}
template <class t_tyJsonInputStream>
void FromJSONStream(JsonReadCursor<t_tyJsonInputStream> &_jrc)
{
SetValueType(_jrc.JvtGetValueType());
switch (JvtGetValueType())
{
case ejvtNull:
case ejvtTrue:
case ejvtFalse:
break;
case ejvtNumber:
case ejvtString:
_jrc.GetValue(StrGet());
break;
case ejvtObject:
_ObjectGet().FromJSONStream(_jrc);
break;
case ejvtArray:
_ArrayGet().FromJSONStream(_jrc);
break;
default:
case ejvtJsonValueTypeCount:
THROWJSONBADUSAGE("Invalid value type [%hhu].", JvtGetValueType());
break;
}
}
template <class t_tyStr, class t_tyFilter>
void FromString( t_tyStr const &_rstr, t_tyFilter &_rfFilter )
{
FromString( &_rstr[0], _rstr.length(), _rfFilter );
}
template <class t_tyFilter>
void FromString( const _tyChar *_psz, size_t _stLen, t_tyFilter &_rfFilter )
{
typedef JsonFixedMemInputStream<_tyCharTraits> _tyJsonInputStream;
typedef JsonReadCursor<_tyCharTraits> _tyJsonReadCursor;
_tyJsonInputStream jisFixed(_psz, _stLen);
_tyJsonReadCursor jrc;
jisFixed.AttachReadCursor(jrc);
FromJSONStream(jrc, _rfFilter);
}
// FromJSONStream with a filter method.
// The filtering takes place in the objects and arrays because these create sub-values.
// The filter is not applied to the root value itself.
template <class t_tyJsonInputStream, class t_tyFilter>
void FromJSONStream(JsonReadCursor<t_tyJsonInputStream> &_jrc, t_tyFilter &_rfFilter)
{
SetValueType(_jrc.JvtGetValueType());
switch (JvtGetValueType())
{
case ejvtNull:
case ejvtTrue:
case ejvtFalse:
break;
case ejvtNumber:
case ejvtString:
_jrc.GetValue(StrGet());
break;
case ejvtObject:
_ObjectGet().FromJSONStream(_jrc, *this, _rfFilter);
break;
case ejvtArray:
_ArrayGet().FromJSONStream(_jrc, *this, _rfFilter);
break;
default:
case ejvtJsonValueTypeCount:
THROWJSONBADUSAGE("Invalid value type [%hhu].", JvtGetValueType());
break;
}
}
template <class t_tyStr>
void ToString(t_tyStr &_rstr, const _tyJsonFormatSpec *_pjfs = 0) const
{
// Stream to memory stream (segmented array) and then copy that to the string.
typedef JsonOutputMemStream<_tyCharTraits> _tyJsonOutputStream;
typedef JsonValueLife<_tyJsonOutputStream> _tyJsonValueLife;
_tyJsonOutputStream jos;
{ //B
_tyJsonValueLife jvlRoot(jos, JvtGetValueType(), _pjfs);
ToJSONStream(jvlRoot);
} //EB
size_t stLen = jos.GetLengthChars();
_rstr.resize(stLen); // fills to stLen+1 with 0 and sets length to stLen.
typename _tyJsonOutputStream::_tyMemStream &rms = jos.GetMemStream();
rms.Seek(0, SEEK_SET);
rms.Read(&_rstr[0], stLen * sizeof(_tyChar));
}
template <class t_tyJsonOutputStream>
void ToJSONStream(JsonValueLife<t_tyJsonOutputStream> &_jvl) const
{
Assert(JvtGetValueType() == _jvl.JvtGetValueType());
switch (JvtGetValueType())
{
case ejvtNull:
case ejvtTrue:
case ejvtFalse:
break; // nothing to do - _jvl has already been created with the correct value type.
case ejvtNumber:
case ejvtString:
_jvl.RJvGet().PCreateStringValue()->assign(StrGet());
break;
case ejvtObject:
_ObjectGet().ToJSONStream(_jvl);
break;
case ejvtArray:
_ArrayGet().ToJSONStream(_jvl);
break;
default:
case ejvtJsonValueTypeCount:
THROWJSONBADUSAGE("Invalid value type [%hhu].", JvtGetValueType());
break;
}
}
template <class t_tyStr, class t_tyFilter>
void ToString(t_tyStr &_rstr, t_tyFilter &_rfFilter, const _tyJsonFormatSpec *_pjfs = 0) const
{
// Stream to memory stream (segmented array) and then copy that to the string.
typedef JsonOutputMemStream<_tyCharTraits> _tyJsonOutputStream;
typedef JsonValueLife<_tyJsonOutputStream> _tyJsonValueLife;
_tyJsonOutputStream jos;
{ //B
_tyJsonValueLife jvlRoot(jos, JvtGetValueType(), _pjfs);
ToJSONStream(jvlRoot, _rfFilter);
} //EB
size_t stLen = jos.GetLengthChars();
_rstr.resize(stLen); // fills to stLen+1 with 0 and sets length to stLen.
typename _tyJsonOutputStream::_tyMemStream &rms = jos.GetMemStream();
rms.Seek(0, SEEK_SET);
rms.Read(&_rstr[0], stLen * sizeof(_tyChar));
}
template <class t_tyJsonOutputStream, class t_tyFilter>
void ToJSONStream(JsonValueLife<t_tyJsonOutputStream> &_jvl, t_tyFilter &_rfFilter) const
{
Assert(JvtGetValueType() == _jvl.JvtGetValueType());
switch (JvtGetValueType())
{
case ejvtNull:
case ejvtTrue:
case ejvtFalse:
break; // nothing to do - _jvl has already been created with the correct value type.
case ejvtNumber:
case ejvtString:
_jvl.RJvGet().PCreateStringValue()->assign(StrGet());
break;
case ejvtObject:
_ObjectGet().ToJSONStream(_jvl, *this, _rfFilter);
break;
case ejvtArray:
_ArrayGet().ToJSONStream(_jvl, *this, _rfFilter);
break;
default:
case ejvtJsonValueTypeCount: