🚧 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

@@ -10,6 +10,10 @@
#include <valarray> // valarray
#include <vector> // vector
#ifdef JSON_HAS_CPP_17
#include <optional> // optional
#endif
#include <nlohmann/detail/iterators/iteration_proxy.hpp>
#include <nlohmann/detail/meta/cpp_future.hpp>
#include <nlohmann/detail/meta/type_traits.hpp>
@@ -198,6 +202,22 @@ struct external_constructor<value_t::object>
// to_json //
/////////////
#ifdef JSON_HAS_CPP_17
template<typename BasicJsonType, typename T,
enable_if_t<std::is_constructible<BasicJsonType, T>::value, int> = 0>
void to_json(BasicJsonType& j, const std::optional<T>& opt)
{
if (opt.has_value())
{
j = *opt;
}
else
{
j = nullptr;
}
}
#endif
template<typename BasicJsonType, typename T,
enable_if_t<std::is_same<T, typename BasicJsonType::boolean_t>::value, int> = 0>
void to_json(BasicJsonType& j, T b) noexcept