mirror of
https://github.com/nlohmann/json.git
synced 2025-07-29 23:01:16 +03:00
Add support for deserialization of STL containers of non-default constructable types (fixes #2574).
This commit is contained in:
@ -1,14 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
#include <nlohmann/detail/conversions/from_json.hpp>
|
||||
#include <nlohmann/detail/conversions/to_json.hpp>
|
||||
#include <nlohmann/detail/meta/type_traits.hpp>
|
||||
|
||||
namespace nlohmann
|
||||
{
|
||||
|
||||
template<typename, typename>
|
||||
template<typename ValueType, typename>
|
||||
struct adl_serializer
|
||||
{
|
||||
/*!
|
||||
@ -20,14 +22,22 @@ struct adl_serializer
|
||||
@param[in] j JSON value to read from
|
||||
@param[in,out] val value to write to
|
||||
*/
|
||||
template<typename BasicJsonType, typename ValueType>
|
||||
static auto from_json(BasicJsonType&& j, ValueType& val) noexcept(
|
||||
template<typename BasicJsonType, typename U = ValueType>
|
||||
static auto from_json(BasicJsonType && j, U& val) noexcept(
|
||||
noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), val)))
|
||||
-> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), val), void())
|
||||
{
|
||||
::nlohmann::from_json(std::forward<BasicJsonType>(j), val);
|
||||
}
|
||||
|
||||
template<typename BasicJsonType, typename U = ValueType>
|
||||
static auto from_json(BasicJsonType && j) noexcept(
|
||||
noexcept(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::tag<U> {})))
|
||||
-> decltype(::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::tag<U> {}))
|
||||
{
|
||||
return ::nlohmann::from_json(std::forward<BasicJsonType>(j), detail::tag<U> {});
|
||||
}
|
||||
|
||||
/*!
|
||||
@brief convert any value type to a JSON value
|
||||
|
||||
@ -37,13 +47,12 @@ struct adl_serializer
|
||||
@param[in,out] j JSON value to write to
|
||||
@param[in] val value to read from
|
||||
*/
|
||||
template<typename BasicJsonType, typename ValueType>
|
||||
static auto to_json(BasicJsonType& j, ValueType&& val) noexcept(
|
||||
noexcept(::nlohmann::to_json(j, std::forward<ValueType>(val))))
|
||||
-> decltype(::nlohmann::to_json(j, std::forward<ValueType>(val)), void())
|
||||
template<typename BasicJsonType, typename U = ValueType>
|
||||
static auto to_json(BasicJsonType& j, U && val) noexcept(
|
||||
noexcept(::nlohmann::to_json(j, std::forward<U>(val))))
|
||||
-> decltype(::nlohmann::to_json(j, std::forward<U>(val)), void())
|
||||
{
|
||||
::nlohmann::to_json(j, std::forward<ValueType>(val));
|
||||
::nlohmann::to_json(j, std::forward<U>(val));
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace nlohmann
|
||||
|
Reference in New Issue
Block a user