-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathjson.hpp
4893 lines (4342 loc) · 146 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
/*!
@file
@copyright The code is licensed under the MIT License
<http://opensource.org/licenses/MIT>,
Copyright (c) 2013-2015 Niels Lohmann.
@author Niels Lohmann <http://nlohmann.me>
@see https://github.com/nlohmann/json
*/
#ifndef NLOHMANN_JSON_HPP
#define NLOHMANN_JSON_HPP
#include <algorithm>
#include <array>
#include <ciso646>
#include <cmath>
#include <cstdio>
#include <functional>
#include <initializer_list>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <limits>
#include <map>
#include <memory>
#include <sstream>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
/*!
@brief namespace for Niels Lohmann
@see https://github.com/nlohmann
*/
namespace nlohmann
{
// Helper to determine whether there's a key_type for T.
// http://stackoverflow.com/a/7728728/266378
template<typename T>
struct has_mapped_type
{
private:
template<typename C> static char test(typename C::mapped_type*);
template<typename C> static int test(...);
public:
enum { value = sizeof(test<T>(0)) == sizeof(char) };
};
/*!
@brief JSON
@tparam ObjectType type for JSON objects
(@c std::map by default)
@tparam ArrayType type for JSON arrays
(@c std::vector by default)
@tparam StringType type for JSON strings and object keys
(@c std::string by default)
@tparam BooleanType type for JSON booleans
(@c bool by default)
@tparam NumberIntegerType type for JSON integer numbers
(@c int64_t by default)
@tparam NumberFloatType type for JSON floating-point numbers
(@c double by default)
@tparam AllocatorType type of the allocator to use
(@c std::allocator by default)
@note ObjectType trick from http://stackoverflow.com/a/9860911
@see RFC 7159 <http://rfc7159.net/rfc7159>
@see ECMA 404 <http://www.ecma-international.org/publications/standards/Ecma-404.htm>
*/
template <
template<typename U, typename V, typename... Args> class ObjectType = std::map,
template<typename U, typename... Args> class ArrayType = std::vector,
class StringType = std::string,
class BooleanType = bool,
class NumberIntegerType = int64_t,
class NumberFloatType = double,
template<typename U> class AllocatorType = std::allocator
>
class basic_json
{
public:
/////////////////////
// container types //
/////////////////////
/// the type of elements in a basic_json container
using value_type = basic_json;
/// the type of an element reference
using reference = value_type&;
/// the type of an element const reference
using const_reference = const value_type&;
/// a type to represent differences between iterators
using difference_type = std::ptrdiff_t;
/// a type to represent container sizes
using size_type = std::size_t;
/// the allocator type
using allocator_type = AllocatorType<basic_json>;
/// the type of an element pointer
using pointer = typename std::allocator_traits<allocator_type>::pointer;
/// the type of an element const pointer
using const_pointer = typename std::allocator_traits<allocator_type>::const_pointer;
/// an iterator for a basic_json container
class iterator;
/// a const iterator for a basic_json container
class const_iterator;
/// a reverse iterator for a basic_json container
class reverse_iterator;
/// a const reverse iterator for a basic_json container
class const_reverse_iterator;
/// returns the allocator associated with the container
inline static allocator_type get_allocator()
{
return allocator_type();
}
///////////////////////////
// JSON value data types //
///////////////////////////
/// a type for an object
using object_t = ObjectType<StringType, basic_json>;
/// a type for an array
using array_t = ArrayType<basic_json>;
/// a type for a string
using string_t = StringType;
/// a type for a boolean
using boolean_t = BooleanType;
/// a type for a number (integer)
using number_integer_t = NumberIntegerType;
/// a type for a number (floating-point)
using number_float_t = NumberFloatType;
/// a type for list initialization
using list_init_t = std::initializer_list<basic_json>;
////////////////////////
// JSON value storage //
////////////////////////
/// a JSON value
union json_value
{
/// object (stored with pointer to save storage)
object_t* object;
/// array (stored with pointer to save storage)
array_t* array;
/// string (stored with pointer to save storage)
string_t* string;
/// bolean
boolean_t boolean;
/// number (integer)
number_integer_t number_integer;
/// number (floating-point)
number_float_t number_float;
/// default constructor (for null values)
json_value() = default;
/// constructor for booleans
json_value(boolean_t v) : boolean(v) {}
/// constructor for numbers (integer)
json_value(number_integer_t v) : number_integer(v) {}
/// constructor for numbers (floating-point)
json_value(number_float_t v) : number_float(v) {}
};
/////////////////////////////////
// JSON value type enumeration //
/////////////////////////////////
/// JSON value type enumeration
enum class value_t : uint8_t
{
null, ///< null value
object, ///< object (unordered set of name/value pairs)
array, ///< array (ordered collection of values)
string, ///< string value
boolean, ///< boolean value
number_integer, ///< number value (integer)
number_float, ///< number value (floating-point)
discarded ///< (internal) indicates the parser callback chose not to keep the value
};
//////////////////////////
// JSON parser callback //
//////////////////////////
/// JSON callback event enumeration
enum class parse_event_t : uint8_t
{
object_start, ///< start an object scope (found a '{' token)
object_end, ///< end of an object scope (found '}' token)
array_start, ///< start of an array scope (found '[' token)
array_end, ///< end of an array scope (found ']' token)
key, ///< found an object key within an object scope
value ///< a value in an appropriate context (i.e., following a tag in an object scope)
};
/// per-element parser callback type
using parser_callback_t = std::function<bool(int depth, parse_event_t event,
const basic_json& parsed)>;
/// default parser callback returns true to keep all elements
static bool default_callback(int, parse_event_t, const basic_json&)
{
return true;
}
/*!
@brief comparison operator for JSON value types
Returns an ordering that is similar to Python:
- order: null < boolean < number < object < array < string
- furthermore, each type is not smaller than itself
*/
friend bool operator<(const value_t lhs, const value_t rhs)
{
std::array<uint8_t, 7> order = {{
0, // null
3, // object
4, // array
5, // string
1, // boolean
2, // integer
2 // float
}
};
// discarded values are not comparable
if (lhs == value_t::discarded or rhs == value_t::discarded)
{
return false;
}
return order[static_cast<std::size_t>(lhs)] < order[static_cast<std::size_t>(rhs)];
}
//////////////////
// constructors //
//////////////////
/*!
@brief create an empty value with a given type
@param value the type to create an value of
@exception std::bad_alloc if allocation for object, array, or string fails.
*/
inline basic_json(const value_t value)
: m_type(value)
{
switch (m_type)
{
case (value_t::null):
case (value_t::discarded):
{
break;
}
case (value_t::object):
{
AllocatorType<object_t> alloc;
m_value.object = alloc.allocate(1);
alloc.construct(m_value.object);
break;
}
case (value_t::array):
{
AllocatorType<array_t> alloc;
m_value.array = alloc.allocate(1);
alloc.construct(m_value.array);
break;
}
case (value_t::string):
{
AllocatorType<string_t> alloc;
m_value.string = alloc.allocate(1);
alloc.construct(m_value.string, "");
break;
}
case (value_t::boolean):
{
m_value.boolean = boolean_t(false);
break;
}
case (value_t::number_integer):
{
m_value.number_integer = number_integer_t(0);
break;
}
case (value_t::number_float):
{
m_value.number_float = number_float_t(0.0);
break;
}
}
}
/*!
@brief create a null object (implicitly)
@ingroup container
*/
inline basic_json() noexcept = default;
/// create a null object (explicitly)
inline basic_json(std::nullptr_t) noexcept
: m_type(value_t::null)
{}
/// create an object (explicit)
inline basic_json(const object_t& value)
: m_type(value_t::object)
{
AllocatorType<object_t> alloc;
m_value.object = alloc.allocate(1);
alloc.construct(m_value.object, value);
}
/// create an object (implicit)
template <class V, typename
std::enable_if<
std::is_constructible<typename object_t::key_type, typename V::key_type>::value and
std::is_constructible<basic_json, typename V::mapped_type>::value, int>::type
= 0>
inline basic_json(const V& value)
: m_type(value_t::object)
{
AllocatorType<object_t> alloc;
m_value.object = alloc.allocate(1);
using std::begin;
using std::end;
alloc.construct(m_value.object, begin(value), end(value));
}
/// create an array (explicit)
inline basic_json(const array_t& value)
: m_type(value_t::array)
{
AllocatorType<array_t> alloc;
m_value.array = alloc.allocate(1);
alloc.construct(m_value.array, value);
}
/// create an array (implicit)
template <class V, typename
std::enable_if<
not std::is_same<V, typename basic_json::iterator>::value and
not std::is_same<V, typename basic_json::const_iterator>::value and
not std::is_same<V, typename basic_json::reverse_iterator>::value and
not std::is_same<V, typename basic_json::const_reverse_iterator>::value and
not std::is_same<V, typename array_t::iterator>::value and
not std::is_same<V, typename array_t::const_iterator>::value and
std::is_constructible<basic_json, typename V::value_type>::value, int>::type
= 0>
inline basic_json(const V& value)
: m_type(value_t::array)
{
AllocatorType<array_t> alloc;
m_value.array = alloc.allocate(1);
using std::begin;
using std::end;
alloc.construct(m_value.array, begin(value), end(value));
}
/// create a string (explicit)
inline basic_json(const string_t& value)
: m_type(value_t::string)
{
AllocatorType<string_t> alloc;
m_value.string = alloc.allocate(1);
alloc.construct(m_value.string, value);
}
/// create a string (explicit)
inline basic_json(const typename string_t::value_type* value)
: m_type(value_t::string)
{
AllocatorType<string_t> alloc;
m_value.string = alloc.allocate(1);
alloc.construct(m_value.string, value);
}
/// create a string (implicit)
template <class V, typename
std::enable_if<
std::is_constructible<string_t, V>::value, int>::type
= 0>
inline basic_json(const V& value)
: basic_json(string_t(value))
{}
/// create a boolean (explicit)
inline basic_json(boolean_t value)
: m_type(value_t::boolean), m_value(value)
{}
/// create an integer number (explicit)
inline basic_json(const number_integer_t& value)
: m_type(value_t::number_integer), m_value(value)
{}
/// create an integer number (implicit)
template<typename T, typename
std::enable_if<
std::is_constructible<number_integer_t, T>::value and
std::numeric_limits<T>::is_integer, T>::type
= 0>
inline basic_json(const T value) noexcept
: m_type(value_t::number_integer), m_value(number_integer_t(value))
{}
/// create a floating-point number (explicit)
inline basic_json(const number_float_t& value)
: m_type(value_t::number_float), m_value(value)
{}
/// create a floating-point number (implicit)
template<typename T, typename = typename
std::enable_if<
std::is_constructible<number_float_t, T>::value and
std::is_floating_point<T>::value>::type
>
inline basic_json(const T value) noexcept
: m_type(value_t::number_float), m_value(number_float_t(value))
{}
/// create a container (array or object) from an initializer list
inline basic_json(list_init_t init, bool type_deduction = true,
value_t manual_type = value_t::array)
{
// the initializer list could describe an object
bool is_object = true;
// check if each element is an array with two elements whose first element
// is a string
for (const auto& element : init)
{
if (element.m_type != value_t::array or element.size() != 2
or element[0].m_type != value_t::string)
{
// we found an element that makes it impossible to use the
// initializer list as object
is_object = false;
break;
}
}
// adjust type if type deduction is not wanted
if (not type_deduction)
{
// if array is wanted, do not create an object though possible
if (manual_type == value_t::array)
{
is_object = false;
}
// if object is wanted but impossible, throw an exception
if (manual_type == value_t::object and not is_object)
{
throw std::logic_error("cannot create JSON object from initializer list");
}
}
if (is_object)
{
// the initializer list is a list of pairs -> create object
m_type = value_t::object;
AllocatorType<object_t> alloc;
m_value.object = alloc.allocate(1);
alloc.construct(m_value.object);
for (auto& element : init)
{
m_value.object->emplace(std::move(*(element[0].m_value.string)), std::move(element[1]));
}
}
else
{
// the initializer list describes an array -> create array
m_type = value_t::array;
AllocatorType<array_t> alloc;
m_value.array = alloc.allocate(1);
alloc.construct(m_value.array, std::move(init));
}
}
/// explicitly create an array from an initializer list
inline static basic_json array(list_init_t init = list_init_t())
{
return basic_json(init, false, value_t::array);
}
/// explicitly create an object from an initializer list
inline static basic_json object(list_init_t init = list_init_t())
{
return basic_json(init, false, value_t::object);
}
/// construct an array with count copies of given value
inline basic_json(size_type count, const basic_json& other)
: m_type(value_t::array)
{
AllocatorType<array_t> alloc;
m_value.array = alloc.allocate(1);
alloc.construct(m_value.array, count, other);
}
/// construct a JSON container given an iterator range
template <class T, typename
std::enable_if<
std::is_same<T, typename basic_json::iterator>::value or
std::is_same<T, typename basic_json::const_iterator>::value
, int>::type
= 0>
inline basic_json(T first, T last)
{
// make sure iterator fits the current value
if (first.m_object != last.m_object or
first.m_object->m_type != last.m_object->m_type)
{
throw std::runtime_error("iterators are not compatible");
}
// set the type
m_type = first.m_object->m_type;
// check if iterator range is complete for non-compound values
switch (m_type)
{
case value_t::number_integer:
case value_t::number_float:
case value_t::boolean:
case value_t::string:
{
if (first.m_it.generic_iterator != 0 or last.m_it.generic_iterator != 1)
{
throw std::out_of_range("iterators out of range");
}
break;
}
default:
{
break;
}
}
switch (m_type)
{
case value_t::number_integer:
{
m_value.number_integer = first.m_object->m_value.number_integer;
break;
}
case value_t::number_float:
{
m_value.number_float = first.m_object->m_value.number_float;
break;
}
case value_t::boolean:
{
m_value.boolean = first.m_object->m_value.boolean;
break;
}
case value_t::string:
{
AllocatorType<string_t> alloc;
m_value.string = alloc.allocate(1);
alloc.construct(m_value.string, *first.m_object->m_value.string);
break;
}
case value_t::object:
{
AllocatorType<object_t> alloc;
m_value.object = alloc.allocate(1);
alloc.construct(m_value.object, first.m_it.object_iterator, last.m_it.object_iterator);
break;
}
case value_t::array:
{
AllocatorType<array_t> alloc;
m_value.array = alloc.allocate(1);
alloc.construct(m_value.array, first.m_it.array_iterator, last.m_it.array_iterator);
break;
}
default:
{
throw std::runtime_error("cannot use construct with iterators from " + first.m_object->type_name());
}
}
}
///////////////////////////////////////
// other constructors and destructor //
///////////////////////////////////////
/*!
@brief copy constructor
@exception std::bad_alloc if allocation for object, array, or string fails.
@ingroup container
*/
inline basic_json(const basic_json& other)
: m_type(other.m_type)
{
switch (m_type)
{
case (value_t::null):
case (value_t::discarded):
{
break;
}
case (value_t::object):
{
AllocatorType<object_t> alloc;
m_value.object = alloc.allocate(1);
alloc.construct(m_value.object, *other.m_value.object);
break;
}
case (value_t::array):
{
AllocatorType<array_t> alloc;
m_value.array = alloc.allocate(1);
alloc.construct(m_value.array, *other.m_value.array);
break;
}
case (value_t::string):
{
AllocatorType<string_t> alloc;
m_value.string = alloc.allocate(1);
alloc.construct(m_value.string, *other.m_value.string);
break;
}
case (value_t::boolean):
{
m_value.boolean = other.m_value.boolean;
break;
}
case (value_t::number_integer):
{
m_value.number_integer = other.m_value.number_integer;
break;
}
case (value_t::number_float):
{
m_value.number_float = other.m_value.number_float;
break;
}
}
}
/// move constructor
inline basic_json(basic_json&& other) noexcept
: m_type(std::move(other.m_type)),
m_value(std::move(other.m_value))
{
// invalidate payload
other.m_type = value_t::null;
other.m_value = {};
}
/*!
@brief copy assignment
@ingroup container
*/
inline reference& operator=(basic_json other) noexcept (
std::is_nothrow_move_constructible<value_t>::value and
std::is_nothrow_move_assignable<value_t>::value and
std::is_nothrow_move_constructible<json_value>::value and
std::is_nothrow_move_assignable<json_value>::value
)
{
using std::swap;
std::swap(m_type, other.m_type);
std::swap(m_value, other.m_value);
return *this;
}
/*!
@brief destructor
@ingroup container
*/
inline ~basic_json() noexcept
{
switch (m_type)
{
case (value_t::object):
{
AllocatorType<object_t> alloc;
alloc.destroy(m_value.object);
alloc.deallocate(m_value.object, 1);
m_value.object = nullptr;
break;
}
case (value_t::array):
{
AllocatorType<array_t> alloc;
alloc.destroy(m_value.array);
alloc.deallocate(m_value.array, 1);
m_value.array = nullptr;
break;
}
case (value_t::string):
{
AllocatorType<string_t> alloc;
alloc.destroy(m_value.string);
alloc.deallocate(m_value.string, 1);
m_value.string = nullptr;
break;
}
default:
{
// all other types need no specific destructor
break;
}
}
}
public:
///////////////////////
// object inspection //
///////////////////////
/*!
@brief serialization
Serialization function for JSON objects. The function tries to mimick
Python's @p json.dumps() function, and currently supports its @p indent
parameter.
@param indent sif indent is nonnegative, then array elements and object
members will be pretty-printed with that indent level. An indent level of 0
will only insert newlines. -1 (the default) selects the most compact
representation
@see https://docs.python.org/2/library/json.html#json.dump
*/
inline string_t dump(const int indent = -1) const noexcept
{
if (indent >= 0)
{
return dump(true, static_cast<unsigned int>(indent));
}
else
{
return dump(false, 0);
}
}
/// return the type of the object (explicit)
inline value_t type() const noexcept
{
return m_type;
}
// return whether value is null
inline bool is_null() const noexcept
{
return m_type == value_t::null;
}
// return whether value is boolean
inline bool is_boolean() const noexcept
{
return m_type == value_t::boolean;
}
// return whether value is number
inline bool is_number() const noexcept
{
return (m_type == value_t::number_integer) or (m_type == value_t::number_float);
}
// return whether value is object
inline bool is_object() const noexcept
{
return m_type == value_t::object;
}
// return whether value is array
inline bool is_array() const noexcept
{
return m_type == value_t::array;
}
// return whether value is string
inline bool is_string() const noexcept
{
return m_type == value_t::string;
}
// return whether value is discarded
inline bool is_discarded() const noexcept
{
return m_type == value_t::discarded;
}
/// return the type of the object (implicit)
inline operator value_t() const noexcept
{
return m_type;
}
private:
//////////////////////
// value conversion //
//////////////////////
/// get an object (explicit)
template <class T, typename
std::enable_if<
std::is_convertible<typename object_t::key_type, typename T::key_type>::value and
std::is_convertible<basic_json, typename T::mapped_type>::value
, int>::type = 0>
inline T get_impl(T*) const
{
switch (m_type)
{
case (value_t::object):
{
return T(m_value.object->begin(), m_value.object->end());
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
}
}
}
/// get an object (explicit)
inline object_t get_impl(object_t*) const
{
switch (m_type)
{
case (value_t::object):
{
return *(m_value.object);
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to object");
}
}
}
/// get an array (explicit)
template <class T, typename
std::enable_if<
std::is_convertible<basic_json, typename T::value_type>::value and
not std::is_same<basic_json, typename T::value_type>::value and
not std::is_arithmetic<T>::value and
not std::is_convertible<std::string, T>::value and
not has_mapped_type<T>::value
, int>::type = 0>
inline T get_impl(T*) const
{
switch (m_type)
{
case (value_t::array):
{
T to_vector;
std::transform(m_value.array->begin(), m_value.array->end(),
std::inserter(to_vector, to_vector.end()), [](basic_json i)
{
return i.get<typename T::value_type>();
});
return to_vector;
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
}
}
}
/// get an array (explicit)
template <class T, typename
std::enable_if<
std::is_convertible<basic_json, T>::value and
not std::is_same<basic_json, T>::value
, int>::type = 0>
inline std::vector<T> get_impl(std::vector<T>*) const
{
switch (m_type)
{
case (value_t::array):
{
std::vector<T> to_vector;
to_vector.reserve(m_value.array->size());
std::transform(m_value.array->begin(), m_value.array->end(),
std::inserter(to_vector, to_vector.end()), [](basic_json i)
{
return i.get<T>();
});
return to_vector;
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
}
}
}
/// get an array (explicit)
template <class T, typename
std::enable_if<
std::is_same<basic_json, typename T::value_type>::value and
not has_mapped_type<T>::value
, int>::type = 0>
inline T get_impl(T*) const
{
switch (m_type)
{
case (value_t::array):
{
return T(m_value.array->begin(), m_value.array->end());
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
}
}
}
inline array_t get_impl(array_t*) const
{
switch (m_type)
{
case (value_t::array):
{
return *(m_value.array);
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to array");
}
}
}
/// get a string (explicit)
template <typename T, typename
std::enable_if<
std::is_convertible<string_t, T>::value
, int>::type = 0>
inline T get_impl(T*) const
{
switch (m_type)
{
case (value_t::string):
{
return *m_value.string;
}
default:
{
throw std::logic_error("cannot cast " + type_name() + " to " + typeid(T).name());
}
}
}
/// get a number (explicit)
template<typename T, typename
std::enable_if<
std::is_arithmetic<T>::value
, int>::type = 0>