🚧 conversions for std::optional

This commit is contained in:
Niels Lohmann committed 2019-11-23 00:21:33 +01:00
1 parent c0f52edbc3
commit df30a0ea07
4 files changed
+139 -1

No files matched your search

@@ -13,6 +13,10 @@
#include <utility> // pair, declval
#include <valarray> // valarray
#ifdef JSON_HAS_CPP_17
#include <optional> // optional
#endif
#include <nlohmann/detail/exceptions.hpp>
#include <nlohmann/detail/macro_scope.hpp>
#include <nlohmann/detail/meta/cpp_future.hpp>
@@ -33,6 +37,21 @@ void from_json(const BasicJsonType& j, typename std::nullptr_t& n)
n = nullptr;
}
#ifdef JSON_HAS_CPP_17
template<typename BasicJsonType, typename T>
void from_json(const BasicJsonType& j, std::optional<T>& opt)
{
if (j.is_null())
{
opt = std::nullopt;
}
else
{
opt = j.template get<T>();
}
}
#endif
// overloads for basic_json template parameters
template<typename BasicJsonType, typename ArithmeticType,
enable_if_t<std::is_arithmetic<ArithmeticType>::value and