Newer
Older
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
break;
}
default:
{
JSON_THROW(
std::domain_error("cannot use construct with iterators from " +
first.m_object->type_name()));
}
}
assert_invariant();
}
/*!
@brief construct a JSON value given an input stream
@param[in,out] i stream to read a serialized JSON value from
@param[in] cb a parser callback function of type @ref parser_callback_t
which is used to control the deserialization by filtering unwanted values
(optional)
@complexity Linear in the length of the input. The parser is a predictive
LL(1) parser. The complexity can be higher if the parser callback function
@a cb has a super-linear complexity.
@note A UTF-8 byte order mark is silently ignored.
@deprecated This constructor is deprecated and will be removed in version
3.0.0 to unify the interface of the library. Deserialization will be
done by stream operators or by calling one of the `parse` functions,
e.g. @ref parse(std::istream&, const parser_callback_t). That is, calls
like `json j(i);` for an input stream @a i need to be replaced by
`json j = json::parse(i);`. See the example below.
@liveexample{The example below demonstrates constructing a JSON value from
a `std::stringstream` with and without callback
function.,basic_json__istream}
@since version 2.0.0, deprecated in version 2.0.3, to be removed in
version 3.0.0
*/
JSON_DEPRECATED
explicit basic_json(std::istream &i, const parser_callback_t cb = nullptr)
{
*this = parser(i, cb).parse();
assert_invariant();
}
///////////////////////////////////////
// other constructors and destructor //
///////////////////////////////////////
/*!
@brief copy constructor
Creates a copy of a given JSON value.
@param[in] other the JSON value to copy
@complexity Linear in the size of @a other.
@requirement This function helps `basic_json` satisfying the
[Container](http://en.cppreference.com/w/cpp/concept/Container)
requirements:
- The complexity is linear.
- As postcondition, it holds: `other == basic_json(other)`.
@throw std::bad_alloc if allocation for object, array, or string fails.
@liveexample{The following code shows an example for the copy
constructor.,basic_json__basic_json}
@since version 1.0.0
*/
basic_json(const basic_json &other) : m_type(other.m_type)
{
// check of passed value is valid
other.assert_invariant();
switch (m_type)
{
case value_t::object:
{
m_value = *other.m_value.object;
break;
}
case value_t::array:
{
m_value = *other.m_value.array;
break;
}
case value_t::string:
{
m_value = *other.m_value.string;
break;
}
case value_t::boolean:
{
m_value = other.m_value.boolean;
break;
}
case value_t::number_integer:
{
m_value = other.m_value.number_integer;
break;
}
case value_t::number_unsigned:
{
m_value = other.m_value.number_unsigned;
break;
}
case value_t::number_float:
{
m_value = other.m_value.number_float;
break;
}
default:
{
break;
}
}
assert_invariant();
}
/*!
@brief move constructor
Move constructor. Constructs a JSON value with the contents of the given
value @a other using move semantics. It "steals" the resources from @a
other and leaves it as JSON null value.
@param[in,out] other value to move to this object
@post @a other is a JSON null value
@complexity Constant.
@liveexample{The code below shows the move constructor explicitly called
via std::move.,basic_json__moveconstructor}
@since version 1.0.0
*/
basic_json(basic_json &&other) noexcept : m_type(std::move(other.m_type)),
m_value(std::move(other.m_value))
{
// check that passed value is valid
other.assert_invariant();
// invalidate payload
other.m_type = value_t::null;
other.m_value = {};
assert_invariant();
}
/*!
@brief copy assignment
Copy assignment operator. Copies a JSON value via the "copy and swap"
strategy: It is expressed in terms of the copy constructor, destructor,
and the swap() member function.
@param[in] other value to copy from
@complexity Linear.
@requirement This function helps `basic_json` satisfying the
[Container](http://en.cppreference.com/w/cpp/concept/Container)
requirements:
- The complexity is linear.
@liveexample{The code below shows and example for the copy assignment. It
creates a copy of value `a` which is then swapped with `b`. Finally\, the
copy of `a` (which is the null value after the swap) is
destroyed.,basic_json__copyassignment}
@since version 1.0.0
*/
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)
{
// check that passed value is valid
other.assert_invariant();
using std::swap;
swap(m_type, other.m_type);
swap(m_value, other.m_value);
assert_invariant();
return *this;
}
/*!
@brief destructor
Destroys the JSON value and frees all allocated memory.
@requirement This function helps `basic_json` satisfying the
[Container](http://en.cppreference.com/w/cpp/concept/Container)
requirements:
- The complexity is linear.
- All stored elements are destroyed and all memory is freed.
@since version 1.0.0
*/
~basic_json()
{
assert_invariant();
switch (m_type)
{
case value_t::object:
{
AllocatorType<object_t> alloc;
alloc.destroy(m_value.object);
alloc.deallocate(m_value.object, 1);
break;
}
case value_t::array:
{
AllocatorType<array_t> alloc;
alloc.destroy(m_value.array);
alloc.deallocate(m_value.array, 1);
break;
}
case value_t::string:
{
AllocatorType<string_t> alloc;
alloc.destroy(m_value.string);
alloc.deallocate(m_value.string, 1);
break;
}
default:
{
// all other types need no specific destructor
break;
}
public:
///////////////////////
// object inspection //
///////////////////////
/// @name object inspection
/// Functions to inspect the type of a JSON value.
/// @{
Serialization function for JSON values. The function tries to mimic
Python's `json.dumps()` function, and currently supports its @a indent
parameter.
@param[in] indent If 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.
@return string containing the serialization of the JSON value
@liveexample{The following example shows the effect of different @a indent
parameters to the result of the serialization.,dump}
@see https://docs.python.org/2/library/json.html#json.dump
@since version 1.0.0
*/
string_t dump(const int indent = -1) const
{
std::stringstream ss;
// fix locale problems
ss.imbue(std::locale::classic());
// 6, 15 or 16 digits of precision allows round-trip IEEE 754
// string->float->string, string->double->string or string->long
// double->string; to be safe, we read this value from
// std::numeric_limits<number_float_t>::digits10
ss.precision(std::numeric_limits<double>::digits10);
if (indent >= 0)
{
dump(ss, true, static_cast<unsigned int>(indent));
}
else
{
dump(ss, false, 0);
}
/*!
@brief return the type of the JSON value (explicit)
Return the type of the JSON value as a value from the @ref value_t
enumeration.
@exceptionsafety No-throw guarantee: this member function never throws
exceptions.
@liveexample{The following code exemplifies `type()` for all JSON
types.,type}
@since version 1.0.0
*/
constexpr value_t type() const noexcept { return m_type; }
/*!
@brief return whether type is primitive
This function returns true iff the JSON type is primitive (string, number,
boolean, or null).
@return `true` if type is primitive (string, number, boolean, or null),
`false` otherwise.
@exceptionsafety No-throw guarantee: this member function never throws
exceptions.
@liveexample{The following code exemplifies `is_primitive()` for all JSON
types.,is_primitive}
@sa @ref is_structured() -- returns whether JSON value is structured
@sa @ref is_null() -- returns whether JSON value is `null`
@sa @ref is_string() -- returns whether JSON value is a string
@sa @ref is_boolean() -- returns whether JSON value is a boolean
@sa @ref is_number() -- returns whether JSON value is a number
@since version 1.0.0
*/
constexpr bool is_primitive() const noexcept
{
return is_null() or is_string() or is_boolean() or is_number();
}
/*!
@brief return whether type is structured
This function returns true iff the JSON type is structured (array or
object).
@return `true` if type is structured (array or object), `false` otherwise.
@exceptionsafety No-throw guarantee: this member function never throws
exceptions.
@liveexample{The following code exemplifies `is_structured()` for all JSON
types.,is_structured}
@sa @ref is_primitive() -- returns whether value is primitive
@sa @ref is_array() -- returns whether value is an array
@sa @ref is_object() -- returns whether value is an object
@since version 1.0.0
*/
constexpr bool is_structured() const noexcept
{
return is_array() or is_object();
}
/*!
@brief return whether value is null
This function returns true iff the JSON value is null.
@return `true` if type is null, `false` otherwise.
@exceptionsafety No-throw guarantee: this member function never throws
exceptions.
@liveexample{The following code exemplifies `is_null()` for all JSON
types.,is_null}
@since version 1.0.0
*/
constexpr bool is_null() const noexcept { return m_type == value_t::null; }
/*!
@brief return whether value is a boolean
This function returns true iff the JSON value is a boolean.
@return `true` if type is boolean, `false` otherwise.
@exceptionsafety No-throw guarantee: this member function never throws
exceptions.
@liveexample{The following code exemplifies `is_boolean()` for all JSON
types.,is_boolean}
@since version 1.0.0
*/
constexpr bool is_boolean() const noexcept
{
return m_type == value_t::boolean;
}
/*!
@brief return whether value is a number
This function returns true iff the JSON value is a number. This includes
both integer and floating-point values.
@return `true` if type is number (regardless whether integer, unsigned
integer or floating-type), `false` otherwise.
@exceptionsafety No-throw guarantee: this member function never throws
exceptions.
@liveexample{The following code exemplifies `is_number()` for all JSON
types.,is_number}
@sa @ref is_number_integer() -- check if value is an integer or unsigned
integer number
@sa @ref is_number_unsigned() -- check if value is an unsigned integer
number
@sa @ref is_number_float() -- check if value is a floating-point number
@since version 1.0.0
*/
constexpr bool is_number() const noexcept
{
return is_number_integer() or is_number_float();
}
/*!
@brief return whether value is an integer number
This function returns true iff the JSON value is an integer or unsigned
integer number. This excludes floating-point values.
@return `true` if type is an integer or unsigned integer number, `false`
otherwise.
@exceptionsafety No-throw guarantee: this member function never throws
exceptions.
@liveexample{The following code exemplifies `is_number_integer()` for all
JSON types.,is_number_integer}
@sa @ref is_number() -- check if value is a number
@sa @ref is_number_unsigned() -- check if value is an unsigned integer
number
@sa @ref is_number_float() -- check if value is a floating-point number
@since version 1.0.0
*/
constexpr bool is_number_integer() const noexcept
{
return m_type == value_t::number_integer or
m_type == value_t::number_unsigned;
}
/*!
@brief return whether value is an unsigned integer number
This function returns true iff the JSON value is an unsigned integer
number. This excludes floating-point and (signed) integer values.
@return `true` if type is an unsigned integer number, `false` otherwise.
@exceptionsafety No-throw guarantee: this member function never throws
exceptions.
@liveexample{The following code exemplifies `is_number_unsigned()` for all
JSON types.,is_number_unsigned}
@sa @ref is_number() -- check if value is a number
@sa @ref is_number_integer() -- check if value is an integer or unsigned
integer number
@sa @ref is_number_float() -- check if value is a floating-point number
@since version 2.0.0
*/
constexpr bool is_number_unsigned() const noexcept
{
return m_type == value_t::number_unsigned;
}
/*!
@brief return whether value is a floating-point number
This function returns true iff the JSON value is a floating-point number.
This excludes integer and unsigned integer values.
@return `true` if type is a floating-point number, `false` otherwise.
@exceptionsafety No-throw guarantee: this member function never throws
exceptions.
@liveexample{The following code exemplifies `is_number_float()` for all
JSON types.,is_number_float}
@sa @ref is_number() -- check if value is number
@sa @ref is_number_integer() -- check if value is an integer number
@sa @ref is_number_unsigned() -- check if value is an unsigned integer
number
@since version 1.0.0
*/
constexpr bool is_number_float() const noexcept
{
return m_type == value_t::number_float;
}
/*!
@brief return whether value is an object
This function returns true iff the JSON value is an object.
@return `true` if type is object, `false` otherwise.
@exceptionsafety No-throw guarantee: this member function never throws
exceptions.
@liveexample{The following code exemplifies `is_object()` for all JSON
types.,is_object}
@since version 1.0.0
*/
constexpr bool is_object() const noexcept
{
return m_type == value_t::object;
}
/*!
@brief return whether value is an array
This function returns true iff the JSON value is an array.
@return `true` if type is array, `false` otherwise.
@exceptionsafety No-throw guarantee: this member function never throws
exceptions.
@liveexample{The following code exemplifies `is_array()` for all JSON
types.,is_array}
@since version 1.0.0
*/
constexpr bool is_array() const noexcept { return m_type == value_t::array; }
/*!
@brief return whether value is a string
This function returns true iff the JSON value is a string.
@return `true` if type is string, `false` otherwise.
@exceptionsafety No-throw guarantee: this member function never throws
exceptions.
@liveexample{The following code exemplifies `is_string()` for all JSON
types.,is_string}
@since version 1.0.0
*/
constexpr bool is_string() const noexcept
{
return m_type == value_t::string;
}
/*!
@brief return whether value is discarded
This function returns true iff the JSON value was discarded during parsing
with a callback function (see @ref parser_callback_t).
@note This function will always be `false` for JSON values after parsing.
That is, discarded values can only occur during parsing, but will be
removed when inside a structured value or replaced by null in other cases.
@return `true` if type is discarded, `false` otherwise.
@exceptionsafety No-throw guarantee: this member function never throws
exceptions.
@liveexample{The following code exemplifies `is_discarded()` for all JSON
types.,is_discarded}
@since version 1.0.0
*/
constexpr bool is_discarded() const noexcept
{
return m_type == value_t::discarded;
}
/*!
@brief return the type of the JSON value (implicit)
Implicitly return the type of the JSON value as a value from the @ref
value_t enumeration.
@exceptionsafety No-throw guarantee: this member function never throws
exceptions.
@liveexample{The following code exemplifies the @ref value_t operator for
all JSON types.,operator__value_t}
@since version 1.0.0
*/
constexpr operator value_t() const noexcept { return m_type; }
private:
//////////////////
// value access //
//////////////////
/// 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_t, typename T::mapped_type>::value,
int>::type = 0>
T get_impl(T * /*unused*/) const
{
if (is_object())
{
return T(m_value.object->begin(), m_value.object->end());
}
JSON_THROW(std::domain_error("type must be object, but is " + type_name()));
}
/// get an object (explicit)
object_t get_impl(object_t * /*unused*/) const
{
if (is_object())
{
return *(m_value.object);
}
JSON_THROW(std::domain_error("type must be object, but is " + type_name()));
}
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
/// get an array (explicit)
template <
class T,
typename std::enable_if<
std::is_convertible<basic_json_t, typename T::value_type>::value and
not std::is_same<basic_json_t, 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>
T get_impl(T * /*unused*/) const
{
if (is_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;
}
JSON_THROW(std::domain_error("type must be array, but is " + type_name()));
}
/// get an array (explicit)
template <class T, typename std::enable_if<
std::is_convertible<basic_json_t, T>::value and
not std::is_same<basic_json_t, T>::value,
int>::type = 0>
std::vector<T> get_impl(std::vector<T> * /*unused*/) const
{
if (is_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;
}
JSON_THROW(std::domain_error("type must be array, but is " + type_name()));
}
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
/// 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>
T get_impl(T * /*unused*/) const
{
if (is_array())
{
return T(m_value.array->begin(), m_value.array->end());
}
JSON_THROW(std::domain_error("type must be array, but is " + type_name()));
}
/// get an array (explicit)
array_t get_impl(array_t * /*unused*/) const
{
if (is_array())
{
return *(m_value.array);
}
JSON_THROW(std::domain_error("type must be array, but is " + type_name()));
}
/// get a string (explicit)
template <typename T,
typename std::enable_if<std::is_convertible<string_t, T>::value,
int>::type = 0>
T get_impl(T * /*unused*/) const
{
if (is_string())
{
return *m_value.string;
}
JSON_THROW(std::domain_error("type must be string, but is " + type_name()));
}
/// get a number (explicit)
template <typename T, typename std::enable_if<std::is_arithmetic<T>::value,
int>::type = 0>
T get_impl(T * /*unused*/) const
{
switch (m_type)
{
case value_t::number_integer:
{
return static_cast<T>(m_value.number_integer);
}
case value_t::number_unsigned:
{
return static_cast<T>(m_value.number_unsigned);
}
case value_t::number_float:
{
return static_cast<T>(m_value.number_float);
}
default:
{
JSON_THROW(
std::domain_error("type must be number, but is " + type_name()));
}
}
}
/// get a boolean (explicit)
boolean_t get_impl(boolean_t * /*unused*/) const
{
if (is_boolean())
{
return m_value.boolean;
}
else
{
JSON_THROW(
std::domain_error("type must be boolean, but is " + type_name()));
}
}
/// get a pointer to the value (object)
object_t *get_impl_ptr(object_t * /*unused*/) noexcept
{
return is_object() ? m_value.object : nullptr;
}
/// get a pointer to the value (object)
constexpr const object_t *get_impl_ptr(const object_t * /*unused*/) const
noexcept
{
return is_object() ? m_value.object : nullptr;
}
/// get a pointer to the value (array)
array_t *get_impl_ptr(array_t * /*unused*/) noexcept
{
return is_array() ? m_value.array : nullptr;
}
/// get a pointer to the value (array)
constexpr const array_t *get_impl_ptr(const array_t * /*unused*/) const
noexcept
{
return is_array() ? m_value.array : nullptr;
}
/// get a pointer to the value (string)
string_t *get_impl_ptr(string_t * /*unused*/) noexcept
{
return is_string() ? m_value.string : nullptr;
}
/// get a pointer to the value (string)
constexpr const string_t *get_impl_ptr(const string_t * /*unused*/) const
noexcept
{
return is_string() ? m_value.string : nullptr;
}
/// get a pointer to the value (boolean)
boolean_t *get_impl_ptr(boolean_t * /*unused*/) noexcept
{
return is_boolean() ? &m_value.boolean : nullptr;
}
/// get a pointer to the value (boolean)
constexpr const boolean_t *get_impl_ptr(const boolean_t * /*unused*/) const
noexcept
{
return is_boolean() ? &m_value.boolean : nullptr;
}
/// get a pointer to the value (integer number)
number_integer_t *get_impl_ptr(number_integer_t * /*unused*/) noexcept
{
return is_number_integer() ? &m_value.number_integer : nullptr;
}
/// get a pointer to the value (integer number)
constexpr const number_integer_t *
get_impl_ptr(const number_integer_t * /*unused*/) const noexcept
{
return is_number_integer() ? &m_value.number_integer : nullptr;
}
/// get a pointer to the value (unsigned number)
number_unsigned_t *get_impl_ptr(number_unsigned_t * /*unused*/) noexcept
{
return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
}
/// get a pointer to the value (unsigned number)
constexpr const number_unsigned_t *
get_impl_ptr(const number_unsigned_t * /*unused*/) const noexcept
{
return is_number_unsigned() ? &m_value.number_unsigned : nullptr;
}
/// get a pointer to the value (floating-point number)
number_float_t *get_impl_ptr(number_float_t * /*unused*/) noexcept
{
return is_number_float() ? &m_value.number_float : nullptr;
}
/// get a pointer to the value (floating-point number)
constexpr const number_float_t *
get_impl_ptr(const number_float_t * /*unused*/) const noexcept
{
return is_number_float() ? &m_value.number_float : nullptr;
}
/*!
@brief helper function to implement get_ref()
This funcion helps to implement get_ref() without code duplication for
const and non-const overloads
@tparam ThisType will be deduced as `basic_json` or `const basic_json`
@throw std::domain_error if ReferenceType does not match underlying value
type of the current JSON
*/
template <typename ReferenceType, typename ThisType>
static ReferenceType get_ref_impl(ThisType &obj)
{
// helper type
using PointerType = typename std::add_pointer<ReferenceType>::type;
// delegate the call to get_ptr<>()
auto ptr = obj.template get_ptr<PointerType>();
if (ptr != nullptr)
{
return *ptr;
}
throw std::domain_error(
"incompatible ReferenceType for get_ref, actual type is " +
obj.type_name());
}
public:
/// @name value access
/// Direct access to the stored value of a JSON value.
/// @{
/*!
@brief get a value (explicit)
Explicit type conversion between the JSON value and a compatible value.
@tparam ValueType non-pointer type compatible to the JSON value, for
instance `int` for JSON integer numbers, `bool` for JSON booleans, or
`std::vector` types for JSON arrays
@return copy of the JSON value, converted to type @a ValueType
@throw std::domain_error in case passed type @a ValueType is incompatible
to JSON; example: `"type must be object, but is null"`
@complexity Linear in the size of the JSON value.
@liveexample{The example below shows several conversions from JSON values
to other types. There a few things to note: (1) Floating-point numbers can
be converted to integers\, (2) A JSON array can be converted to a standard
`std::vector<short>`\, (3) A JSON object can be converted to C++
associative containers such as `std::unordered_map<std::string\,
json>`.,get__ValueType_const}
@internal
The idea of using a casted null pointer to choose the correct
implementation is from <http://stackoverflow.com/a/8315197/266378>.
@endinternal
@sa @ref operator ValueType() const for implicit conversion
@sa @ref get() for pointer-member access
@since version 1.0.0
*/
template <typename ValueType,
typename std::enable_if<not std::is_pointer<ValueType>::value,
int>::type = 0>
ValueType get() const
{
return get_impl(static_cast<ValueType *>(nullptr));
}
/*!
@brief get a pointer value (explicit)
Explicit pointer access to the internally stored JSON value. No copies are
made.
@warning The pointer becomes invalid if the underlying JSON object
changes.