-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathTinyJSON.h
More file actions
1930 lines (1680 loc) · 54.5 KB
/
TinyJSON.h
File metadata and controls
1930 lines (1680 loc) · 54.5 KB
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
// Licensed to Florent Guelfucci under one or more agreements.
// Florent Guelfucci licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
#ifndef TJ_INCLUDED
#define TJ_INCLUDED
// Assume that we do not want std::string
#ifndef TJ_INCLUDE_STD_STRING
#define TJ_INCLUDE_STD_STRING 0
#endif
// use the std vector or not, (use the custom array).
// using the vector can cause performance issue as the
// array is optimised for deep searches.
#ifndef TJ_INCLUDE_STDVECTOR
#define TJ_INCLUDE_STDVECTOR 0
#endif
#include <algorithm>
#include <functional>
#include <exception>
#include <iterator>
#include <type_traits>
#include <vector>
#if TJ_INCLUDE_STDVECTOR == 0
#include <stdexcept>
#endif
#if TJ_INCLUDE_STD_STRING == 1
#include <string>
#endif
// https://semver.org/
// Semantic Versioning 2.0.0
// MAJOR version when you make incompatible API changes
// MINOR version when you add functionality in a backward compatible manner
// PATCH version when you make backward compatible bug fixes
// v0.1.1 - added some add( ... ) and set( ... ) methods
// v0.1.2 - added a bit of get/set( ... ) for values and objects.
// v0.1.3 - added iterator.
// v0.1.4 - added copy/move constructors and operators.
// v0.2.0 - Breaking change: get_* methods no longer take throw parameters, use parse_options::strict instead.
// v0.2.1 - added remove_at to TJValueArray.
// v0.2.2 - added support for Json5 https://github.com/json5/
// v0.2.3 - added atomic file saving
// v0.2.4 - added operator[] and as<T>() accessors to TJValue
static const short TJ_VERSION_MAJOR = 0;
static const short TJ_VERSION_MINOR = 2;
static const short TJ_VERSION_PATCH = 4;
static const char TJ_VERSION_STRING[] = "0.2.4";
#ifndef TJ_USE_CHAR
# define TJ_USE_CHAR 1
#endif
#if TJ_USE_CHAR == 1
# define TJCHAR char
# define TJCHARPREFIX(x) x
#elif TJ_USE_CHAR == 8
# define TJCHAR char8_t
# define TJCHARPREFIX(x) u8 ## x
#elif TJ_USE_CHAR == 16
# define TJCHAR char16_t
# define TJCHARPREFIX(x) u ## x
#elif TJ_USE_CHAR == 32
# define TJCHAR char32_t
# define TJCHARPREFIX(x) U ## x
#endif
#define TJ_TEMPLATE_FLOAT \
typename std::enable_if< \
std::is_same<T, float>::value || \
std::is_same<T, double>::value || \
std::is_same<T, long double>::value, \
T >
#define TJ_TEMPLATE_NUMBER \
typename std::enable_if< \
std::is_same<T, signed>::value || \
std::is_same<T, unsigned>::value || \
std::is_same<T, short>::value || \
std::is_same<T, long>::value || \
std::is_same<T, int>::value || \
std::is_same<T, unsigned int>::value || \
std::is_same<T, signed int>::value || \
std::is_same<T, unsigned short int>::value || \
std::is_same<T, signed short int>::value || \
std::is_same<T, long int>::value || \
std::is_same<T, signed long int>::value || \
std::is_same<T, unsigned long int>::value || \
std::is_same<T, long long int>::value || \
std::is_same<T, unsigned long long int>::value, \
T >
namespace TinyJSON
{
#if TJ_INCLUDE_STDVECTOR == 1
#define TJDICTIONARY std::vector<TJMember*>
#define TJLIST std::vector<TJValue*>
#else
class TJList;
class TJDictionary;
#define TJDICTIONARY TJDictionary
#define TJLIST TJList
#endif
// optional class
template<typename T>
class Optional
{
public:
Optional() noexcept : _has_value(false) {}
Optional(const T& value) noexcept(std::is_nothrow_copy_constructible<T>::value) :
_has_value(true)
{
new(&_storage.value) T(value);
}
Optional(T&& value) noexcept(std::is_nothrow_move_constructible<T>::value) :
_has_value(true)
{
new(&_storage.value) T(std::move(value));
}
Optional(const Optional& other) noexcept(std::is_nothrow_copy_constructible<T>::value) :
_has_value(other._has_value)
{
if (_has_value)
{
new(&_storage.value) T(other._storage.value);
}
}
Optional(Optional&& other) noexcept(std::is_nothrow_move_constructible<T>::value) :
_has_value(other._has_value)
{
if (_has_value)
{
new(&_storage.value) T(std::move(other._storage.value));
}
}
~Optional()
{
reset();
}
[[nodiscard]] inline bool operator!() const noexcept
{
if (!has_value())
{
return true;
}
return !(value());
}
[[nodiscard]] inline bool operator==(const Optional& other) const noexcept
{
if (!other.has_value() && !has_value())
{
return true;
}
if (!has_value() || !other.has_value())
{
return false;
}
return other.value() == value();
}
[[nodiscard]] inline bool operator!=(const Optional& other) const noexcept
{
return !(other == *this);
}
[[nodiscard]] inline bool operator==(const T& other) const noexcept
{
if (!has_value())
{
return false;
}
return value() == other;
}
[[nodiscard]] inline bool operator!=(const T& other) const noexcept
{
return !(*this == other);
}
Optional& operator=(const Optional& other) noexcept(std::is_nothrow_copy_constructible<T>::value)
{
if (this != &other)
{
if (other._has_value)
{
if (_has_value)
{
_storage.value = other._storage.value;
}
else
{
new(&_storage.value) T(other._storage.value);
_has_value = true;
}
}
else
{
reset();
}
}
return *this;
}
Optional& operator=(Optional&& other) noexcept(std::is_nothrow_move_constructible<T>::value)
{
if (this != &other)
{
if (other._has_value)
{
if (_has_value)
{
_storage.value = std::move(other._storage.value);
}
else
{
new(&_storage.value) T(std::move(other._storage.value));
_has_value = true;
}
}
else
{
reset();
}
}
return *this;
}
void reset() noexcept
{
if (_has_value)
{
_storage.value.~T();
_has_value = false;
}
}
[[nodiscard]] inline T& value() &
{
if (!_has_value) throw std::logic_error("Optional has no value");
return _storage.value;
}
[[nodiscard]] inline const T& value() const &
{
if (!_has_value) throw std::logic_error("Optional has no value");
return _storage.value;
}
[[nodiscard]] inline const T& value_or(const T& if_has_no_value) const noexcept
{
return _has_value ? _storage.value : if_has_no_value;
}
[[nodiscard]] inline bool has_value() const noexcept
{
return _has_value;
}
[[nodiscard]] inline explicit operator bool() const noexcept
{
return _has_value;
}
[[nodiscard]] inline T& operator*() & noexcept
{
return _storage.value;
}
[[nodiscard]] inline const T& operator*() const & noexcept
{
return _storage.value;
}
[[nodiscard]] inline T* operator->() noexcept
{
return &_storage.value;
}
[[nodiscard]] inline const T* operator->() const noexcept
{
return &_storage.value;
}
private:
union Storage
{
T value;
char dummy;
Storage() : dummy(0) {}
~Storage() {}
} _storage;
bool _has_value;
};
template<typename T>
inline bool operator==(const T& lhs, const Optional<T>& rhs)
{
return rhs == lhs;
}
template<typename T>
inline bool operator!=(const T& lhs, const Optional<T>& rhs)
{
return rhs != lhs;
}
// the various types of formatting.
enum class formatting
{
minify,
indented
};
template<typename T> struct is_vector : std::false_type {};
template<typename T, typename A> struct is_vector<std::vector<T, A>> : std::true_type {};
/// <summary>
/// The parsing options.
/// </summary>
struct parse_options
{
enum specification
{
rfc4627,
rfc7159,
rfc8259,
json5_1_0_0 // https://spec.json5.org/ v1.0.0
};
enum message_type
{
trace,
debug,
info,
warning,
error,
fatal
};
/// <summary>
/// The RFC specification we want to follow.
/// </summary>
specification specification = rfc8259;
/// <summary>
/// If we want to throw an exception or not.
/// </summary>
bool throw_exception = false;
/// <summary>
/// If we want to be strict or not when calling getter methods.
/// </summary>
bool strict = false;
/// <summary>
/// How deep we want to allow the array/objects to recuse.
/// </summary>
unsigned int max_depth = 64;
/// <summary>
/// The callback function that will be called on errors/warnings/etc.
// 0 = trace
// 1 = debug
// 2 = info
// 3 = warning
// 4 = error
// 5 = fatal/panic/exception
/// <summary>
std::function<void(message_type, const TJCHAR*)> callback_function = [] (message_type, const TJCHAR*) {
// do nothing
};
};
/// <summary>
/// The write options.
/// </summary>
struct write_options
{
enum byte_order_mark
{
none,
utf8
};
/// <summary>
/// If we want to throw an exception or not.
/// </summary>
bool throw_exception = false;
/// <summary>
/// The formatting type we want to use.
/// </summary>
formatting write_formatting = formatting::indented;
/// <summary>
/// The byte order mark we will be using.
/// </summary>
byte_order_mark byte_order_mark = none;
};
class TJWriteException : public std::exception
{
public:
TJWriteException(const char* message);
TJWriteException(const TJWriteException& exception);
TJWriteException& operator=(const TJWriteException& exception);
virtual ~TJWriteException() noexcept;
virtual const char* what() const noexcept;
private:
void free_message() noexcept;
void assign_message(const char* message);
char* _message;
};
class TJParseException : public std::exception
{
public:
TJParseException(const char* message);
TJParseException(const TJParseException& exception);
TJParseException& operator=(const TJParseException& exception);
virtual ~TJParseException() noexcept;
virtual const char* what() const noexcept;
private:
void free_message() noexcept;
void assign_message(const char* message);
char* _message;
};
struct internal_dump_configuration;
class TJHelper;
class TJValueArray;
class TJValueObject;
// A simple JSON value, the base of all items in a json
class TJValue
{
friend TJValueArray;
friend TJValueObject;
private:
template<bool IsConst>
class base_iterator
{
friend TJValue;
int _current;
const int _size;
#if __cplusplus >= 201402L // C++14 or later
using TJValueType = std::conditional_t<IsConst, const TJValue, TJValue>;
#else
using TJValueType = typename std::conditional<IsConst, const TJValue, TJValue>::type;
#endif
TJValueType& _type;
static base_iterator make_begin(TJValueType& value)
{
base_iterator it(value);
it._current = 0;
return it;
}
static base_iterator make_end(TJValueType& value)
{
base_iterator it(value);
it._current = it._size;
return it;
}
public:
base_iterator(TJValueType& type) :
_current(0),
_size(type.internal_size()),
_type(type)
{
}
const TJValueType& operator*() const noexcept
{
return _type.internal_at(_current);
}
TJValueType& operator*() noexcept
{
return _type.internal_at(_current);
}
base_iterator& operator++() noexcept
{
++_current;
if (_current >= _size)
{
_current = _size;
}
return *this;
}
base_iterator& operator--() noexcept
{
--_current;
if (_current < 0)
{
_current = 0;
}
return *this;
}
bool operator!=(const base_iterator& other) const noexcept
{
return _current != other._current;
}
bool operator==(const base_iterator& other) const noexcept
{
return _current == other._current;
}
};
public:
TJValue(const parse_options& options = {});
TJValue(const TJValue& other);
TJValue(TJValue&& other) noexcept;
TJValue& operator=(const TJValue& other);
TJValue& operator=(TJValue&& other) noexcept;
virtual ~TJValue();
virtual bool is_object() const;
virtual bool is_array() const;
virtual bool is_string() const;
virtual bool is_number() const;
virtual bool is_true() const;
virtual bool is_false() const;
virtual bool is_null() const;
virtual bool is_comment() const;
const TJCHAR* dump(formatting formatting = formatting::indented, const TJCHAR* indent = TJCHARPREFIX(" ")) const;
const TJCHAR* dump_string() const;
/// <summary>
/// Allow each derived class to create a copy of itself.
/// </summary>
/// <returns></returns>
TJValue* clone() const;
virtual void set_parse_options(const parse_options& options);
bool get_boolean() const;
const TJCHAR* get_string() const;
// Non-template overload for ambiguous case - default to long long
inline std::vector<long long> get_numbers() const
{
return get_raw_numbers();
}
inline long long get_number() const
{
return get_raw_number();
}
// Non-template overload for ambiguous case - default to long double
inline std::vector<long double> get_floats() const
{
return get_raw_floats();
}
inline long double get_float() const
{
return get_raw_float();
}
template<typename T>
std::vector<TJ_TEMPLATE_NUMBER::type>
get_numbers() const
{
auto llVector = get_raw_numbers();
std::vector<T> tVector;
tVector.reserve(llVector.size());
// Transform and move the values
std::transform(std::make_move_iterator(llVector.begin()),
std::make_move_iterator(llVector.end()),
std::back_inserter(tVector),
[](long long value) { return static_cast<T>(value); });
return tVector;
}
template<typename T>
TJ_TEMPLATE_FLOAT::type
get_floats() const
{
auto ldVector = get_raw_floats();
std::vector<T> tVector;
tVector.reserve(ldVector.size());
// Transform and move the values
std::transform(std::make_move_iterator(ldVector.begin()),
std::make_move_iterator(ldVector.end()),
std::back_inserter(tVector),
[](long double value) { return static_cast<T>(value); });
return tVector;
}
template<typename T>
TJ_TEMPLATE_NUMBER::type
get_number() const
{
return static_cast<T>(get_raw_number());
}
template<typename T>
TJ_TEMPLATE_FLOAT::type
get_float() const
{
return static_cast<T>(get_raw_float());
}
// For integral types (excluding bool)
template<typename T>
typename std::enable_if<std::is_integral<T>::value && !std::is_same<T, bool>::value, T>::type
get() const
{
return get_number<T>();
}
// For floating point types
template<typename T>
typename std::enable_if<std::is_floating_point<T>::value, T>::type
get() const
{
return get_float<T>();
}
// For boolean
template<typename T>
typename std::enable_if<std::is_same<T, bool>::value, bool>::type
get() const
{
return get_boolean();
}
// For strings (const TJCHAR*)
template<typename T>
typename std::enable_if<std::is_same<T, const TJCHAR*>::value, const TJCHAR*>::type
get() const
{
return get_string();
}
#if TJ_INCLUDE_STD_STRING == 1
// For std::string
template<typename T>
typename std::enable_if<std::is_same<T, std::string>::value, std::string>::type
get() const
{
const TJCHAR* str = get_string();
return str ? std::string(str) : std::string();
}
#endif
// For vectors
template<typename T>
typename std::enable_if<is_vector<T>::value, T>::type
get() const
{
typedef typename T::value_type V;
return get_vector_internal<V>(std::is_integral<V>());
}
/// <summary>
/// Casting the value to a specific type.
/// it behaves exactly as the get() methods.
/// </summary>
/// <returns></returns>
template<typename T>
auto as() const -> decltype(this->get<T>())
{
return get<T>();
}
/// <summary>
/// Access a member of an object by its key.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
const TJValue& operator[](const TJCHAR* key) const;
#if TJ_INCLUDE_STD_STRING == 1
/// <summary>
/// Access a member of an object by its key.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
const TJValue& operator[](const std::string& key) const;
#endif
public:
static const TJValue& null_value();
private:
template<typename V>
std::vector<V> get_vector_internal(std::true_type) const
{
return get_numbers<V>();
}
template<typename V>
std::vector<V> get_vector_internal(std::false_type) const
{
return get_floats<V>();
}
public:
using iterator = base_iterator<false>;
using const_iterator = base_iterator<true>;
inline iterator begin()
{
return iterator::make_begin(*this);
}
inline iterator end()
{
return iterator::make_end(*this);
}
inline const_iterator begin() const
{
return const_iterator::make_begin(*this);
}
inline const_iterator end() const
{
return const_iterator::make_end(*this);
}
protected:
parse_options _parse_options;
long long get_raw_number() const;
long double get_raw_float() const;
std::vector<long long> get_raw_numbers() const;
std::vector<long double> get_raw_floats() const;
/// <summary>
/// Allow each derived class to create a copy of itself.
/// </summary>
/// <returns></returns>
virtual TJValue* internal_clone() const = 0;
virtual void internal_dump(internal_dump_configuration& configuration, const TJCHAR* current_indent) const = 0;
virtual int internal_size() const;
virtual const TJValue& internal_at(int index) const;
virtual TJValue& internal_at(int index);
private:
mutable TJCHAR* _last_dump;
void free_last_dump() const;
};
// The parser class
class TJ
{
public:
virtual ~TJ() = default;
/// <summary>
/// Return if the given source is valid or not.
/// </summary>
/// <param name="source"></param>
/// <param name="parse_options"></param>
/// <returns></returns>
static bool is_valid(const TJCHAR* source, const parse_options& parse_options = {});
/// <summary>
/// Parse a json string
/// </summary>
/// <param name="source">The source we are trying to parse.</param>
/// <param name="parse_options">The option we want to use when parsing this.</param>
/// <returns></returns>
static TJValue* parse(const TJCHAR* source, const parse_options& parse_options = {});
/// <summary>
/// Parse a json5 string
/// </summary>
/// <param name="source">The source we are trying to parse.</param>
/// <param name="parse_options">The option we want to use when parsing this.</param>
/// <returns></returns>
static TJValue* parse5(const TJCHAR* source, const parse_options& parse_options = {});
/// <summary>
/// Parse a json file
/// </summary>
/// <param name="file_path">The source file we are trying to parse.</param>
/// <param name="parse_options">The option we want to use when parsing this.</param>
/// <returns></returns>
static TJValue* parse_file(const TJCHAR* file_path, const parse_options& parse_options = {});
/// <summary>
/// Write a value to a file.
/// </summary>
/// <param name="file_path">The path of the file.</param>
/// <param name="root">the value we are writing</param>
/// <param name="write_options">The options we will be using to write</param>
/// <returns></returns>
static bool write_file(const TJCHAR* file_path, const TJValue& root, const write_options& write_options = {});
protected:
/// <summary>
/// Internal parsing of a json source
/// We will use the option to throw, (or not).
/// </summary>
/// <param name="source"></param>
/// <param name="parse_options"></param>
/// <returns></returns>
static TJValue* internal_parse(const TJCHAR* source, const parse_options& parse_options);
/// <summary>
/// Write a value to a file.
/// </summary>
/// <param name="file_path">The path of the file.</param>
/// <param name="root">the value we are writing</param>
/// <param name="write_options">The options we will be using to write</param>
/// <returns></returns>
static bool internal_write_file(const TJCHAR* file_path, const TJValue& root, const write_options& write_options);
private:
TJ() = delete;
TJ(TJ&&) = delete;
TJ(const TJ&) = delete;
TJ& operator=(const TJ&) = delete;
TJ& operator=(TJ&&) = delete;
};
// A TJMember is a key value pair, (name/value), that belong to an object.
class TJMember
{
friend TJHelper;
friend TJValueObject;
public:
TJMember(const TJCHAR* string, const TJValue* value, const parse_options& options = {});
TJMember(const TJMember& src);
TJMember(TJMember&& src) noexcept;
TJMember& operator=(const TJMember& src);
TJMember& operator=(TJMember&& src) noexcept;
virtual ~TJMember();
const TJCHAR* name() const;
const TJValue* value() const;
TJValue* value();
/// <summary>
/// Casting the value to a specific type.
/// it behaves exactly as the get() methods.
/// </summary>
/// <returns></returns>
template<typename T>
auto as() const -> decltype(this->value()->template as<T>())
{
return value()->template as<T>();
}
static const TJMember& null_member();
protected:
/// <summary>
/// Move the value ownership to the member.
/// The caller will _not_ delete!
/// </summary>
/// <param name="string"></param>
/// <param name="value"></param>
/// <returns></returns>
static TJMember* move(TJCHAR*& string, TJValue*& value, const parse_options& options = {});
/// <summary>
/// Move a value to the member
/// </summary>
void move_value(TJValue*& value);
private:
TJCHAR* _string;
TJValue* _value;
parse_options _parse_options;
void free_string();
void free_value();
};
class TJNumberedValues
{
protected:
TJNumberedValues() noexcept : _number_of_items(-1)
{
}
TJNumberedValues(const TJNumberedValues& other) noexcept : _number_of_items(other._number_of_items)
{
}
TJNumberedValues(TJNumberedValues&& other) noexcept : _number_of_items(other._number_of_items)
{
other._number_of_items = 0;
}
TJNumberedValues& operator=(const TJNumberedValues& other) noexcept
{
if (this != &other)
{
_number_of_items = other._number_of_items;
}
return *this;
}
TJNumberedValues& operator=(TJNumberedValues&& other) noexcept
{
if (this != &other)
{
_number_of_items = other._number_of_items;
other._number_of_items = 0;
}
return *this;
}
int get_number_of_items() const noexcept {
return _number_of_items;
};
void reset_number_of_items() noexcept {
_number_of_items = -1;
}
void set_number_of_items(int number_of_items) const noexcept {
_number_of_items = number_of_items;
}
private:
mutable int _number_of_items;
};
// A Json object that contain an array of key/value pairs.
class TJValueObject : public TJValue, protected TJNumberedValues
{
friend TJHelper;
public:
TJValueObject(const parse_options& options = {});
TJValueObject(const TJValueObject& other);
TJValueObject(TJValueObject&& other) noexcept;
TJValueObject& operator=(const TJValueObject& other);
TJValueObject& operator=(TJValueObject&& other) noexcept;
virtual ~TJValueObject();
void set_parse_options(const parse_options& options) override;
/// <summary>
/// Get the number of items in this array
/// </summary>
/// <returns></returns>
unsigned int get_number_of_items() const;
/// <summary>
/// Get the number of elements in this object (including comments)
/// </summary>
/// <returns></returns>
unsigned int get_number_of_elements() const;
/// <summary>
/// Try and get a string value, if it does not exist, then we return null.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
const TJCHAR* try_get_string(const TJCHAR* key, bool case_sensitive = true) const;
#if TJ_INCLUDE_STD_STRING == 1
/// <summary>
/// Try and get a string value, if it does not exist, then we return null.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
inline const TJCHAR* try_get_string(const std::string& key, bool case_sensitive = true) const
{
return try_get_string(key.c_str(), case_sensitive);
}
#endif
/// <summary>
/// Try and get the value of this member if it exists.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
virtual const TJValue* try_get_value(const TJCHAR* key, bool case_sensitive = true) const;
/// <summary>
/// Check if a key exists in this object.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
bool has_key(const TJCHAR* key, bool case_sensitive = true) const;
#if TJ_INCLUDE_STD_STRING == 1
/// <summary>
/// Check if a key exists in this object.
/// </summary>
/// <param name="key"></param>
/// <returns></returns>
inline bool has_key(const std::string& key, bool case_sensitive = true) const
{