mirror of
https://github.com/nlohmann/json.git
synced 2026-02-13 13:02:22 +00:00
🚧 conversions for std::optional
This commit is contained in:
@@ -50,6 +50,7 @@ using nlohmann::json;
|
||||
#endif
|
||||
|
||||
#if defined(JSON_HAS_CPP_17)
|
||||
#include <optional>
|
||||
#include <string_view>
|
||||
#endif
|
||||
|
||||
@@ -189,7 +190,6 @@ TEST_CASE("value conversion")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
SECTION("get an object (implicit)")
|
||||
{
|
||||
json::object_t o_reference = {{"object", json::object()},
|
||||
@@ -1575,3 +1575,63 @@ TEST_CASE("JSON to enum mapping")
|
||||
CHECK(TS_INVALID == json("what?").get<TaskState>());
|
||||
}
|
||||
}
|
||||
|
||||
#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, 3};
|
||||
std::optional<std::vector<int>> opt_array = {{1, 2, 3}};
|
||||
|
||||
CHECK(json(opt_array) == j_array);
|
||||
CHECK(std::optional<std::vector<int>>(j_array) == opt_array);
|
||||
}
|
||||
|
||||
SECTION("object")
|
||||
{
|
||||
json j_object = {{"one", 1}, {"two", 2}};
|
||||
std::map<std::string, int> m {{"one", 1}, {"two", 2}};
|
||||
std::optional<std::map<std::string, int>> opt_object = m;
|
||||
|
||||
CHECK(json(opt_object) == j_object);
|
||||
CHECK(std::optional<std::map<std::string, int>>(j_object) == opt_object);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user