Compare commits

...

16 Commits

Author SHA1 Message Date
Niels Lohmann
7ffd3ae400
Merge branch 'develop' of https://github.com/nlohmann/json into feature/optional 2021-01-03 20:15:28 +01:00
Niels Lohmann
384442e8e5
🔥 remove useless line 2021-01-03 20:15:03 +01:00
Niels Lohmann
f1913fe7a9
♻️ do not include experimental headers 2021-01-03 20:10:43 +01:00
Niels Lohmann
ebb63bd1fa
Merge branch 'develop' of https://github.com/nlohmann/json into feature/optional
 Conflicts:
	test/src/unit-conversions.cpp
2020-12-30 18:41:51 +01:00
Niels Lohmann
5e55158d60
Merge branch 'develop' of https://github.com/nlohmann/json into feature/optional
 Conflicts:
	test/src/unit-conversions.cpp
2020-12-21 20:59:41 +01:00
Niels Lohmann
fedf82c904
Merge branch 'develop' of https://github.com/nlohmann/json into feature/optional 2020-08-07 09:37:56 +02:00
Niels Lohmann
b61d563ebe
Merge branches 'develop' and 'feature/optional' of https://github.com/nlohmann/json into feature/optional 2020-05-20 19:00:49 +02:00
Niels Lohmann
f25bf312fc
🏁 include right <optional> header 2020-05-17 13:58:09 +02:00
Niels Lohmann
007c6c4bc5
update tests 2020-05-16 19:37:18 +02:00
Niels Lohmann
71c80ccfd8
Merge branches 'develop' and 'feature/optional' of https://github.com/nlohmann/json into feature/optional 2020-05-16 19:31:59 +02:00
Niels Lohmann
6b31375c7c
⚗️ set CXXFLAGS 2019-12-19 12:25:27 +01:00
Niels Lohmann
a2fea87ef6
Merge pull request #1872 from remedi/feature/optional
Use JSON_HAS_CPP_17 only after it has been defined
2019-12-18 10:59:57 +01:00
Markus Palonen
1b846c9d81 Use JSON_HAS_CPP_17 only after it has been defined 2019-12-17 16:25:35 +02:00
Niels Lohmann
713038fa27
💚 overwork tests 2019-11-23 14:40:15 +01:00
Niels Lohmann
1359c56137
🏁 fix <optional> inclusion 2019-11-23 13:29:47 +01:00
Niels Lohmann
df30a0ea07
🚧 conversions for std::optional 2019-11-23 00:21:33 +01:00
5 changed files with 129 additions and 0 deletions

View File

@ -32,6 +32,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&&

View File

@ -219,6 +219,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

View File

@ -31,6 +31,10 @@
#define JSON_HAS_CPP_14
#endif
#ifdef JSON_HAS_CPP_17
#include <optional>
#endif
// disable float-equal warnings on GCC/clang
#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
#pragma GCC diagnostic push

View File

@ -2108,6 +2108,10 @@ JSON_HEDLEY_DIAGNOSTIC_POP
#define JSON_HAS_CPP_14
#endif
#ifdef JSON_HAS_CPP_17
#include <optional>
#endif
// disable float-equal warnings on GCC/clang
#if defined(__clang__) || defined(__GNUC__) || defined(__GNUG__)
#pragma GCC diagnostic push
@ -3518,6 +3522,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&&
@ -4294,6 +4313,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

View File

@ -1703,6 +1703,65 @@ TEST_CASE("JSON to enum mapping")
}
}
#ifdef JSON_HAS_CPP_17
TEST_CASE("std::optional")
{
SECTION("null")
{
json j_null;
std::optional<std::string> opt_null;
CHECK(json(opt_null) == j_null);
CHECK(std::optional<std::string>(j_null) == std::nullopt);
}
SECTION("string")
{
json j_string = "string";
std::optional<std::string> opt_string = "string";
CHECK(json(opt_string) == j_string);
CHECK(std::optional<std::string>(j_string) == opt_string);
}
SECTION("bool")
{
json j_bool = true;
std::optional<bool> opt_bool = true;
CHECK(json(opt_bool) == j_bool);
CHECK(std::optional<bool>(j_bool) == opt_bool);
}
SECTION("number")
{
json j_number = 1;
std::optional<int> opt_int = 1;
CHECK(json(opt_int) == j_number);
CHECK(std::optional<int>(j_number) == opt_int);
}
SECTION("array")
{
json j_array = {1, 2, nullptr};
std::vector<std::optional<int>> opt_array = {{1, 2, std::nullopt}};
CHECK(json(opt_array) == j_array);
CHECK(std::vector<std::optional<int>>(j_array) == opt_array);
}
SECTION("object")
{
json j_object = {{"one", 1}, {"two", 2}, {"zero", nullptr}};
std::map<std::string, std::optional<int>> opt_object {{"one", 1}, {"two", 2}, {"zero", std::nullopt}};
CHECK(json(opt_object) == j_object);
CHECK(std::map<std::string, std::optional<int>>(j_object) == opt_object);
}
}
#endif
#ifdef JSON_HAS_CPP_17
#undef JSON_HAS_CPP_17
#endif