mirror of
https://github.com/nlohmann/json.git
synced 2025-08-07 18:02:57 +03:00
Merge pull request #2576 from AnthonyVH/non_default_constructable_stl_containers
Add support for deserialization of STL containers of non-default constructable types (fixes #2574).
This commit is contained in:
@@ -2920,50 +2920,53 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
/// @{
|
||||
|
||||
/*!
|
||||
@brief get special-case overload
|
||||
@brief get a pointer value (implicit)
|
||||
|
||||
This overloads avoids a lot of template boilerplate, it can be seen as the
|
||||
identity method
|
||||
Implicit pointer access to the internally stored JSON value. No copies are
|
||||
made.
|
||||
|
||||
@tparam BasicJsonType == @ref basic_json
|
||||
@warning Writing data to the pointee of the result yields an undefined
|
||||
state.
|
||||
|
||||
@return a copy of *this
|
||||
@tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
|
||||
object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
|
||||
@ref number_unsigned_t, or @ref number_float_t. Enforced by a static
|
||||
assertion.
|
||||
|
||||
@return pointer to the internally stored JSON value if the requested
|
||||
pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
|
||||
|
||||
@complexity Constant.
|
||||
|
||||
@since version 2.1.0
|
||||
@liveexample{The example below shows how pointers to internal values of a
|
||||
JSON value can be requested. Note that no type conversions are made and a
|
||||
`nullptr` is returned if the value and the requested pointer type does not
|
||||
match.,get_ptr}
|
||||
|
||||
@since version 1.0.0
|
||||
*/
|
||||
template<typename BasicJsonType, detail::enable_if_t<
|
||||
std::is_same<typename std::remove_const<BasicJsonType>::type, basic_json_t>::value,
|
||||
int> = 0>
|
||||
basic_json get() const
|
||||
template<typename PointerType, typename std::enable_if<
|
||||
std::is_pointer<PointerType>::value, int>::type = 0>
|
||||
auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
|
||||
{
|
||||
return *this;
|
||||
// delegate the call to get_impl_ptr<>()
|
||||
return get_impl_ptr(static_cast<PointerType>(nullptr));
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get special-case overload
|
||||
|
||||
This overloads converts the current @ref basic_json in a different
|
||||
@ref basic_json type
|
||||
|
||||
@tparam BasicJsonType == @ref basic_json
|
||||
|
||||
@return a copy of *this, converted into @a BasicJsonType
|
||||
|
||||
@complexity Depending on the implementation of the called `from_json()`
|
||||
method.
|
||||
|
||||
@since version 3.2.0
|
||||
@brief get a pointer value (implicit)
|
||||
@copydoc get_ptr()
|
||||
*/
|
||||
template < typename BasicJsonType, detail::enable_if_t <
|
||||
!std::is_same<BasicJsonType, basic_json>::value&&
|
||||
detail::is_basic_json<BasicJsonType>::value, int > = 0 >
|
||||
BasicJsonType get() const
|
||||
template < typename PointerType, typename std::enable_if <
|
||||
std::is_pointer<PointerType>::value&&
|
||||
std::is_const<typename std::remove_pointer<PointerType>::type>::value, int >::type = 0 >
|
||||
constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
|
||||
{
|
||||
return *this;
|
||||
// delegate the call to get_impl_ptr<>() const
|
||||
return get_impl_ptr(static_cast<PointerType>(nullptr));
|
||||
}
|
||||
|
||||
private:
|
||||
/*!
|
||||
@brief get a value (explicit)
|
||||
|
||||
@@ -2987,7 +2990,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
- @ref json_serializer<ValueType> does not have a `from_json()` method of
|
||||
the form `ValueType from_json(const basic_json&)`
|
||||
|
||||
@tparam ValueTypeCV the provided value type
|
||||
@tparam ValueType the returned value type
|
||||
|
||||
@return copy of the JSON value, converted to @a ValueType
|
||||
@@ -3003,23 +3005,14 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
|
||||
@since version 2.1.0
|
||||
*/
|
||||
template < typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>,
|
||||
template < typename ValueType,
|
||||
detail::enable_if_t <
|
||||
!detail::is_basic_json<ValueType>::value &&
|
||||
detail::has_from_json<basic_json_t, ValueType>::value &&
|
||||
!detail::has_non_default_from_json<basic_json_t, ValueType>::value,
|
||||
detail::is_default_constructible<ValueType>::value&&
|
||||
detail::has_from_json<basic_json_t, ValueType>::value,
|
||||
int > = 0 >
|
||||
ValueType get() const noexcept(noexcept(
|
||||
JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
|
||||
ValueType get_impl(detail::priority_tag<0> /*unused*/) const noexcept(noexcept(
|
||||
JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>(), std::declval<ValueType&>())))
|
||||
{
|
||||
// we cannot static_assert on ValueTypeCV being non-const, because
|
||||
// there is support for get<const basic_json_t>(), which is why we
|
||||
// still need the uncvref
|
||||
static_assert(!std::is_reference<ValueTypeCV>::value,
|
||||
"get() cannot be used with reference types, you might want to use get_ref()");
|
||||
static_assert(std::is_default_constructible<ValueType>::value,
|
||||
"types must be DefaultConstructible when used with get()");
|
||||
|
||||
ValueType ret{};
|
||||
JSONSerializer<ValueType>::from_json(*this, ret);
|
||||
return ret;
|
||||
@@ -3036,7 +3029,7 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
|
||||
The function is equivalent to executing
|
||||
@code {.cpp}
|
||||
return JSONSerializer<ValueTypeCV>::from_json(*this);
|
||||
return JSONSerializer<ValueType>::from_json(*this);
|
||||
@endcode
|
||||
|
||||
This overloads is chosen if:
|
||||
@@ -3047,7 +3040,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
@note If @ref json_serializer<ValueType> has both overloads of
|
||||
`from_json()`, this one is chosen.
|
||||
|
||||
@tparam ValueTypeCV the provided value type
|
||||
@tparam ValueType the returned value type
|
||||
|
||||
@return copy of the JSON value, converted to @a ValueType
|
||||
@@ -3056,16 +3048,151 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
|
||||
@since version 2.1.0
|
||||
*/
|
||||
template < typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>,
|
||||
detail::enable_if_t < !std::is_same<basic_json_t, ValueType>::value &&
|
||||
detail::has_non_default_from_json<basic_json_t, ValueType>::value,
|
||||
int > = 0 >
|
||||
ValueType get() const noexcept(noexcept(
|
||||
JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>())))
|
||||
template < typename ValueType,
|
||||
detail::enable_if_t <
|
||||
detail::has_non_default_from_json<basic_json_t, ValueType>::value,
|
||||
int > = 0 >
|
||||
ValueType get_impl(detail::priority_tag<1> /*unused*/) const noexcept(noexcept(
|
||||
JSONSerializer<ValueType>::from_json(std::declval<const basic_json_t&>())))
|
||||
{
|
||||
return JSONSerializer<ValueType>::from_json(*this);
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get special-case overload
|
||||
|
||||
This overloads converts the current @ref basic_json in a different
|
||||
@ref basic_json type
|
||||
|
||||
@tparam BasicJsonType == @ref basic_json
|
||||
|
||||
@return a copy of *this, converted into @a BasicJsonType
|
||||
|
||||
@complexity Depending on the implementation of the called `from_json()`
|
||||
method.
|
||||
|
||||
@since version 3.2.0
|
||||
*/
|
||||
template < typename BasicJsonType,
|
||||
detail::enable_if_t <
|
||||
detail::is_basic_json<BasicJsonType>::value,
|
||||
int > = 0 >
|
||||
BasicJsonType get_impl(detail::priority_tag<2> /*unused*/) const
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get special-case overload
|
||||
|
||||
This overloads avoids a lot of template boilerplate, it can be seen as the
|
||||
identity method
|
||||
|
||||
@tparam BasicJsonType == @ref basic_json
|
||||
|
||||
@return a copy of *this
|
||||
|
||||
@complexity Constant.
|
||||
|
||||
@since version 2.1.0
|
||||
*/
|
||||
template<typename BasicJsonType,
|
||||
detail::enable_if_t<
|
||||
std::is_same<BasicJsonType, basic_json_t>::value,
|
||||
int> = 0>
|
||||
basic_json get_impl(detail::priority_tag<3> /*unused*/) const
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get a pointer value (explicit)
|
||||
@copydoc get()
|
||||
*/
|
||||
template<typename PointerType,
|
||||
detail::enable_if_t<
|
||||
std::is_pointer<PointerType>::value,
|
||||
int> = 0>
|
||||
constexpr auto get_impl(detail::priority_tag<4> /*unused*/) const noexcept
|
||||
-> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
|
||||
{
|
||||
// delegate the call to get_ptr
|
||||
return get_ptr<PointerType>();
|
||||
}
|
||||
|
||||
public:
|
||||
/*!
|
||||
@brief get a (pointer) value (explicit)
|
||||
|
||||
Performs explicit type conversion between the JSON value and a compatible value if required.
|
||||
|
||||
- If the requested type is a pointer to the internally stored JSON value that pointer is returned.
|
||||
No copies are made.
|
||||
|
||||
- If the requested type is the current @ref basic_json, or a different @ref basic_json convertible
|
||||
from the current @ref basic_json.
|
||||
|
||||
- Otherwise the value is converted by calling the @ref json_serializer<ValueType> `from_json()`
|
||||
method.
|
||||
|
||||
@tparam ValueTypeCV the provided value type
|
||||
@tparam ValueType the returned value type
|
||||
|
||||
@return copy of the JSON value, converted to @tparam ValueType if necessary
|
||||
|
||||
@throw what @ref json_serializer<ValueType> `from_json()` method throws if conversion is required
|
||||
|
||||
@since version 2.1.0
|
||||
*/
|
||||
template < typename ValueTypeCV, typename ValueType = detail::uncvref_t<ValueTypeCV>>
|
||||
#if defined(JSON_HAS_CPP_14)
|
||||
constexpr
|
||||
#endif
|
||||
auto get() const noexcept(
|
||||
noexcept(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {})))
|
||||
-> decltype(std::declval<const basic_json_t&>().template get_impl<ValueType>(detail::priority_tag<4> {}))
|
||||
{
|
||||
// we cannot static_assert on ValueTypeCV being non-const, because
|
||||
// there is support for get<const basic_json_t>(), which is why we
|
||||
// still need the uncvref
|
||||
static_assert(!std::is_reference<ValueTypeCV>::value,
|
||||
"get() cannot be used with reference types, you might want to use get_ref()");
|
||||
return JSONSerializer<ValueType>::from_json(*this);
|
||||
return get_impl<ValueType>(detail::priority_tag<4> {});
|
||||
}
|
||||
|
||||
/*!
|
||||
@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.
|
||||
|
||||
@tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
|
||||
object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
|
||||
@ref number_unsigned_t, or @ref number_float_t.
|
||||
|
||||
@return pointer to the internally stored JSON value if the requested
|
||||
pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
|
||||
|
||||
@complexity Constant.
|
||||
|
||||
@liveexample{The example below shows how pointers to internal values of a
|
||||
JSON value can be requested. Note that no type conversions are made and a
|
||||
`nullptr` is returned if the value and the requested pointer type does not
|
||||
match.,get__PointerType}
|
||||
|
||||
@sa see @ref get_ptr() for explicit pointer-member access
|
||||
|
||||
@since version 1.0.0
|
||||
*/
|
||||
template<typename PointerType, typename std::enable_if<
|
||||
std::is_pointer<PointerType>::value, int>::type = 0>
|
||||
auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
|
||||
{
|
||||
// delegate the call to get_ptr
|
||||
return get_ptr<PointerType>();
|
||||
}
|
||||
|
||||
/*!
|
||||
@@ -3138,101 +3265,6 @@ class basic_json // NOLINT(cppcoreguidelines-special-member-functions,hicpp-spec
|
||||
return v;
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
@brief get a pointer value (implicit)
|
||||
|
||||
Implicit pointer access to the internally stored JSON value. No copies are
|
||||
made.
|
||||
|
||||
@warning Writing data to the pointee of the result yields an undefined
|
||||
state.
|
||||
|
||||
@tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
|
||||
object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
|
||||
@ref number_unsigned_t, or @ref number_float_t. Enforced by a static
|
||||
assertion.
|
||||
|
||||
@return pointer to the internally stored JSON value if the requested
|
||||
pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
|
||||
|
||||
@complexity Constant.
|
||||
|
||||
@liveexample{The example below shows how pointers to internal values of a
|
||||
JSON value can be requested. Note that no type conversions are made and a
|
||||
`nullptr` is returned if the value and the requested pointer type does not
|
||||
match.,get_ptr}
|
||||
|
||||
@since version 1.0.0
|
||||
*/
|
||||
template<typename PointerType, typename std::enable_if<
|
||||
std::is_pointer<PointerType>::value, int>::type = 0>
|
||||
auto get_ptr() noexcept -> decltype(std::declval<basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
|
||||
{
|
||||
// delegate the call to get_impl_ptr<>()
|
||||
return get_impl_ptr(static_cast<PointerType>(nullptr));
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get a pointer value (implicit)
|
||||
@copydoc get_ptr()
|
||||
*/
|
||||
template < typename PointerType, typename std::enable_if <
|
||||
std::is_pointer<PointerType>::value&&
|
||||
std::is_const<typename std::remove_pointer<PointerType>::type>::value, int >::type = 0 >
|
||||
constexpr auto get_ptr() const noexcept -> decltype(std::declval<const basic_json_t&>().get_impl_ptr(std::declval<PointerType>()))
|
||||
{
|
||||
// delegate the call to get_impl_ptr<>() const
|
||||
return get_impl_ptr(static_cast<PointerType>(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.
|
||||
|
||||
@tparam PointerType pointer type; must be a pointer to @ref array_t, @ref
|
||||
object_t, @ref string_t, @ref boolean_t, @ref number_integer_t,
|
||||
@ref number_unsigned_t, or @ref number_float_t.
|
||||
|
||||
@return pointer to the internally stored JSON value if the requested
|
||||
pointer type @a PointerType fits to the JSON value; `nullptr` otherwise
|
||||
|
||||
@complexity Constant.
|
||||
|
||||
@liveexample{The example below shows how pointers to internal values of a
|
||||
JSON value can be requested. Note that no type conversions are made and a
|
||||
`nullptr` is returned if the value and the requested pointer type does not
|
||||
match.,get__PointerType}
|
||||
|
||||
@sa see @ref get_ptr() for explicit pointer-member access
|
||||
|
||||
@since version 1.0.0
|
||||
*/
|
||||
template<typename PointerType, typename std::enable_if<
|
||||
std::is_pointer<PointerType>::value, int>::type = 0>
|
||||
auto get() noexcept -> decltype(std::declval<basic_json_t&>().template get_ptr<PointerType>())
|
||||
{
|
||||
// delegate the call to get_ptr
|
||||
return get_ptr<PointerType>();
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get a pointer value (explicit)
|
||||
@copydoc get()
|
||||
*/
|
||||
template<typename PointerType, typename std::enable_if<
|
||||
std::is_pointer<PointerType>::value, int>::type = 0>
|
||||
constexpr auto get() const noexcept -> decltype(std::declval<const basic_json_t&>().template get_ptr<PointerType>())
|
||||
{
|
||||
// delegate the call to get_ptr
|
||||
return get_ptr<PointerType>();
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief get a reference value (implicit)
|
||||
|
||||
|
Reference in New Issue
Block a user